Jump to content

Help with my ip-log function (OOP)


Re-JeeP

Recommended Posts

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

?>[/code]

// Johan
Link to comment
https://forums.phpfreaks.com/topic/15568-help-with-my-ip-log-function-oop/
Share on other sites

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]
Hi!

Thanks for answering!

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

[code]<?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'];

?>[/code]
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]
Thanks alot!

Works now.

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

[code]<?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();

?>[/code]
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.