简体   繁体   English

为什么我的会话变量不会传递给其他页面?

[英]Why aren't my session variables being passed to the other pages?

I have a class Authenticator that uses Datatype User to log in users. 我有一个使用数据类型用户登录用户的类Authenticator。

include('User.datatype.php');

$usher = new Authenticator;
$usher->checkCreds();
$usher->startSession();

Class Authenticator {
    protected $user;
    protected function getCreds() {
        if (!isset($_POST['login']))
                throw new Exception("There was an error processing your request", 1);
        else if ($_POST['username'] == '' || $_POST['password'] == '')
            throw new Exception("You must enter a username and password", 1);
        $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
        $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
        $this->user = new User;
        $this->user->username = $username;
        $this->user->password = $password;
    }

    public function checkCreds() {
        $this->getCreds();
        if (empty($this->user->username) || empty($this->user->password))
            throw new Exception("Error Processing Request", 1);
        include('dbconnect.php');       // Normally I'd store the db connect script outside of webroot
        $pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
        $stmt = $pdo->prepare('SELECT username FROM Users WHERE username = :uname AND password = :pword');
        $stmt->bindParam(':uname', $this->user->username);
        $stmt->bindParam(':pword', $this->user->password);
        $stmt->execute();
        $status = $stmt->fetch(PDO::FETCH_NUM);
        $this->user->status = $status;
    }

    protected function createSessionID() {
        $seshID = mt_rand(99999, 1000000000);
        return $seshID;
    }

    public function startSession() {
        if ($this->user->status === false)
            throw new Exception("There was a problem connecting to the database", 1);
        session_start();
        $_SESSION['username'] = $this->user->username;
        $_SESSION['id'] = $this->createSessionID();
        $secret = $_SESSION['id'];
        header('Location:loggedin.php?' . $secret);
        return true;
    }
}

The login works and session_starts() works, but when I try print 'Welcome, ' . $_SESSION['username']; 登录工作和session_starts()工作,但当我尝试print 'Welcome, ' . $_SESSION['username']; print 'Welcome, ' . $_SESSION['username']; on loggedin.php, the session variable is empty. 在loggedin.php上,会话变量为空。

HTML for loggedin.php loggedin.php的HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Product Cost Calculator</title>
            <link rel="stylesheet" type="text/css" href="style.css" />
        </head>

        <body>
            <div id="container">
                <?php
                    /*require_once ('Authenticator.php');
                    if (!Authenticator::startSession())
                        print 'you are not logged in';*/
                    print 'Welcome, ' . $_SESSION['username'];
                ?>
            </div>
        </body>
    </html>

You should set a session in the current page to make use of session related code 您应该在当前页面中设置会话以使用与会话相关的代码

session_start();
include('User.datatype.php');
//rest of code etc etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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