Jump to content

[SOLVED] Sessions problem


NathanLedet

Recommended Posts

I have inside a database with a column called isAdmin with a 1 or 0. 1 means they are an Admin and 0 means they are not.

 

When they log in, I store a session, $_SESSION['isAdmin']

 

which returns a 1 or a 0

 

Now, when i go to a specific page, I have this script that's not working properly:

session_start();
if ($_SESSION['isAdmin'] == 1){
	echo "isAdmin is 1<br />";
      //Admin options go here
}

 

When I'm logged out, and I go to the page, it seems to ignore the check to see if the session is 1 and displays the "isAdmin is 1" string.

Link to comment
https://forums.phpfreaks.com/topic/115666-solved-sessions-problem/
Share on other sites

can we see your logout function???

::)

 

silly me.  That reminded me to unset the session!

What's in bold is what I just added...I will test and ensure that was my issue, and will respond to let you know that was the issue.  Thanks!

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){

  //to fully log out a visitor we need to clear the session varialbles

  $_SESSION['MM_Username'] = NULL;

  $_SESSION['MM_UserGroup'] = NULL;

  $_SESSION['PrevUrl'] = NULL;

$_SESSION['isAdmin'] = NULL;

  unset($_SESSION['MM_Username']);

  unset($_SESSION['MM_UserGroup']);

  unset($_SESSION['PrevUrl']);

  unset($_SESSION['isAdmin']);

 

  $logoutGoTo = "index.php";

  if ($logoutGoTo) {

    header("Location: $logoutGoTo");

    exit;

  }

}

Just a suggestion for better buidling using sessions is to always define arrays in you session variables that pretain to what those variables are about

 

For example if you are storing User Login Data, Shopping Cart data and Site vars all in sessions you should organize your sessions vars to be like

<?php
$_SESSION['UserData']['UserID'] = "1";
$_SESSION['UserData']['Is_Admin'] = "1";
$_SESSION["UserData']['Username'] = "Joebob";

$_SESSION['CartData']['CartID'] = "654234";
$_SESSION['CartData']['CartName'] = "Cart 1";

$_SESSION["SiteVars']['Lang'] = "English";
$_SESSION['SiteVars']['Resolution'] = "800*600";
?>

 

then if a user logs out you simply can delete all the user data sessions by saying

<?php
$_SESSION['UserData'] = array();
?>

 

Or to delete the shopping cart

<?php
$_SESSION['CartData'] = array();
?>

 

I don't like to muddy my global level of my sessions because I've sometimes like to use the same name for session data pertaining to two different things and you don't realize you have problems later.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.