Jump to content

[SOLVED] Getting information for Session


kyleldi

Recommended Posts

Hi All,

 

I've got a user login form that forwards the user to a page based on their permissions.  This part of the form works fine, so I know it is passing the SESSION variable correctly.  What i'm trying to do though, is on the landing page, I want to says "Welcome, Username!.  Right now that information is stored in a table called firstname, and here is my query attempting to gather that information.  Any ideas what I have missed?  Thanks in advance for your help!

 

$colname_rs_user = "-1";
if (isset($_SESSION['firstname'])) {
  $colname_rs_user = $_SESSION['firstname'];
}
mysql_select_db($database_bmc, $bmc);
$query_rs_user = sprintf("SELECT * FROM admin_users WHERE firstname = %s", GetSQLValueString($colname_rs_user, "text"));
$rs_user = mysql_query($query_rs_user, $bmc) or die(mysql_error());
$row_rs_user = mysql_fetch_assoc($rs_user);
$totalRows_rs_user = mysql_num_rows($rs_user);

Link to comment
https://forums.phpfreaks.com/topic/104863-solved-getting-information-for-session/
Share on other sites

I've already started the session, this is merely continuing it on this page.

 

Here's the code:

 

//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

 

Here's the code for the entire query portion of the page.  The only additional PHP in this is the dynamic text echo displaying the results.

 

<?php require_once('../Connections/bmc.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

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;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);

  $logoutGoTo = "login.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "1,2,3,4";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_bmc, $bmc);
$query_rs_nav = "SELECT * FROM admin_navcategories";
$rs_nav = mysql_query($query_rs_nav, $bmc) or die(mysql_error());
$row_rs_nav = mysql_fetch_assoc($rs_nav);
$totalRows_rs_nav = mysql_num_rows($rs_nav);

$colname_rs_user = "-1";
if (isset($_SESSION['firstname'])) {
  $colname_rs_user = $_SESSION['firstname'];
}
mysql_select_db($database_bmc, $bmc);
$query_rs_user = sprintf("SELECT * FROM admin_users WHERE firstname = %s", GetSQLValueString($colname_rs_user, "text"));
$rs_user = mysql_query($query_rs_user, $bmc) or die(mysql_error());
$row_rs_user = mysql_fetch_assoc($rs_user);
$totalRows_rs_user = mysql_num_rows($rs_user);
?>

you wrote...

 

//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

 

which means if there is no session then start a session. Its meant to be...

 

//initialize the session
if (isset($_SESSION)) {
  session_start();
}

 

if there is a session continue it ^^^

If i'm not terminating the session (logging out), then how do I go about pulling the data from that session?  The user never types their name, but it's stored under the same table in which the login page validates the username/password and permissions that got them to that page in the first place.

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.