无法从PHP中的$ _SESSION中检索数据

时间:2017-02-20 08:48:58

标签: php session

您好我是PHP语言的初学者。我发现很难从PHP中的会话中获取/显示数据。我的登录页面工作正常,会话也正在正确地传送到用户页面。但我没有数据也没有错误。

以下是我正常工作的登录页面脚本。我没有发布带有html元素的页面,因为我在下面的脚本上使用了require_once并且它工作正常。

<?php  
session_start();
require_once 'connection.php';

$err = array();
$errcnt = 0;

if (!empty($_POST)) {
    if (empty($_POST['usermail'])) {
        $err['verifyerr'] = "Please insert email";
        $errcnt++; 
    }
    if (empty($_POST['password'])) {
        $err['passerr'] = "Please insert password";
        $errcnt++;
    }
    if (!empty($_POST['usermail'])) {
        $userquery = "SELECT * FROM studentdetails WHERE email = '".$_POST['usermail']."'";
        $userlookup = mysqli_query($connect, $userquery);
        $userfetch = mysqli_fetch_assoc($userlookup);

        if (empty($userfetch)) {
            $err['matcherr'] = "Invalid username";
            $errcnt++;
        }
        else {
            $passquery = "SELECT * FROM studentdetails WHERE email = '".$_POST['usermail']."' AND entrykey = '".md5($_POST['password'])."'";
            $passlookup = mysqli_query($connect, $passquery);
            $passfetch = mysqli_fetch_assoc($passlookup);

            if (empty($passfetch)) {
                $err['invalidpass'] = "Invalid password";
                $errcnt++;
            }
            else {
                $_SESSION['uid'] = $_POST['usermail'];
                header("Location: http://localhost/Examination/user.php");
            }
        }
    }
}
?>

以下是user.php页面,我遇到了实际问题。

<?php 
session_start();
require_once 'connection.php';

if (isset($_SESSION['uid'])) {


    echo $_SESSION['uid']  . '<br>';

    $query = "SELECT * FROM studentdetails WHERE id='".$_SESSION['uid']."'";
    $result = mysqli_query($connect, $query);
    if (!$result) {
        die("Database query failed");
    }
    else {
        echo "All ok!";
    }
}
else {
    echo "not connected";
}


    //$queryoutput = "SELECT * FROM studentdetails WHERE id='".$_SESSION['uid']."'";
    //$outquery = mysqli_connect($connect, $queryoutput);

    //if (!isset($_SESSION['uid'])) {
    //header("Location: http://localhost/Examination/edulogin.php");
    //}

?>


<!DOCTYPE html>
<html>
<head>
    <title>User Profile</title>
</head>
<body>
<h1>Welcome</h1>
    <?php  
    while ($row = mysqli_fetch_row($result)) {
        var_dump($row);
        echo "<hr />";
    }
    ?>


    <!--
    <h1></h1>
    <table>
        <tr>
            <td><h4>Firstname</h4></td>

        </tr>
        <tr>
            <td><h4>Lastname</h4></td>

        </tr>
        <tr>
            <td><h4>Gender</h4></td>
        </tr>
        <tr>
            <td><h4>Email</h4></td>
        </tr>       
        <tr>
            <td><h4>About You</h4></td>
        </tr>
        <tr>
            <td><h4>Country</h4></td>
        </tr>
        <tr>
            <td><h4>Interests</h4></td>
        </tr>
        <tr>
            <td><h4>Image</h4></td>
        </tr>

    </table>

    <h3>Click here to <a href="http://localhost/Examination/userout.php">Logout</a></h3>
    -->
</body>
</html>

它甚至可以正确地回应会话,它会显示用户的电子邮件并回显“一切正常”。但奇怪的是没有数据被提取,因为我说没有错误。如果你知道,请告诉我一个解决方案。我将非常感激。

如果你想看看我的connection.php页面,这里是

<?php

define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'user_2');
$connect = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

if (mysqli_connect_errno()) {
    die("Connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")"); 
}
?>  

2 个答案:

答案 0 :(得分:2)

因为您使用电子邮件地址设置$_SESSION['uid'],然后尝试匹配id=email

所在的数据库记录
$query = "SELECT * FROM studentdetails WHERE id='".$_SESSION['uid']."'";

如果您要分配电子邮件地址,则应尝试将选择查询更改为(类似):

$query = "SELECT * FROM studentdetails WHERE email='".$_SESSION['uid']."'";

或者分配用户ID($_SESSION['uid'] = $userfetch['id']),然后查询:

$query = "SELECT * FROM studentdetails WHERE id='".$_SESSION['uid']."'";

您看到"All OK",因为没有错误,只是没有匹配的记录(因为查询可能不正确)。

......当然,请确保您保护自己免受注射!

答案 1 :(得分:-2)

首先,您需要在$_SESSION数组中分配数据。你已经在

做了这个
$_SESSION['uid'] = $_POST['usermail'];  

在您的登录页面脚本中。 还要检查网址。两者(当前和请求一个)是否在同一协议上开放,即http://