Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Timed visitor block

    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 Code:
    <?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?
    Last edited by delaw; 01-10-2012 at 06:11 PM.

  • #2
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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: How to write better PHP code

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, noobie !

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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.
    Last edited by XmisterIS; 01-11-2012 at 11:55 AM.

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    Have you got a LAMP (or WAMP) server set up correctly? What does localhost/index.php look like?

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
     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); ?> 

  • #9
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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

  • #10
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heres the index without the above code in.

    PHP Code:

    <!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 2009CoffeeCup SoftwareInc.
         
    http://www.coffeecup.com
    //-->
    <titleFlawless 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:absolutetop:119pxleft:1pxz-index:0; }
    div.Object268 table widthauto; }
    div.Object269 position:absolutetop:1pxleft:1pxz-index:1; }
    div.Object269 table widthauto; }
    div.Object270 position:absolutetop:142pxleft:14pxz-index:2; }
    div.Object270 table widthauto; }
    div.Object271 position:absolutetop:157pxleft:24pxz-index:3; }
    div.Object271 table widthauto; }
    div.Object272 position:absolutetop:336pxleft:39pxz-index:4; }
    div.Object272 table widthauto; }
    div.Object273 position:absolutetop:655pxleft:23pxz-index:5; }
    div.Object273 table widthauto; }
    div.Object274 position:absolutetop:396pxleft:49pxz-index:6; }
    div.Object274 table widthauto; }
    div.Object275 position:absolutetop:398pxleft:343pxz-index:7; }
    div.Object275 table widthauto; }
    div.Object276 position:absolutetop:395pxleft:638pxz-index:8; }
    div.Object276 table widthauto; }
    div.Object277 position:absolutetop:1pxleft:553pxz-index:9; }
    div.Object277 table widthauto; }
    div.Object278 position:absolutetop:1pxleft:655pxz-index:10; }
    div.Object278 table widthauto; }
    div.Object279 position:absolutetop:1pxleft:757pxz-index:11; }
    div.Object279 table widthauto; }
    div.Object280 position:absolutetop:0pxleft:859pxz-index:12; }
    div.Object280 table widthauto; }
    div.Object281 position:absolutetop:171pxleft:605pxz-index:13; }
    div.Object281 table widthauto; }
    div.Object282 position:absolutetop:457pxleft:39pxz-index:14; }
    div.Object282 table widthauto; }
    div.Object283 position:absolutetop:38pxleft:34pxz-index:15; }
    div.Object283 table widthauto; }
    div.Object284 position:absolutetop:169pxleft:41pxz-index:16; }
    div.Object284 table widthauto; }
    div.Object285 position:absolutetop:224pxleft:37pxz-index:17; }
    div.Object285 table widthauto; }
    div.Object286 position:absolutetop:404pxleft:96pxz-index:18; }
    div.Object286 table widthauto; }
    div.Object287 position:absolutetop:403pxleft:430pxz-index:19; }
    div.Object287 table widthauto; }
    div.Object288 position:absolutetop:402pxleft:723pxz-index:20; }
    div.Object288 table widthauto; }
    </
    style>


    <!-- 
    centering -->
    <
    style type="text/css">
    body {
     
    margin0px;
     
    padding 0px;
     
    text-aligncenter;
     
    height100%;
     
    width100%;
     
    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 carssee 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

  • #11
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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?
    Last edited by XmisterIS; 01-13-2012 at 09:28 AM.

  • #12
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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>"; ?>?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •