簡體   English   中英

如何從會話ID獲取值

[英]How to get a value from session id

我有一個頁面,我需要從會話中獲取變量以使用該變量執行sql查詢。

(請原諒我使用mysql而不是mysqli,我將繼續進行更改)

我已經試過$_SESSION["user_name"] = $cnid; 它不起作用。

因此,我嘗試將變量$cnid$cnin到登錄頁面的url中,並在用戶顯示板頁面上獲取它們中的任何一個,但它們似乎未顯示在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());
?>

在“用戶儀表板”頁面中,無需先將會話值分配給變量,只需直接使用它即可。

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

更改為:

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

我希望現在可以解決您的問題! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM