Jump to content


Photo

Need a bit of help - easy stuff


  • Please log in to reply
3 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 09 July 2006 - 08:36 PM

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.nameofsit...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 file
fclose($fp);

?>

<script language=javascript> <!--
// Redirect code
window.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.nameofsit...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


#2 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 09 July 2006 - 08:59 PM

try:

$ip = $_REMOTE_ADDR;

-Chris

#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 09 July 2006 - 09:05 PM

In your fopen code:
$fp = fopen("$filepath", "$ips", "a") or die("cannot open $ips\n");
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 http://www.nameofsite.com/log/ips.txt you should use /log/ips.txt as the file.

$fp = fopen("$ips", "a") or die("cannot open $ips\n"); 

You could try:
$ip = getenv("HTTP_CLIENT_IP"); 
Or:
$ip = getenv("HTTP_X_FORWARDED_FOR");

Hopefully that will solve it because otherwise it's illogical.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#4 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 10 July 2006 - 12:29 AM

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 file
fclose($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 :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users