Page 1 of 1
One time webpage !
Posted: Thu Jul 13, 2006 5:35 am
by herbat8
Request
What i want is a webpage that only can be viewed once per ip.. This script doesnt work can someone please help me rewrite this ?
The problem
When i vist the page for the first time with my ip it will print out Hello World. But later when i try reaching the page from another ip it give's me google instead of Hello world

that is what needs to be fixed if you can see the problem please tell me how to modify it
Php read user ip
php write user ip to file if already exists close connection
The Code
Code: Select all
<?
$key = $_SERVER['REMOTE_ADDR'];
$fc=file("some.txt");
$f=fopen("some.txt","w");
foreach($fc as $line)
{
if (!strstr($line,$key))
fputs($f,$line);
header("Location:http://www.google.com");
}
fclose($f);
$text = "\n$key";
$file = fopen('some.txt', 'a');
fwrite($file,$text);
fclose($file);
echo "Hello World";
?>
Posted: Thu Jul 13, 2006 6:00 am
by jamiel
Your header isn't within parenthesis, so is outside of your if statement, so it will always do the header redirect. Also, I don't know why you are doing an fputs in your foreach if you are writing the IP address later on.
Posted: Thu Jul 13, 2006 6:01 am
by Jenk
You'll need to confirm that the machines are not running through the same proxy or gateway.
e.g. two machines in one room, using same net connection (router, or ICS) will have the same external IP.
Thank you
Posted: Thu Jul 13, 2006 6:38 am
by herbat8
Code: Select all
<?
$key = $_SERVER['REMOTE_ADDR'];
$fc=file("some.txt");
foreach($fc as $line)
{
if (!strstr($line,$key))
then {header("Location:http://www.google.com");}
}
$text = "\n$key";
$file = fopen('some.txt', 'a');
fwrite($file,$text);
fclose($file);
echo "Hello World";
?>
Im sorry im a complete newbie but um should the code look someting like this ?
Posted: Thu Jul 13, 2006 6:46 am
by jamiel
Erm, So you are trying to redirect anyone who's IP Address is in the file to Google right?
Code: Select all
$userAddress = $_SERVER['REMOTE_ADDRESS'];
$loggedVisited = array_map('rtrim', file("some.txt"));
foreach ($loggedVisted as $address) {
if (strstr($address, $userAddress)) {
header("Location: http://www.google.com");
}
}
$file = fopen('some_txt', 'a');
fwrite($file, $userAddress . "\n");
fclose($file);
echo "Hello World!";
Untested but should work.
P.S. 'then' does not exist in PHP as far as I am aware
EDIT: Side note ... I did the array_map of rtrim because file add's an irritating line break into your array values.
Posted: Thu Jul 13, 2006 6:52 am
by JayBird
You do know that IPs are not at all, in any way, reliably related to users!?
Just FYI
Thank you
Posted: Thu Jul 13, 2006 8:19 am
by herbat8
Id like to say this much about the users and this forum. It's the best , best answering time , nice'st users , best reply's.
God bless you all for helping me . I hope you will get rich and all your dreams will be answered. As for me my dream has been answered
Once again thank you !!
Code: Select all
$userAddress = $_SERVER['REMOTE_ADDR'];
$loggedVisited = array_map('rtrim', file("some.txt"));
foreach ($loggedVisited as $address) {
if (strstr($address, $userAddress)) {
header("Location: http://www.google.com");
}
}
$file = fopen('some.txt', 'a');
fwrite($file, $userAddress . "\n");
fclose($file);
echo "Hello World!";
Posted: Thu Jul 13, 2006 12:18 pm
by daedalus__
Could I ask why you want to do this?