User can open the index.php page just once.

Discussion in 'PHP' started by Ethan8, Apr 1, 2010.

  1. #1
    Hi,

    Every user (IP) may open the index.php page just once.
    Can someone give me a good code for it. I know a bit of PHP but this hard for me to find.:rolleyes:

    I know you can bypass this code using a proxy but that is not really a problem. If you use a proxy on my site you can't download large files.

    Index.php
    <?php
    include 'check.php';
    ?>
    <html>
    CONTENT
    </html>
    PHP:
    Check.php
    ??
    PHP:

     
    Ethan8, Apr 1, 2010 IP
  2. CoreyPeerFly

    CoreyPeerFly Notable Member Affiliate Manager

    Messages:
    394
    Likes Received:
    24
    Best Answers:
    5
    Trophy Points:
    240
    #2
    The easiest way in my opinion would be to use sessions... they can of course just delete them though, so you may want to store the IPs in a database and check their IP against the database.

    Check.php would look something like this:
    <?php
    	session_start();
    	$ip = $_SERVER['REMOTE_ADDR'];
    
    	if(isset($_SESSION['ip']) && $ip == $_SESSION['ip'])
    	{
    		die("Error.");
    	} else {
    		$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
    	}
    ?>
    PHP:
     
    CoreyPeerFly, Apr 1, 2010 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    If you were using a database would look something like:

    // MySQL connection info.
    
    $Data = mysql_fetch_assoc( mysql_query("SELECT COUNT(*) FROM visited WHERE ip = '" . $_SERVER['REMOTE_ADDR'] . "'") );
    $Amount = (int) $Data['COUNT(*)'];
    
    if($Amount > 1) {
    	exit;
    }
    PHP:
    Obviously should be improved on, but you get a rough idea.
     
    Alex Roxon, Apr 2, 2010 IP
  4. cono1616

    cono1616 Peon

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Something like this maybe?

    
    <?php
    $ip = $_SERVER['REMOTE_ADDR'];
    
    $sql = mysql_query("SELECT * FROM `iplogs` WHERE `ip` = '$ip'");
    
    $num = mysql_num_rows($sql);
    
    
    if($num == "0"){
    
    //PAGE CONTENT GOES HERE
    
    
    mysql_query("INSERT INTO `iplogs` (`ip`) VALUES (`$ip`);");
    }else {
    exit();
    }
    
    ?>
    
    PHP:
    Might need editing or fixing.
     
    cono1616, Apr 2, 2010 IP
  5. Ethan8

    Ethan8 Peon

    Messages:
    200
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Fixing is not really a problem. I have a lot of problems to find the correct code and to build it 'clean'. Thanks for those replies, it will help me out!

    How can they delete it?
     
    Last edited: Apr 2, 2010
    Ethan8, Apr 2, 2010 IP
  6. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sessions basically store cookies on the computer of the visitor... they can just delete cookies.
     
    ThomasTwen, Apr 2, 2010 IP
  7. Ethan8

    Ethan8 Peon

    Messages:
    200
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I tried that, if they delete the cookies it won't affect anything.
     
    Ethan8, Apr 2, 2010 IP
  8. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #8
    That's because ThomasTwen was mistaken - $_SESSION and $_COOKIE are not the same :) The user could, theoretically, force a change of their SID to bypass but you're getting into the realms of unlikely. Just a note that, as the name implies, would allow them one access per session as opposed to one access for all eternity. That would require a database (SQL or text based). Finally, you should use require_once() rather than include(), firstly just as good practise (there are very few situations where a missing file should not throw a fatal error) but especially in this case as, were check.php not to be available for whatever warning, the user could then do whatever they wanted.

    Hope all that helps you :)
     
    Rory M, Apr 2, 2010 IP
  9. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You would have to use something that the user doesn't have access to SESSIONS or DATABASE are alright for this situation. Cookies aren't as all they have to do is clear their browser and they can revisit the site.
     
    Brandon.Add.On, Apr 3, 2010 IP