drcb19700 2019-07-30 11:58
浏览 62
已采纳

在PHP $ _SESSIONS中没有得到任何东西

I've been trying to add a user login system to my site and have the following login page that seems to work fine (in so far as it checks username and password, passes the user through):

index.php

<html>    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Login Form</title>
    </head>

    <body>
        <form id="form1" name="form1" method="post" action="login.php">
            <table width="510" border="0" align="center">
                <tr>
                    <td colspan="2">Login Form</td>
                </tr>
                <tr>
                    <td>Email Address:</td>
                    <td><input type="text" name="email" id="email" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="password" id="password" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="button" id="button" value="Submit" /></td>
                </tr>
                <tr>
                    <td colspa"2"><?php if(isset($_GET['f'])){echo("<h2>Login Failed</h2>");} ?></td>
                </tr>
            </table>
        </form>
    </body>
</html>

login.php

<?php
    session_start();
    $email = $_POST['email'];
    $password = $_POST['password'];

    include 'includes/db_connect.php';
    $conn = new PDO($dsn, $user, $pass);

    $query = "SELECT password, salt FROM member WHERE email = :email";

    $result = $conn->prepare($query);
    $result->bindParam(":email", $email);
    $result->execute();

    $number_of_rows = $result->rowCount();

    if($number_of_rows == 0) // User not found. So, redirect to login_form again.
    {
        header('Location: index.php?f=1');
    }

    $userData = $result->fetch(PDO::FETCH_ASSOC);

    $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );

    if($hash != $userData['password']) // Incorrect password. So, redirect to login_form again.
    {
        header('Location: index.php?f=1');
    }else{ // Redirect to home page after successful login.
        session_regenerate_id();
        $_SESSION['email'] = $email;
        header('Location: results.php');
    }
?>

results.php

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Results</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="assets/css/style.css" />
    <?php
        //are you logged in?
        session_start();
        if(isset($_SESSION['email'])){
            $email = $_SESSION['email'];
        }
        if(empty($email)){
            echo "You're not an authorized user. Please <a href='./index.php'>login</a>.<br />";
            exit();
        }
//rest of website

However, I always get the "You're not an authorized user..." error, I've tried putting a var_dump() and var_export() for $_SESSION but get NULL and print_r produces nothing, as does echo $_SESSION['email'] which would suggest my $_SESSION global is empty, I have session_start(); at the start of both login.php and results.php so the session should be there and continue.

I have things like WordPress installed on the same server so pretty sure things like $_SESSION works on the host and PHP is 7.2 so should be up to snuff too.

What dumb mistake am I overlooking?

  • 写回答

1条回答 默认 最新

  • dongzhang2150 2019-07-30 12:30
    关注

    In results.php, you should have the session_start() at the top of your code before any HTML output.

    <?php 
    session_start();
    ?>
    <html>
    ... rest of code
    

    From the docs:

    To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

    If you have error_reporting set to show E_WARNING, you should see the warning "Cannot send session cookie - headers already sent..."

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀