如何从会话ID获取值

时间:2017-06-24 15:41:40

标签: php mysql get session-variables

我有一个页面,我需要从会话中获取变量以使用变量执行sql查询。

  

(请原谅我使用mysql而不是mysqli,我将继续工作   改变它)

我试过这个$_SESSION["user_name"] = $cnid;它不起作用。

所以我尝试将变量$cnid$cnin添加到登录页面的网址中,并将其中任何一个添加到用户信息中心页面上,但它们似乎不会出现在url因此用户仪表板页面$_GET没有。如果我做错了什么,请有人告诉我!

以下是登录会话:

<?php
session_start();
include("server-side/ufunctions.php");
if(count($_POST)>0) {
$sqlses="SELECT * FROM consignments WHERE cn='" . mysql_real_escape_string($_POST['user_name']) . "'";
$resultses= mysql_query($sqlses);
$rwses= mysql_fetch_array($resultses);
$cnid = $rwses[1];    
$cnin = $rwses[0];    
$cnn = mysql_real_escape_string($_POST['user_name']);

    if( $cnn == $cnid ) {
        $_SESSION["user_id"] = 1001;
        $_SESSION["user_name"] = $cnid;
        $_SESSION['loggedin_time'] = time();  
    } else {
        //Login Error Msg
       $msge = "<i class=\"fa fa-exclamation-circle\"></i> Consignment Does Not Exist!";
       header("Location:?msge=$msge");
    }
}

if(isset($_SESSION["user_id"])) {
    if(!isLoginSessionExpired()) {
        //Login Success Msg
       $msg = "<i class=\"fa fa-check\"></i> Welcome $cnid! "; //This where i included the cnid variable to echo as a message for test to know if the link is getting the variables, yet no show!
       header("location:$url/server-side/consignment_detail?cnin=$cnin&msg=$msg"); //So included the cnin as a variable into the url still it didnt show!
    } else {
       header("Location:logout_user.php?session_expired=1");
    }
}

if(isset($_GET["session_expired"])) {
        //Login Error Msg
       $msge = "<i class=\"fa fa-exclamation-circle\"></i> You've Been Checked Out. Please Check Back In!";
       header("Location:?msge=$msge");
}
?>

以下是用户信息中心:

<?php 
session_start();
include '_inc/dbconn.php'; 
$sql="SELECT * FROM admin WHERE id='1'";
$result= mysql_query($sql);
$rws= mysql_fetch_array($result);
$url= $rws[7];

include("ufunctions.php");
if(isset($_SESSION["user_id"])) {
    if(isLoginSessionExpired()) {
        header("Location:logout_user?session_expired=1");
    }
}
?>

<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Consignment Details | User Dashboard</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

</head>
<body class="hold-transition skin-yellow layout-top-nav">
<?php        
            $_SESSION["user_name"] = $cnid;
            $sql1="SELECT * FROM consignments WHERE id='$cnid'";
            $result1= mysql_query($sql1);
            $rws1= mysql_fetch_array($result1);
            $cn= $rws1[1];
            $type= $rws1[2];
            $weight= $rws1[3];
            $qty= $rws1[4];
            $mode= $rws1[5];
            $pdate= $rws1[6];
            $ptime= $rws1[7];
            $pdt= "$pdate $ptime";
            $invoice= $rws1[8];
            $status= $rws1[9];
            $cpfn= $rws1[10];
            $cpln= $rws1[11];

            $dp= $rws1[12];
            if($rws1[12] == ""){
            $output = "<img class='profile-user-img img-responsive img-circle' src='default.png' alt='contact personnel'  width='200' height='200'>";
            }else{
            $output = "<img class='profile-user-img img-responsive img-circle' src='uploads/".$rws1[12]."' alt='contact personnel'>";
            }

            $email= $rws1[13];
            $cpphone= $rws1[14];
            $branch= $rws1[15];
            $sfn= $rws1[16];
            $sln= $rws1[17];
            $semail= $rws1[18];
            $sphone= $rws1[19];
            $saddress= $rws1[20];
            $rfn= $rws1[21];
            $rln= $rws1[22];
            $remail= $rws1[23];
            $rphone= $rws1[24];
            $raddress= $rws1[25];
            $cnd= $rws1[26];
            $lutd= $rws1[27];


            $sql2="SELECT * FROM `".$cn."status` ";
            $result2= mysql_query($sql2) or die(mysql_error());
?>

1 个答案:

答案 0 :(得分:0)

在“用户信息中心”页面中,不要先将会话值分配给变量,而只是直接使用它。

$_SESSION["user_name"] = $cnid;
$sql1="SELECT * FROM consignments WHERE id='$cnid'";

将其更改为:

$sql1="SELECT * FROM consignments WHERE id='". $_SESSION["user_name"]."'";

我希望现在这可以解决你的问题! :)