会话变量问题

时间:2018-07-14 19:09:32

标签: php session caching session-variables

我有一个到目前为止运行良好的php5.6应用程序。根据已登录的user_id会话获取的查询将显示另一个已登录的用户数据。 下面是我的代码: login.php

<?php require_once("functions.php");
if (isset($_POST['submit'])){   
$user_id = $_POST['user_id'];
$pwd = $_POST['pwd'];

$sql = "SELECT manguserid, mangpwd,passport, amount,home_addr, previous_schl, religion, gender, dob,state_origin, child_nationality,email, phone, alt_phone, surname, first_name, other_name, username, password, curr_year,curr_arm,year_of_grad,stud_status, a.stud_id as 'stud_id' FROM student_profile as a,  students_login as b WHERE `username` like '%$user_id%' AND username NOT LIKE '%BLOCKED%' AND `password`='$pwd' and a.stud_id=b.stud_id";

    //Establish data connection     
    $query = mysql_query($sql, $conn) or die(mysql_error());
    $row = mysql_fetch_assoc($query);
    $total = mysql_num_rows($query);
if($total>0){
    $_SESSION['stud_id'] = $row['stud_id'];

                echo "<script language='Javascript'>
                    window.location=\"home.php\"
                </script>";
} ?>

我的home.php是一个仪表板,并且在其中启动了session_start()

然后我有一个页面report.php,如下所示:

<?php require_once("functions.php");
$stud_id = $_SESSION['stud_id'];
$temp = explode(",", $_GET['u']);
$term = $temp[1];
$ss = $temp[0 ];

 if($ss == $_SESSION['ss']){
$table = 'student_profile';
 }

else{
$table = 'student_profile_'.str_replace('/','_',$ss);
}



mysql_select_db($database_conn, $conn);

$query_stud = "SELECT a.stud_id, surname, first_name, other_name, curr_year, curr_arm, stud_status, curr_session, passport FROM ".$table." as a WHERE a.stud_id='$stud_id'";

$stud = mysql_query($query_stud, $conn) or die(mysql_error());

$row_stud = mysql_fetch_assoc($stud);

$totalRows_stud = mysql_num_rows($stud);

$a = $row_stud['curr_year'];
$b = $row_stud['curr_arm'];

?>

我的functions.php如下:

<?php require_once('Connections/conn.php');

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

$logoutAction = "";

function logOut(){
    global $logoutAction;
    // ** 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['user_id'] = NULL;
      $_SESSION['MM_UserGroup'] = NULL;
      $_SESSION['PrevUrl'] = NULL;
      $_SESSION['stud_id'] = NULL;
      unset($_SESSION['user_id']);
      unset($_SESSION['MM_UserGroup']);
      unset($_SESSION['PrevUrl']);
      unset($_SESSION['stud_id']);

      $logoutGoTo = "login.php";
      if ($logoutGoTo) {
        header("Location: $logoutGoTo");
        exit;
      }
    }
}

function restrictUser(){
    //log out any user that attemps not to login
    //
    $MM_authorizedUsers = "";
    $MM_donotCheckaccess = "true";
    $_SESSION['MM_UserGroup'] = NULL;

    // *** 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 == "") && true) { 
          $isValid = true; 
        } 
      } 
      return $isValid; 
    }

    $MM_restrictGoTo = "login.php";
    if (!((isset($_SESSION['user_id'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['user_id'], $_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;
    }
}//restric user ends

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $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;
}
}
?>

最后我的conn.php如下所示:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"

global $hostname_conn;
global $database_conn;
global $username_conn;
global $password_conn;
global $conn;

$hostname_conn = "localhost";
$database_conn = "dbname";
$username_conn = "dbusername";
$password_conn = "password";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

就像我说的那样,用户在report.php中看到的属于另一个正在主动登录的用户的不同报告,而无需使用同一台计算机或浏览器。

1 个答案:

答案 0 :(得分:0)

我能够通过创建目录以将会话存储在根目录中来解决此问题。 然后,我在代码中添加了以下内容:

ini_set("session.gc_maxlifetime","3600"); // 1 hours
ini_set("session.save_path", $_SERVER['DOCUMENT_ROOT']."/mysession");
session_start();

那解决了我的问题,我的会议像以前一样正常进行。显然,这条路是由于上帝知道的原因而消失的。