Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:33:06+00:00 2026-05-26T13:33:06+00:00

I’m currently learning PHP and MySQL and I have been working from a basic

  • 0

I’m currently learning PHP and MySQL and I have been working from a basic (albeit old) login script tutorial – I’ve come across various depreciated functions and such and I’m trying to improve on this script.

I would like to add different access levels to my script such as admins and users. I have added a row in my ‘users’ table called ‘access’ whereby access will be be ‘1’ for users and ‘9’ for admins.

After some initial research it seems I need to be able to store the users access level in a session variable – is this the right way to go about it? If so, how would I retrieve this initially, when the user logs in?

Once the access level is stored in the session variable, how will I then be able restrict access to pages – using header redirects perhaps?

This is the current code I’m working with from functions.php

function checkLogin()
    {
    /* Check if user has been remembered */
    if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
        $_SESSION['username'] = $_COOKIE['cookname'];
        $_SESSION['password'] = $_COOKIE['cookpass'];
    }

    /* Username and password have been set */
    if(isset($_SESSION['username']) && isset($_SESSION['password']))
    {
    /* Confirm that username and password are valid */
    if(confirmUser($_SESSION['username'], $_SESSION['password']) != 0)
    {
    /* Variables are incorrect, user not logged in */
        unset($_SESSION['username']);
        unset($_SESSION['password']);
        return false;
    }
        return true;
    }
    /* User not logged in */
    else
    {
      return false;
    }
    }

And from login.php

if(isset($_POST['sublogin'])){
   /* Check that all fields were typed in */
   if(!$_POST['user'] || !$_POST['pass']){
      $errors .= "You didn't fill in a required field.<br/>\n";

   }


else{

    /* Once all fields are entered - perform form validation */


   /* Checks that username is in database and password is correct */
   $md5pass = md5($_POST['pass']);
   $result = confirmUser($_POST['user'], $md5pass);

   /* Check error codes */
   if($result == 1){
      $user_errors .= "That username doesn't exist in our database.<br/>\n";
   }
   else if($result == 2){
      $pass_errors .= "Incorrect password, please try again.<br/>\n";
   }


   /* Username and password correct, register session variables */
   if (empty($errors) && empty($user_errors) && empty($pass_errors)){
        $_POST['user'] = mysql_real_escape_string($_POST['user']);
        $_SESSION['username'] = $_POST['user'];
        $_SESSION['password'] = $md5pass;
        /* Quick self-redirect to avoid resending data on refresh */
        echo "<META HTTP-EQUIV='refresh' CONTENT='0;URL=index.php'>";
   } 



   /**
    * This is the cool part: the user has requested that we remember that
    * and one to hold his md5 encrypted password. We set them both to
    * he's logged in, so we set two cookies. One to hold his username,
    * expire in 100 days. Now, next time he comes to our site, we will
    * log him in automatically.
    */
   if(isset($_POST['remember'])){
      setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
      setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
    }

    //return;
}
}

Any help would be really appreciated as I’ve been stuck on this for a few days now, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:33:07+00:00Added an answer on May 26, 2026 at 1:33 pm

    So this ex assumes that you are checking the limiting resources to roles in your PHP not from the DB. This can be easily changed, but i figured i’d make this ex a little simple.

    Schema:

    users
    -------
    id_user
    identity
    credential
    role
    ... other fields
    

    Account Creation

    don’t use md5, its easy to implement, and easily overcome. sha is just a litttle more secure with no additional work.

    $email_or_username = "francis@yaconiello.com";
    $password = "PinkEleph4nt"; // not my real password for anything
    $role = "admin";
    $sql = sprintf("INSERT INTO users SET identity='%s', credential='%s', role='%s'",
        mysql_real_escape_string($email_or_username), 
        mysql_real_escape_string(sha1($password)), 
        mysql_real_escape_string($role));
    

    Account Login

    This is shortened, i threw the query in, but not alot of the DB logic, write it as its needed, read the comments.

    // VALIDATE THE EMAIL/USERNAME and PASSWORD
    
    if($is_valid == TRUE)
    {
        $sql = sprintf("SELECT id_user FROM users WHERE identity='%s' AND credential='%s' LIMIT 1",
            mysql_real_escape_string($email_or_username), 
            mysql_real_escape_string(sha1($password)));
    
        // FETCH ROW save into $row
    
        if(!empty($row))
        {
            // A user was fetched save it into the session
            $_SESSION['id_user'] = $row['id_user'];
    
            // SUCCESS
        }
        else
        {
            // FAILURE
        }
    }
    

    Check user’s role

    <?php
    function fetch_role()
    {
        $role = "guest";
        if(isset($_SESSION['id_user']))
        {
            // User exists
            $sql = sprintf("SELECT * FROM users WHERE id_user='%s' LIMIT 1",
                mysql_real_escape_string($_SESSION['id_user']));
    
            // RUN THE MYSQL QUERY TO FETCH THE USER, SAVE INTO $row
    
            if(!empty($row))
            {
                $role = $user_row['role'];
            }
        }
    
        return $role;
    }
    
    ...
    
    $role = fetch_role();
    if($role == 'guest')
    {
        // SHOW GUEST CONTENT
    }
    elseif($role == 'member')
    {
        // SHOW OTHER CONTENT
    }
    elseif($role == 'admin')
    {
        // SHOW ADMIN CONTENT
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.