Re-JeeP Posted July 25, 2006 Share Posted July 25, 2006 Hi!I'm having some problem with my ip-log function.Could anyone give me a good solution on how to handle the ip in the class!?Difficult to explane, hope someone get what i mean![code]<?phpclass mysql{ function db_conn($database) { $mysql_server = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_database = $database; $conn = mysql_connect($mysql_server, $mysql_user, $mysql_password); mysql_select_db($mysql_database, $conn); }}class iplog{ function check() { $sql = mysql_query("SELECT ip FROM log WHERE ip = '{$this->ip}'"); if(mysql_num_rows($sql) == 0) { return true; } else { return false; } } function set($ip) { if($this->check()) { $sql = mysql_query("INSERT INTO log (ip, times) VALUES ('{$ip}', '1')"); } else { $sql = mysql_query("UPDATE log SET times = times + 1 WHERE ip = '{$ip}'"); } }}$mysql = new mysql;$mysql->db_conn("iplog");$iplog = new iplog;$iplog->set($_SERVER['REMOTE_ADDR']);?>[/code]// Johan Quote Link to comment https://forums.phpfreaks.com/topic/15568-help-with-my-ip-log-function-oop/ Share on other sites More sharing options...
wildteen88 Posted July 25, 2006 Share Posted July 25, 2006 In the check function for your iplog class you are trying to access an internal variable called ip within that class, which doesnt exists, so $this->ip will not return anthing. So to solve this, first setup an blank ip var within the class:[code]class iplog{ // initiate our class variables $ip = ''; // rest of class here}[/code]Now to to set a value for ip variable out side of the class you do this:[code]$iplog->ip = $_SERVER['REMOTE_ADDR'];[/code]Now when you call your check method $this->ip will now return the value of the ip variable.Or pass the $ip var through to the check function, like so:[code]if($this->check($ip)) {[/code]Then with your check method do this:[code]function check($ip) { $sql = mysql_query("SELECT ip FROM log WHERE ip = '{$ip}'"); if(mysql_num_rows($sql) == 0) { return true; } else { return false; } }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15568-help-with-my-ip-log-function-oop/#findComment-63311 Share on other sites More sharing options...
Re-JeeP Posted July 25, 2006 Author Share Posted July 25, 2006 Hi!Thanks for answering!I know have this code. But it just gives me a blank ip in the database.[code]<?phpclass mysql{ function db_conn($database) { $mysql_server = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_database = $database; $conn = mysql_connect($mysql_server, $mysql_user, $mysql_password); mysql_select_db($mysql_database, $conn); }}class iplog{ var $ip = ''; function check($ip) { $sql = mysql_query("SELECT ip FROM log WHERE ip = '{$ip}'"); if(mysql_num_rows($sql) == 0) { return true; } else { return false; } } function set() { if($this->check($ip)) { $sql = mysql_query("INSERT INTO log (ip, times) VALUES ('{$ip}', '1')"); } else { $sql = mysql_query("UPDATE log SET times = times + 1 WHERE ip = '{$ip}'"); } }}$mysql = new mysql;$mysql->db_conn("iplog");$iplog = new iplog;$iplog->set();$iplog->ip = $_SERVER['REMOTE_ADDR'];?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15568-help-with-my-ip-log-function-oop/#findComment-63333 Share on other sites More sharing options...
king arthur Posted July 25, 2006 Share Posted July 25, 2006 Because your set() function is trying to access the $ip variable but it is not within scope - it needs to be[code] function set() { if($this->check($this->ip)) { $sql = mysql_query("INSERT INTO log (ip, times) VALUES ('{$this->ip}', '1')"); } else { $sql = mysql_query("UPDATE log SET times = times + 1 WHERE ip = '{$this->ip}'"); } }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15568-help-with-my-ip-log-function-oop/#findComment-63337 Share on other sites More sharing options...
Re-JeeP Posted July 25, 2006 Author Share Posted July 25, 2006 Thanks alot!Works now.I changed it a bit. The script now look like this:[code]<?phpclass mysql{ function db_conn($database) { $mysql_server = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_database = $database; $conn = mysql_connect($mysql_server, $mysql_user, $mysql_password); mysql_select_db($mysql_database, $conn); }}class iplog{ var $ip = ''; function set_ip() { $this->ip = $_SERVER['REMOTE_ADDR']; } function check() { $sql = mysql_query("SELECT ip FROM log WHERE ip = '{$this->ip}'"); if(mysql_num_rows($sql) == 0) { return true; } else { return false; } } function set() { if($this->check($this->set_ip())) { $sql = mysql_query("INSERT INTO log (ip, times) VALUES ('{$this->ip}', '1')"); } else { $sql = mysql_query("UPDATE log SET times = times + 1 WHERE ip = '{$this->ip}'"); } }}$mysql = new mysql;$mysql->db_conn("iplog");$iplog = new iplog;$iplog->set();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15568-help-with-my-ip-log-function-oop/#findComment-63343 Share on other sites More sharing options...
king arthur Posted July 25, 2006 Share Posted July 25, 2006 Technically, although that may work, it's not quite correct as in the line[code]if($this->check($this->set_ip())) {[/code]you are passing a value into $this->check() which isn't expecting one, and using $this->set_ip() to return a value true or false, which it doesn't explicitly do.This would be better:[code]$this->set_ip();if($this->check()) {[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15568-help-with-my-ip-log-function-oop/#findComment-63351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.