Forum Moderators: coopster
I was wondering if someone would be able to help me out. (I do not know any php.. so please be kind :) )
I am using below code prevent same IP visiting certain page for more than once every x hours.
I am using below IFrame to call this php script
<iframe SRC="http://www.domain.com/phpscript.php" name="phpscript"
scrolling="no" border="0" frameborder="0" width="450" height="50" marginwidth="1" marginheight="1"></iframe>
I am inserting the IFrame in a .cgi file.
Now the problem, is the output of the script reads You have been banned from accessing this page. Try again in 24 hours. but it is not redirecting users to specified page.
Can anyone tell me what how to fix this problem?
Thanks.
Vishal
=========================
PHP Script Code Below
<?php$ipLog='ipLogFile.txt'; // Your logfiles name here
$timeout='24'; // How many hours to block IP
$goHere='Allowed.html'; // Allowed pages name here// PHP script by Dave Lauderdale
// Published at: www.digi-dl.comfunction recordData($REMOTE_ADDR,$ipLog,$goHere)
{
$log=fopen("$ipLog", "a+");
fputs ($log,$REMOTE_ADDR."][".time()."\n");
fclose($log);
Header ("Location: $goHere"); exit(0);
}
function checkLog($REMOTE_ADDR,$ipLog,$timeout)
{
global $valid; $ip=$REMOTE_ADDR;
$data=file("$ipLog"); $now=time();foreach ($data as $record)
{
$subdata=explode("][",$record);
if ($now < ($subdata[1]+3600*$timeout) && $ip == $subdata[0])
{
$valid=0; echo "You have been banned from accessing this page. Try again in $timeout hours.";
break;
}
}
}
checkLog($REMOTE_ADDR,$ipLog,$timeout);
if ($valid!="0") recordData($REMOTE_ADDR,$ipLog,$goHere);?>
$validvariable. You are setting it to 0 right before sending the message, then checking to see if it is not 0 so you can redirect.
function checkLog($REMOTE_ADDR,$ipLog,$timeout) {
...
$valid=0;
echo "You have been banned from accessing this page. Try again in $timeout hours.";
...
}
...
checkLog($REMOTE_ADDR,$ipLog,$timeout);
if ($valid!="0") recordData($REMOTE_ADDR,$ipLog,$goHere);However, this is probably how you actually want it to work. If you try to redirect now after sending anything to the browser, including your "Try again..." message, you are going to get a Warning: Cannot modify header information - headers already sent by ...
message from PHP. You should probably keep the code as is, but offer the user a link as opposed to trying to redirect automatically. This way they have time to read the message and can chose when they want to go to the redirected page.