Jump to content

how to use sessions to give a user admin different access????


Recommended Posts

 

is this a good way of doing a simple admin different rights e.g  if secret word then go to adminpage.php

 

 

session_start();
$captcha = $_POST["captcha"] ;
$secretword = $_SESSION["secretword"] ;

if (strcmp( $captcha, $secretword )) {
  // it's a bot
}
else {
  // matched -- it's a human
}

You should store that information with the user's record in the database, such as in a field called `admin` with a boolean TRUE/FALSE value. Then check against that to determine if the user is an admin, and if so set a $_SESSION variable to that effect.

If you've already added the field to the DB table, while you're authenticating the user's login credentials, just check the field's value. Remember to have session_start(); as the first thing in a script that will use $_SESSION data.

 

$query = "SELECT username, password, admin FROM user WHERE username = $username AND password = $password";
$result = mysql_query($query); 
$array = mysql_fetch_assoc($result);
// take normal steps to check for errors, etc. then check the admin field's value

     $_SESSION['is_admin'] = $array['admin'];

 

Then all you have to do is check for the $_SESSION variable on page access.

<?php
session_start();
if( isset($_SESSION['is_admin']) && $_SESSION['is_admin'] === 1 ) {
     // user is authenticated as an admin
} else {
     //user isn't authenticated, so boot them out with a header() redirect, or display an error, or whatever you need to do.
}

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.