Jump to content

Starting with OOP, ran in a problem


Peanutz

Recommended Posts

Okay, so I already have a website made the old fasion way and I wanted to learn OOP so I intend to completely rework the code. Since I am a total noob at this I already came across a problem I don't know the answer. Yes I did go through the basic tutorial.

I wanted to start with my ban system and made these 2 classes to check for the user IP and make sure he aint using any crappy proxies and check if his IP is in my database.

 

class Ip {

var $ip;

function GetIp() {
   $ip = $_SERVER['REMOTE_ADDR'];

   if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
       $ip = $_SERVER['HTTP_CLIENT_IP'];
   } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
       $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
   }
    return $ip;

}

}

class Ban extends Ip{

var $ip;

        function __construct() {
               $this->ip = Ip::GetIp();

		}



	function CheckBan($ip){

		$query = mysql_query("SELECT * FROM Banned");
		if(mysql_num_rows($query) > 0){
		while($row = mysql_fetch_array( $query ))
		{ 
		$deny[] = $row['IP'];
		}

		if (in_array ($ip, $deny)) {
		$Status = "banned";
		return $Status; 
		}else {
			 // this is for wild card matches
			 foreach($deny as $bannedip) {
				  if(preg_match("/$bannedip/",$ip)) {
					$Status = "banned";
					return $Status;
				    } 
		}
		}
		}

	}
}

 

If the result is a string "banned" then we redirect him to the banned.php page

 

$Ban = new Ban();
if($Ban->CheckBan($Ban) == "banned")
{$page = "banned";}

 

But I get this error

Warning: preg_match() expects parameter 2 to be string, object given in C:\wamp\www\class.php

 

I understand that it means that the $ip is not a string but an object but I dont know how to transform it into a string >.<

And I dont understand why is it defined as an object, I thought return $ip in the Ip class would return a string...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...