简体   繁体   中英

How to get a value from session id

I have a page where i need to get a variable from the session to perform an sql query with the variable.

(Please forgive my use of mysql instead of mysqli, i'll work on changing it)

I have tried this $_SESSION["user_name"] = $cnid; its not working.

So i tried to add the variable $cnid and $cnin into the url of the login page and get either of them on the user dashboard page but they don't seem to appear on the url hence the user dashboard page $_GET nothing. please someone should tell me if am doing something wrong!

Here is the login session:

<?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");
}
?>

Here is the User Dashboard:

<?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());
?>

In your User Dashboard page, Instead of first assigning the session value to a variable just use it directly.

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

Change this to:

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

I hope now this solves your problem! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM