...

View Full Version : Timed visitor block



delaw
01-10-2012, 10:21 AM
Hi folks,

Ive set up a website using VSD, and I would like a auto timed block on my site, basically when someone visits the page they have their ip logged, if they come back they see a "go away page" for 72 hours after that they are allowed back in.

Its for voting on pics of cars for competions so I dont want someone clicking away and making the score go up.

Ive tried to add this code into my main page but it shows the code at the top and it never logs any ip's on the txt file.



<?php
$ipLog='ipLogFile.txt'; // Your logfiles name here
$timeout='72'; // How many hours to block IP
$goHere='Allowed.html'; // Allowed pages name here

// published at: scripts.tropicalpcsolutions.com


$register_globals = (bool) ini_get('register_gobals');
if ($register_globals) $vis_ip = getenv(REMOTE_ADDR);
else $vis_ip = $_SERVER['REMOTE_ADDR'];


function recordData($vis_ip,$ipLog,$goHere)
{
$log=fopen("$ipLog", "a+");
fputs ($log,$vis_ip."][".time()."\n");
fclose($log);

// The below header function was causing errors in apache
//so I changed it to echo the script tag.
//Header ("Location: $goHere"); exit(0);
echo "<script>location.replace('$goHere')</script>"; exit(0);
}
function checkLog($vis_ip,$ipLog,$timeout)
{
global $valid; $ip=$vis_ip;
$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($vis_ip,$ipLog,$timeout);
if ($valid!="0") recordData($vis_ip,$ipLog,$goHere);

?>


Can anyone help?

XmisterIS
01-10-2012, 03:32 PM
Please could you put your code between PHP BB tags and indent it correctly? I makes it painfully difficult to read otherwise. See item 1.1 here: http://www.codingforums.com/showthread.php?t=220807

delaw
01-10-2012, 07:11 PM
Sorry, noobie !

delaw
01-11-2012, 09:14 AM
Anyone?

XmisterIS
01-11-2012, 12:51 PM
in checkLog, $valid is never being set to anything other than 0. Therefore, $valid != "0" will never evaluate to TRUE.

Also, I personally wouldn't use globals - they're ok for small bits of code but if it gets larger, the code rapidly turns into a complete dog's dinner which is excruciatingly painful to maintain. (I have had to take on other people's code to fix it ... and then take a walk round the block, groaning, after having looked at the complete nightmare that they've concocted ...)

Personally, I would pass $valid by reference as an extra argument to checkLog, or have checkLog return a value that is assigned to $valid.

delaw
01-11-2012, 03:59 PM
Thanks for the advice, I cant figure why its showing the code on the page when I insert it in my header. Any ideas? Im thinking it has something to do with the last echo?

XmisterIS
01-11-2012, 05:48 PM
Have you got a LAMP (or WAMP) server set up correctly? What does localhost/index.php look like?

delaw
01-11-2012, 08:22 PM
I had the site on a 123-reg site but took it down till i get this sorted.

If I put the code after the first head tag I get the below showing across the top.



location.replace('$goHere')"; exit(0); } function checkLog($vis_ip,$ipLog,$timeout) { global $valid; $ip=$vis_ip; $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($vis_ip,$ipLog,$timeout); if ($valid!="0") recordData($vis_ip,$ipLog,$goHere); ?>

XmisterIS
01-12-2012, 10:34 PM
Can you post the entire file, exactly as it is uploaded to your server?

Also, I bet that the server complains about the header function because you are outputting to the browser before you call header, which is not allowed. See here: http://uk.php.net/manual/en/function.header.php

delaw
01-13-2012, 12:20 AM
Heres the index without the above code in.




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>


<!--
This Website was created with CoffeeCup Visual Site Designer.
Copyright 2009, CoffeeCup Software, Inc.
http://www.coffeecup.com
//-->
<title> Flawless cars </title>


<style type="text/css">
.textstyle0 {font-family:Arial;font-weight:normal;font-style:normal;font-size:14pt;text-decoration:none;color:#bebeaf;}
.textstyle1 {font-family:Arial;font-weight:bold;font-style:normal;font-size:14pt;text-decoration:none;color:#e87b23;}
.textstyle2 {font-family:Arial;font-weight:normal;font-style:normal;font-size:24pt;text-decoration:none;color:#bebfaf;}
.textstyle3 {font-family:Arial;font-weight:normal;font-style:normal;font-size:14pt;text-decoration:none;color:#bebfaf;}
.textstyle4 {font-family:Arial;font-weight:bold;font-style:normal;font-size:12pt;text-decoration:none;color:#e87b23;}
</style>

<style type="text/css">
div.Object268 { position:absolute; top:119px; left:1px; z-index:0; }
div.Object268 table { width: auto; }
div.Object269 { position:absolute; top:1px; left:1px; z-index:1; }
div.Object269 table { width: auto; }
div.Object270 { position:absolute; top:142px; left:14px; z-index:2; }
div.Object270 table { width: auto; }
div.Object271 { position:absolute; top:157px; left:24px; z-index:3; }
div.Object271 table { width: auto; }
div.Object272 { position:absolute; top:336px; left:39px; z-index:4; }
div.Object272 table { width: auto; }
div.Object273 { position:absolute; top:655px; left:23px; z-index:5; }
div.Object273 table { width: auto; }
div.Object274 { position:absolute; top:396px; left:49px; z-index:6; }
div.Object274 table { width: auto; }
div.Object275 { position:absolute; top:398px; left:343px; z-index:7; }
div.Object275 table { width: auto; }
div.Object276 { position:absolute; top:395px; left:638px; z-index:8; }
div.Object276 table { width: auto; }
div.Object277 { position:absolute; top:1px; left:553px; z-index:9; }
div.Object277 table { width: auto; }
div.Object278 { position:absolute; top:1px; left:655px; z-index:10; }
div.Object278 table { width: auto; }
div.Object279 { position:absolute; top:1px; left:757px; z-index:11; }
div.Object279 table { width: auto; }
div.Object280 { position:absolute; top:0px; left:859px; z-index:12; }
div.Object280 table { width: auto; }
div.Object281 { position:absolute; top:171px; left:605px; z-index:13; }
div.Object281 table { width: auto; }
div.Object282 { position:absolute; top:457px; left:39px; z-index:14; }
div.Object282 table { width: auto; }
div.Object283 { position:absolute; top:38px; left:34px; z-index:15; }
div.Object283 table { width: auto; }
div.Object284 { position:absolute; top:169px; left:41px; z-index:16; }
div.Object284 table { width: auto; }
div.Object285 { position:absolute; top:224px; left:37px; z-index:17; }
div.Object285 table { width: auto; }
div.Object286 { position:absolute; top:404px; left:96px; z-index:18; }
div.Object286 table { width: auto; }
div.Object287 { position:absolute; top:403px; left:430px; z-index:19; }
div.Object287 table { width: auto; }
div.Object288 { position:absolute; top:402px; left:723px; z-index:20; }
div.Object288 table { width: auto; }
</style>


<!-- centering -->
<style type="text/css">
body {
margin: 0px;
padding : 0px;
text-align: center;
height: 100%;
width: 100%;
background-color: #3d3a37;
}


a:link {
color: #6cd7f5;
}


a:visited {
color: #6cd7f5;
}

</style>
<!-- --------- -->



<!-- centering -->
<style type="text/css">
#container { position:relative; margin: 0px auto 0 auto; height: 100%; width:960px; text-align:left; padding-left:0px;}
</style>


<!-- centering -->
<!-- --------- -->

</head>

<body >


<!-- centering -->
<div id="container">
<!-- --------- -->

<table style="height:785px;" width="960" border=0 cellspacing=0 cellpadding=0>
<tr>
<td>
<br>
</td>
</tr>
</table>

<div class="Object268"><img style="border:none;" src="files/IMG_0.jpg" alt="" width=959 height=330></div>

<div class="Object269"><img style="border:none;" src="files/IMG_1.jpg" alt="" width=959 height=119></div>

<div class="Object270"><img style="border:none;" src="files/IMG_2.jpg" alt="" width=931 height=624></div>

<div class="Object271"><img style="border:none;" src="files/IMG_3.jpg" alt="" width=913 height=607></div>

<div class="Object272"><img style="border:none;" src="files/hero_border.jpg" alt="" width=556 height=2></div>

<div class="Object273"><img style="border:none;" src="files/IMG_5.jpg" alt="" width=913 height=103></div>

<div class="Object274"><img style="border:none;" src="files/IMG_6.jpg" alt="" width=269 height=37></div>

<div class="Object275"><img style="border:none;" src="files/IMG_6.jpg" alt="" width=269 height=37></div>

<div class="Object276"><img style="border:none;" src="files/IMG_6.jpg" alt="" width=269 height=37></div>

<div class="Object277"><a href="index.html" ><img style="border:none;" src="files/IMG_9.jpg" alt="" width=100 height=66></a></div>

<div class="Object278"><a href="Vote.html" ><img style="border:none;" src="files/IMG_10.jpg" alt="" width=100 height=66></a></div>

<div class="Object279"><a href="about.html" ><img style="border:none;" src="files/IMG_11.jpg" alt="" width=100 height=66></a></div>

<div class="Object280"><a href="contact.html" ><img style="border:none;" src="files/IMG_12.jpg" alt="" width=100 height=66></a></div>

<div class="Object281"><img style="border:none;" src="files/IMG_13.jpg" alt="" width=322 height=210></div>

<div class="Object282"><table border=0 cellspacing=0 cellpadding=0><tr><td valign="top" align="left"><div class="text-align:left;margin-left:0px;text-indent:0px;"><span class="textstyle0">Discover the cars, see the work <br>
<br>
</span></div></td></tr></table></div>

<div class="Object283"><table border=0 cellspacing=0 cellpadding=0><tr><td valign="top" align="left"><div class="text-align:left;margin-left:0px;text-indent:0px;"><span class="textstyle1">Flawless cars<br>
</span></div></td></tr></table></div>

<div class="Object284"><table border=0 cellspacing=0 cellpadding=0><tr><td valign="top" align="left"><div class="text-align:left;margin-left:0px;text-indent:0px;"><span class="textstyle2">Vehicle Detailing Experts.<br>
</span></div></td></tr></table></div>

<div class="Object285"><table border=0 cellspacing=0 cellpadding=0><tr><td valign="top" align="left"><div class="text-align:left;margin-left:0px;text-indent:0px;"><span class="textstyle3">Take a look at the cars and vote for the best <br>
The vehicle with the most votes wins
<br>
</span></div></td></tr></table></div>

<div class="Object286"><table border=0 cellspacing=0 cellpadding=0><tr><td valign="top" align="center"><div class="text-align:center;margin-left:0px;text-indent:0px;"><span class="textstyle4"><a href="Vote.html">HOW TO VOTE<br>
</a></span></div></td></tr></table></div>

<div class="Object287"><table border=0 cellspacing=0 cellpadding=0><tr><td valign="top" align="center"><div class="text-align:center;margin-left:0px;text-indent:0px;"><span class="textstyle4"><a href="about.html">ABOUT US<br>
</a></span></div></td></tr></table></div>

<div class="Object288"><table border=0 cellspacing=0 cellpadding=0><tr><td valign="top" align="center"><div class="text-align:center;margin-left:0px;text-indent:0px;"><span class="textstyle4"><a href="contact.html">CONTACT US<br>
</a></span></div></td></tr></table></div>
</div>
<!-- --------- -->

</body>
</html>

XmisterIS
01-13-2012, 10:25 AM
Ok, that's a start. I please could you indent your markup to make it easier for you and others (me!) to read - it's particularly difficult to read.

Can you post the code exactly as it is on your server? I.e. the precise code that generates the error?

delaw
01-13-2012, 01:43 PM
No error appears which is confusing me, I get part of the script showing wherever I place the first php code.

The part it shows is after the echo underneath this

" // The below header function was causing errors in apache
//so I changed it to echo the script tag.
//Header ("Location: $goHere"); exit(0); "

Im also not getting anything written to my iplogfile.

Thanks a lot for helping.

All Im after is a working code that will log visiting ip addresses then if they try to come back within say 72 hours they will get some kind of message saying go away or similar.

XmisterIS
01-13-2012, 02:22 PM
Ok, but please could you post the file exactly as it is stored on your server? Perhaps attach the php file itself to your next reply?

And do other echo statements work? E.g. <?php echo "<p>Hello World!</p>"; ?>?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum