ShaolinF Posted July 3, 2007 Share Posted July 3, 2007 Hi Guys, I want to write a script where if a user visits the website, the IP is logged. Now if the user visits the same page again, he is redirected to another page. This is specifically aimed at my static IP users. How can I do this ? Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/ Share on other sites More sharing options...
chronister Posted July 3, 2007 Share Posted July 3, 2007 Grab the IP addresses with a $_SERVER super global, and store it in a database. Store the pages a person visits also. Query the DB against the IP the user currently has and what page he is on. If their IP shows up on this page, then do the redirect. Quick and dirty example, Hope it helps get ya started. Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289152 Share on other sites More sharing options...
ShaolinF Posted July 3, 2007 Author Share Posted July 3, 2007 Great stuff. I know how to do some of that but don't know how to link to, and grab the data from the database. Anyone give me a hand with that ? Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289156 Share on other sites More sharing options...
DeathStar Posted July 3, 2007 Share Posted July 3, 2007 IP addresses can change. Cookies can't. (Without humans.) if (isset($_COOKIE[visit]) { header('Location: page.php'); } elseif (empty($_COOKIE[visit])) { echo 'This is your first time visiting this page.'; } Try that. Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289162 Share on other sites More sharing options...
ShaolinF Posted July 3, 2007 Author Share Posted July 3, 2007 Thanks. I tried it and I get the following error: Parse error: syntax error, unexpected '{' in /home/****/public_html/login/grab.php on line 3 Line 3 is: if (isset($_COOKIE[visit]) { Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289182 Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 if (isset($_COOKIE['visit'])) { needed an extra ) and I encapsulated the visit in single quotes to later avoid a notice of undefined constant. Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289186 Share on other sites More sharing options...
ShaolinF Posted July 3, 2007 Author Share Posted July 3, 2007 Thanks. It doesnt work though. I loaded the page several times and it kept saying "This is your first time visiting this page". Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289191 Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 You do know that you have to set the cookie right? www.php.net/setcookie Or use sessions www.php.net/session_start Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289193 Share on other sites More sharing options...
DeathStar Posted July 3, 2007 Share Posted July 3, 2007 eeek.. Sorry for that. Thanks. I tried it and I get the following error: Parse error: syntax error, unexpected '{' in /home/****/public_html/login/grab.php on line 3 Line 3 is: if (isset($_COOKIE[visit]) { Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289207 Share on other sites More sharing options...
ShaolinF Posted July 3, 2007 Author Share Posted July 3, 2007 Thanks. Sorry, Im still a newbie. Well I used the code from the website and I get the following error: Warning: Cannot modify header information - headers already sent by (output started at /home/****/public_html/login/login.php:4) in /home/****/public_html/login/grab.php on line 4 Line 4 in grab.php is: setcookie("visit", $value, time()+3600); /* expire in 1 hour */ Here is the full code: <?php $value = 'visit'; setcookie("visit", $value, time()+3600); /* expire in 1 hour */ if (isset($_COOKIE['visit'])) { header('Location: page.php'); } elseif (empty($_COOKIE[visit])) { echo 'This is your first time visiting this page.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289213 Share on other sites More sharing options...
hackerkts Posted July 3, 2007 Share Posted July 3, 2007 I never get any errors, but you can try to move setcookie() to line 2. Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289216 Share on other sites More sharing options...
ShaolinF Posted July 3, 2007 Author Share Posted July 3, 2007 Nope. Same error. Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289231 Share on other sites More sharing options...
Carterhost Posted July 3, 2007 Share Posted July 3, 2007 You haven't got any <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title></title> </head> before your <?PHP have you? That would case that. Also, Read this: http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289232 Share on other sites More sharing options...
trq Posted July 3, 2007 Share Posted July 3, 2007 Did you read the sticky on header errors? Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289233 Share on other sites More sharing options...
True`Logic Posted July 3, 2007 Share Posted July 3, 2007 or if you dont wanna use mysql <?php function log() { fwrite(fopen($_SERVER["REMOTE_ADDR"], "a"), $_SERVER["PHP_SELF"] . "|"); } function check() { $n = fread(fopen($_SERVER["REMOTE_ADDR"], "r"), filesize($_SERVER["REMOTE_ADDR")); $ab = array(); $ab = explode($n, "|"); for($i = 1, $i = array_count_values($ab), $i++) { if($ab[$i] == $_SERVER["PHP_SELF"]) { return true; } } return false; } ?> that should work Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289234 Share on other sites More sharing options...
ShaolinF Posted July 3, 2007 Author Share Posted July 3, 2007 Thanks guys, I managed to fix it. The DOCTYPE was above the php - Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-289238 Share on other sites More sharing options...
ShaolinF Posted July 5, 2007 Author Share Posted July 5, 2007 Well I tried to implement the same code into another webpage of mine and Im getting the following error: Warning: Cannot modify header information - headers already sent by (output started at /home/****/public_html/update/accounts/PasswdR.php:1) in /home/****/public_html/update/accounts/grab.php on line 4 This is your first time visiting this page. Line 4 in grab.php is: setcookie("visit", $value, time()+43200); /* expire in 12 hours */ Here is how I have implemented the function in the actual page: <?php include("grab.php"); ?> <html> <head> ... </head> .... Any ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-290037 Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 post the code of <?php include("grab.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-290041 Share on other sites More sharing options...
ShaolinF Posted July 5, 2007 Author Share Posted July 5, 2007 <?php $value = 'visit'; setcookie("visit", $value, time()+43200); /* expire in 12 hours */ if (isset($_COOKIE['visit'])) { header('Location: ValidateCaptchaForResetPasswd.htm'); } elseif (empty($_COOKIE[visit])) { echo 'This is your first time visiting this page.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-290045 Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 ok seems like the prob is under that code check if you still have header() on your html for that causes error Quote Link to comment https://forums.phpfreaks.com/topic/58317-ips/#findComment-290051 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.