Jump to content

Need a bit of help - easy stuff


simcoweb

Recommended Posts

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 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.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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.