簡體   English   中英

為什么我的會話變量不會傳遞給其他頁面?

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

我有一個使用數據類型用戶登錄用戶的類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;
    }
}

登錄工作和session_starts()工作,但當我嘗試print 'Welcome, ' . $_SESSION['username']; print 'Welcome, ' . $_SESSION['username']; 在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>

您應該在當前頁面中設置會話以使用與會話相關的代碼

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