Jump to content

Recommended Posts

Set a flag when an admin logs in. On all admin only sections check for the flag

// login - check admin user
$_SESSION['adminuser'] = ($row->admin == 1) ? true : false;

// admin area
if($_SESSION['adminuser']) {

}

Note you must check for admin privileges when a user logs in to set a flag.

ok i obviously did this wrong...

 

<?php
session_start();

mysql_connect("localhost", "Master", "password");
mysql_select_db("Login");
$admin = mysql_query("SELECT * FROM `Users`") or die("Error");

if ($_SESSION['username']($admin['admin'] == 0)) {

 

how do i do, if the user is logged in check if the admin field is not equal to zero?

no,no,no. what you should be doing is setting a flag in a session when you authenticate the user i.e. login. You should not be setting the username in a session (not needed after login). set another flag to say that the user is logged in. i.e.

 

<?php
// login.php
if($_POST['action'] == 'login') {
  if(strlen(trim($_POST['username'])) && strlen(trim($_POST['password']))) {
     // check user
     $result = mysql_query("SELECT userId, admin FROM users WHERE username='".mysql_real_escape_string($_POST['username'])."' AND password='".mysql_real_escape_string($_POST['password'])."' LIMIT 1");
     if(mysql_num_rows($result)) {
       // user valid
       $row = mysql_fetch_assoc($result);
       $_SESSION['userId'] = $row['userId'];
       // is this an admin user
       $_SESSION['admin'] = ($row['admin'] == 1) ? true : false;
       // redirect
       header("Location:welcome.php");
       exit();
     }
  }
}
?>

 

Now on page that needs authentication i.e welcome.php

<?php
// welcome.php
// check user is authenticated
if(!is_numeric($_SESSION['userId'])) {
  header("Location:login.php");
  exit();
}

// is the user an admin?
if($_SESSION['admin']) {
  print "You are an admin";
}
else {
  print "You are a standard user";
}
?>

 

too complicated, don't know how to implement it into my code..

 

 

 

 

i want to check if the thats user logged in, has the admin field not equal to zero.. in the table, value 1 indicates your an admin, value 0 indicates your a normal user.. how do i check for this...

 

this was my attempt

<?php
session_start();

mysql_connect("localhost", "Master", "password");
mysql_select_db("Login");
$admin = mysql_query("SELECT * FROM `Users`") or die("Error");

if ($_SESSION['username']($admin['admin'] == 0)) {

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.