How to restrict access to a page

Discussion in 'Programming' started by Hayze, Oct 5, 2007.

Remove these ads by signing in
Remove these ads by signing in
Thread Status:
Not open for further replies.
  1. Hayze New Member

    Member Since:
    Sep 6, 2007
    Message Count:
    949
    Likes Received:
    0
    Occupation:
    Un Occupied.
    Location:
    To your left... no your other left.
    This tutorial assumes that you have a variable, let's call it $admin, defined for a certain IP address or another way of identifying the veriable. For the quick way, here is how you would do that:

    PHP:
    $admin = $SERVER[REMOTE_ADDR];
    Once you have added that code, you move on to dsiplaying the link, which would look something like this:

    PHP:
    <a href="index.php">Index</a>
    <a href="link.php">Link</a>
    <?php
    if $admin = 12.145.23.19
    //here is your IP address, I just made one up for the purpose of this tutorial
    { ?><a href="protectedlink.php">Only 12.145.23.19 can see this</a> <?php } ?>
    <a href="morelinks.php">More links here</a>
    This will make that link only show up to that IP address. But, if you are a smart web designer, you know that your restriced page can then be accessed by typing in www.domain.com/protectedlink.php and the hacker will get to it fine. To prevent this, put this code into protectedlink.php:

    PHP:
    <?php
    //get the variable $admin working
    require_once("filewithvariable.php");
    if $admin = 12.145.23.19 {
    //you would code your site into this area
    }
    else {
    echo "You are not allowed to access this page.";
    }
    Now, this will not work all of the time, the hacker can mask his IP address to be 12.145.23.19, but this is the first step to protecting your site. Further actions would have to be taken with password protecting, using either Javascript or PHP. This will work fine for a site that will not be experiencing any professional hacking attempts. Hope this helped :)
  2. Eureka New Member

    Member Since:
    Dec 16, 2007
    Message Count:
    17
    Likes Received:
    0
    Simpler one is just:

    PHP:

    <?php
    if ($open){
    // your page here
    }
    else {
    // refuse access here
    }
    ?>
     
    Before require, define $open.
  3. Edwin New Member

    Member Since:
    Oct 7, 2007
    Message Count:
    417
    Likes Received:
    0
    Occupation:
    Now that would be telling
    Location:
    UK
    I presume $open would be a boolean depending on your IP?
  4. Eureka New Member

    Member Since:
    Dec 16, 2007
    Message Count:
    17
    Likes Received:
    0
    No, it isn't be an IP; however, it is a boolean. It doesn't make too much sense to incorporate in IPs because if you're on a router (wireless) it changes (or at least the last digit). I never actually get what kind of site you would make to only allow yourself to view it. And it's in PHP so it's not like people will get the source script anyways. Aside from that, I'd just set a boolean or a log in system if the page is only for me.
Thread Status:
Not open for further replies.

Share This Page