[英]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.