restricting access to a webpage

Discussion in 'PHP' started by newbie12345, May 10, 2010.

  1. #1
    does any one no how can i restrict access to a particular page

    i created this session after the user is logged in
    	   $_SESSION['username'] = $user;
    PHP:
    thank you
     
    newbie12345, May 10, 2010 IP
  2. CoreyPeerFly

    CoreyPeerFly Notable Member Affiliate Manager

    Messages:
    394
    Likes Received:
    24
    Best Answers:
    5
    Trophy Points:
    240
    #2
    Who do you not want to access the page? If you only want logged in users to have access to it, add this to beginning of the page:
    <?php
    	session_start();
    	if(!isset($_SESSION['username']))
    	{
    		die("You do not have permission to access to this page.");
    	}
    ?>
    PHP:
     
    CoreyPeerFly, May 10, 2010 IP
  3. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #3
    mfscripts, May 11, 2010 IP
  4. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The best idea is to set a column or status, or access in the database. Admin, Moderator, User, ....

    Once user logs in, set $_SESSION['status']=$status and use this. This way, you can set certain users that can access certain pages.
     
    Erind, May 11, 2010 IP
  5. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or, if you wished, you could replace the Die with a headers send to redirect the user to a login box :)
     
    Rory M, May 11, 2010 IP
  6. live.co.uk

    live.co.uk Banned

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    use $_SESSION['loged'] = false;
     
    live.co.uk, May 11, 2010 IP
  7. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    Here is a quick code for you.

    
    <?php
    if($_COOKIE['site_cookie']=='true')
     {
       //Page Content
       exit;
      } 
    else
     {
     if(isset($_POST['submitted']))
      {
       setcookie('site_cookie','true',time()+60*60*24);
      }
    else
      {
      }
    }
    <html>
    <head>
    <title>Restricted Page</title>
    </head>
    <body>
    <form action="page.php" method="post">
    <b>Page Is Restricted</b><br />
    Password: <input type="password" name="password" />
    <input type="hidden" name="submitted" value="true" />
    <input type="submit" value="Enter Page!" />
    </form>
    </body>
    </html>
    
    Code (markup):
    However cookies are insecure and I would advise you if you are keeping really secret information in here and/or the page is easy to find than use sessions and a more complex system.
     
    Pudge1, May 11, 2010 IP