Jump to content


Photo

Help with my ip-log function (OOP)


  • Please log in to reply
5 replies to this topic

#1 Re-JeeP

Re-JeeP
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 25 July 2006 - 10:12 AM

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!

<?php

class 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']);

?>

// Johan

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 July 2006 - 11:14 AM

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:
class iplog
{
    // initiate our class variables
    $ip = '';

    // rest of class here
}
Now to to set a value for ip variable out side of the class you do this:
$iplog->ip = $_SERVER['REMOTE_ADDR'];
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:
if($this->check($ip)) {
Then with your check method do this:
function check($ip)
	{
		$sql = mysql_query("SELECT ip FROM log WHERE ip = '{$ip}'");
		
		if(mysql_num_rows($sql) == 0) {
			return true;
		} else {
			return false;
		}
	}


#3 Re-JeeP

Re-JeeP
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 25 July 2006 - 12:08 PM

Hi!

Thanks for answering!

I know have this code. But it just gives me a blank ip in the database.

<?php

class 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'];

?>


#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 25 July 2006 - 12:19 PM

Because your set() function is trying to access the $ip variable but it is not within scope - it needs to be
	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}'");
		}
	}

Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 Re-JeeP

Re-JeeP
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 25 July 2006 - 12:31 PM

Thanks alot!

Works now.

I changed it a bit. The script now look like this:

<?php

class 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();

?>


#6 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 25 July 2006 - 12:49 PM

Technically, although that may work, it's not quite correct as in the line
if($this->check($this->set_ip())) {
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:
$this->set_ip();
if($this->check()) {

Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users