Jump to content

[SOLVED] How to allow only logged in users to a specific webpage


scarlson

Recommended Posts

I am wondering how I go about allowing those users who successfully login to be able to go to a certain page.  Those users who are not logged should be re-directed to login page.  I was able to get the user authentication to work fine on my site but not sure what I need to add to certain pages to check if a user is logged in.

 

Any help will be greatly appreciated.

 

Thanks,

 

Scott

Link to comment
Share on other sites

session_start();

if($_SESSION['sexy'] == 'you'){
// redirect to login
}else

if($_SESSION['sexy'] == 'me'){
// logged in
}

 

 

Sorry, pretty new to this.  What would ['sexy'] represent?  I think on my page that I do the authentication do I need to register the session with the username and password and then use it in the example above?

Link to comment
Share on other sites

Here is my code from the login page:

 

if($count==1){

// Register $myusername, $mypassword and redirect to file "ad_setup.php"

session_register("myusername");

session_register("mypassword");

header("location:ad_setup.php");

}

else {
echo "<div id='error_setup'>The account information does not exist, Try again!</div>";

 

So would I do something like this:

 

session_start();

if($_SESSION['username'] == 'not sure what to put here'){
// redirect to login
}else

if($_SESSION['username'] == 'not sure what to put here'){
// logged in
}

 

Link to comment
Share on other sites

Here is my code from the login page:

 

if($count==1){

// Register $myusername, $mypassword and redirect to file "ad_setup.php"

session_register("myusername");

session_register("mypassword");

header("location:ad_setup.php");

}

else {
echo "<div id='error_setup'>The account information does not exist, Try again!</div>";

 

So would I do something like this:

 

session_start();

if($_SESSION['username'] == 'not sure what to put here'){
// redirect to login
}else

if($_SESSION['username'] == 'not sure what to put here'){
// logged in
}

 

 

 

 

Login Page

 

<?php
session_start(); // GOES ON TOP OF EVERYTHING  "LINE 1"

    if(isset($_POST['submit button']))
    { // check if user has posted the form
          if((trim($_POST['username'] != ""))||
             (trim($_POST['password']!= "")))
              { // Check if user has filled in the fields
                 $row = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE username 
                        = ".mysql_real_escape_string(strip_tags($_POST['username']))." AND password    
                        = ".mysql_real_escape_string(strip_tags($_POST['password']))."");

            if(($row['username']==$_POST['username'])&&($row['password']==$_POST['password'])){

               $_SESSION['username'] = $row['username'];
                     
                        if($row['user_level']=="admin"){
                        $_SESSION['user_level'] = 2; // Make this a 1 for user, and 2 for admin
                        }
                       else
                       {
                       $_SESSION['user_level'] = 1; // Make this a 1 for user, and 2 for admin
                       }

                       header("location:ad_setup.php"); // Redirect user if loggin was successful

            }
   
else {

    echo 'Username/Password fields were incorrect.';          
} 
}
else 
{ 
echo "Empty Fields"; 
}
}

?>


/// FORM GOES HERE

 

 

Restricted Area

 

 

<?php
session_start();

if(isset($_SESSION['username'])){

// logged in


}
else
{

header("location:login.php"); // REDIRECT TO LOGIN PAGE

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.