simcoweb Posted July 9, 2006 Share Posted July 9, 2006 Hello:I'm setting up a simple PHP page to display as a default directory home page to protect against intruders searching for directories on our sites that don't contain an index.xxx file. This way they hit my 'warning' page, their IP and the date are logged to a file and they are redirected back to the site's home page. Here's the code:<?php$ip = $_SERVER['REMOTE_ADDR'];$filepath = "http://www.nameofsite.com/pictures/";$ips = 'ips.txt';//set the date$v_date = date("l d F H:i:s");echo "Unauthorized Access Warning Message";print "<h3>You should not be here</h4><p>";echo "Your IP address is ". $ip . " and has been logged<p>";echo "$v_date<p>";echo "You will be redirected to an authorized page";//open the file and write the IP + date$fp = fopen("$filepath", "$ips", "a") or die("cannot open $ips\n");fputs($fp, "IP: $ip - DATE: $v_date\n\n");//close the filefclose($fp);?><script language=javascript> <!-- // Redirect codewindow.setTimeout("location='http://www.nameofsite.com/index.php'",6000);//--></script>I'm inserting this page into multiple directories but want it to write to one common file. The way I have it set up now with these variables:$ip = $_SERVER['REMOTE_ADDR'];$filepath = "http://www.nameofsite.com/pictures/";$ips = 'ips.txt';it writes to the file fine but instead of writing the person's IP address it writes the IP address of the server. However, on that page it displays the visitor's IP address. I can't figure out why it's not writing the visitor's IP address and, instead, is writing the server's IP to the file. Here's the page in question: http://www.guitar4sale.com/pictures/Notice it displays your IP address. It's not writing that IP. I think it's something to do with the $filepath variable and the location I have placed there for the common file.Help anyone? ;D Quote Link to comment https://forums.phpfreaks.com/topic/14116-need-a-bit-of-help-easy-stuff/ Share on other sites More sharing options...
cmgmyr Posted July 9, 2006 Share Posted July 9, 2006 try:$ip = $_REMOTE_ADDR;-Chris Quote Link to comment https://forums.phpfreaks.com/topic/14116-need-a-bit-of-help-easy-stuff/#findComment-55292 Share on other sites More sharing options...
ShogunWarrior Posted July 9, 2006 Share Posted July 9, 2006 In your fopen code:[code]$fp = fopen("$filepath", "$ips", "a") or die("cannot open $ips\n");[/code]You were passing filepath to the function, so $ips was the mode an 'a' wasn't used.What you want to do is have an absolute filepath for your server, not an internet URL.So, instead of [b]http://www.nameofsite.com/log/ips.txt[/b] you should use [b]/log/ips.txt[/b] as the file.[code]$fp = fopen("$ips", "a") or die("cannot open $ips\n"); [/code]You could try:[code]$ip = getenv("HTTP_CLIENT_IP"); [/code]Or:[code]$ip = getenv("HTTP_X_FORWARDED_FOR");[/code]Hopefully that will solve it because otherwise it's illogical. Quote Link to comment https://forums.phpfreaks.com/topic/14116-need-a-bit-of-help-easy-stuff/#findComment-55294 Share on other sites More sharing options...
simcoweb Posted July 10, 2006 Author Share Posted July 10, 2006 First, thanks for your efforts and ideas. I got it to work by using a combination of the ideas you suggested. Here's the final code:<?php$ip = $_SERVER["REMOTE_ADDR"];$ips = "../pictures/ips.txt";//set the date$v_date = date("l d F H:i:s");echo "Unauthorized Access Warning Message";print "<h3>You should not be here</h4><p>";echo "Your IP address is ". $ip . " and has been logged<p>";echo "$v_date<p>";echo "You will be redirected to an authorized page";//open the file and write the IP + date$fp = fopen("$ips", "a") or die("cannot open $ips\n");fputs($fp, "IP: $ip - DATE: $v_date\n\n");//close the filefclose($fp);?>Note I took out the variable from the fopen function as you suggested. My 'logical' mind thought I needed a path indicator there in order to find the right file. The 'getenv' didn't seem to work so I kept the original 'REMOTE_ADDR' call there and it works fine. The key to all this is the ips.txt file resides in a specific folder that are on the same level as the others instead of being a sub-directory. Therefore I had to direct the script to write to that common file.I'm a complete noob with PHP but am studying tutorials like a mad man to learn it as quick as possible. These forums are great. Keep up the good work :) Quote Link to comment https://forums.phpfreaks.com/topic/14116-need-a-bit-of-help-easy-stuff/#findComment-55330 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.