ok here it goes. I found a bunch of scripts online that do kind of what I want, but not exactly. so i copied and pasted what i wanted from those scripts and put them together into one script. of course it does not work at all now haha. probably just a syntax issue but i cant figure it out. i am still a php noob. what i am trying to do is make the script determine if they have visited before based on the users ip address. If they have visited for the very first time, they will see a picture displayed. if they have already visited then it will redirect them. here is the broken code it does not log ip, it does not redirect, and it does not show picture. everything about it is broken. you would think there would already be a working script template available online but i could not find one that does exactly what i want. Code: <?php //get their ip address if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP"); else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); else $ip = "0.0.0.0"; //read the log file and check it against their ip and redirect if they match then exit script. $file = fopen("ips.txt", "r"); while (!feof($file)) { $line = fgets($file); if ($line = $ip) header( 'Location: http://www.yoursite.com/new_page.html' ); exit; endif; } fclose($file); //if there is no match, add their ip to the list $file = "ips.txt"; //Select file $file = fopen($file, "a"); //Appened file fwrite($file, $ip); //Write ip to file fclose($file); //Close the file //then show a picture $imagepath="test.jpg"; $image=imagecreatefromjpeg($imagepath); header('Content-Type: image/jpeg'); imagejpeg($image); ?>
you'd better use cookie to do it. example code PHP: <?php //get their ip address if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP"); else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); else $ip = "0.0.0.0"; $cvip = str_replace(".","_",$ip); $timestamp = time(); if(empty($HTTP_COOKIE_VARS[$cvip])&&empty($_COOKIE[$cvip])){ setcookie($cvip,$cvip,$timestamp+24*60*60); echo "welcome to our site, your ip is ".$ip; }else { echo "you have visited: ".$ip; } ?>
that looks good and it will work well as an alternative if I cannot get this working server side. my concern with this cookie method is that all a user would have to do is erase their cookie to circumvent this. they could not erase data on my server.
cookie will expire in a period which is specify in setcookie(). also you can save the REMOTE_ADDR to a file in your server, you can modify my code simply to achieve it.
Yes I know i can set the cookie to never expire but that is irrelevant if the cookie is erased. I understand what you are saying but I tried to save the REMOTE_ADDR to a file on my server and it is not working. If i could get it working then a cookie would be overkill really and not needed. please see the example script I posted if you have time. thank you!
I have see the your script again ,and modify some error, it work well now in my environment. try again. PHP: <?php //get their ip address if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP"); else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); else $ip = "0.0.0.0"; //read the log file and check it against their ip and redirect if they match then exit script. $file = fopen("ppwind/ips.txt", "r"); if($file){ while (!feof($file)) { $line = fgets($file); if ($line == $ip){ echo "you have visited $line = $ip"; exit; } } } fclose($file); //if there is no match, add their ip to the list $file = "ppwind/ips.txt"; //Select file $file = fopen($file, "a+"); //Appened file fwrite($file, $ip); //Write ip to file fclose($file); //Close the file //then show a picture echo "you are the first time to visited"; ?>
thank you. I adjusted your code you forgot to add the line delimiter in two lines of code it was not functioning properly but it works now. PHP: <?php//get their ip addressif (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");else $ip = "0.0.0.0"; //read the log file and check it against their ip and redirect if they match then exit script.$file = fopen("ips.txt", "r");if($file){ while (!feof($file)) { $line = fgets($file); if ($line == $ip."\n"){ //echo "you have visited $ip";header( 'Location: http://www.yoursite.com/new_page.html' ) ; exit; } }}fclose($file);//if there is no match, add their ip to the list$file = "ips.txt"; //Select file$file = fopen($file, "a+"); //Appened filefwrite($file, $ip."\n"); //Write ip to filefclose($file); //Close the file//then show a picture//echo "you are the first time to visited";$imagepath="test.jpg";$image=imagecreatefromjpeg($imagepath);header('Content-Type: image/jpeg');imagejpeg($image);?> thanks again!
I have thought of the line delimiter , but the code work in my php environment, so I didn't add it. good luck. hehe
it is better to have code that works in all environments yes? thank you for the code i am learning fast.