Peanutz Posted October 6, 2011 Report Share Posted October 6, 2011 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... Quote Link to comment Share on other sites More sharing options...
jstern Posted October 6, 2011 Report Share Posted October 6, 2011 call the function to get the ip as a string try this: $Ban = new Ban(); if($Ban->CheckBan($Ban->GetIp()) == "banned") { $page = "banned"; } Quote Link to comment Share on other sites More sharing options...
Peanutz Posted October 6, 2011 Author Report Share Posted October 6, 2011 Oh my, thank you.. Been watching the code and tutorials for a bit too long I guess and did not spot that one lol Quote Link to comment Share on other sites More sharing options...
jstern Posted October 6, 2011 Report Share Posted October 6, 2011 No worries, you should see the stuff i miss on a daily basis sometimes lol Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.