簡體   English   中英

嘗試使用PHP設置Cookie,但未設置

[英]Trying to set cookies with PHP but aren't setting

好吧,所以我有一個名為authentication的php類,如果該人成功登錄,它會在login方法中設置一些cookie,然后使用同一方法login時,我的腳本將使用php的header將用戶轉發到相應的url。 但是由於某些原因,未設置cookie。

這是類authentication

<?php

require '../../config.php';
require base .'lib/helpers/bao.php';

class authentication extends bao {

    /*
        Login
    */
    public function login( $email, $pass, $fUrl = null ) {

        // set the forwarding url
        $forward = '/login.php';

        // check for empty values
        if( !empty( $email ) && !empty( $pass ) ) {

            // set params for binding and run query
            $params = array( ':email' => $email );
            $runQuery = parent::query( "SELECT * FROM users WHERE email = :email", $params );

            // check to see if an account was found
            if( parent::numRows( $runQuery ) > 0 ) {

                // place result into array for use
                $result = parent::fetch( $runQuery );

                // check to see if passwords match
                if( $result['pword'] === sha1( $result['salt'] . $pass ) ) {

                    // update login in time in the database
                    $params = array(':login' => expLogin, ':id' => $result['id'] );
                    $updateLoggedin = parent::query( "UPDATE users SET login = :login WHERE id = :id", $params );

                    // Set a bunch of cookies
                    setcookie("userId", $result['id'], expLogin, '/');
                    $emailCookie = setcookie("userEmail", $result['email'], expLogin, '/');
                    setcookie("userNotifications", base64_encode( $result['email'] ), expLogin, '/');
                    setcookie("userType", $result['type'], expLogin, '/');
                    setcookie("userStatus", $result['status'], expLogin, '/');
                    setcookie("userLogin", $result['login'], expLogin, '/');

                    // set forwarding url
                    switch( $result['type'] ) {
                        case 'obl':
                            $forward = $fUrl ? $fUrl : '/admin/';
                        break;

                        case 'broker':
                            $forward = $fUrl ? $fUrl : '/admin/';
                        break;

                        case 'borrower':
                            $forward = $fUrl ? $fUrl : '/admin/';
                        break;
                    }

                    $processMeta = 'Congrats you are logged in!';

                } else {

                    $processMeta = 'Your passwords did not match, please try again!';

                }

            } else {

                $processMeta = 'I\'m sorry we could not locate your account';

            }

        } else {
            $processMeta = 'You did not enter one of the required fields please try again!';
        }

        // set process message to session
        $_SESSION['processMeta'] = $processMeta;

        // forward onto next url
        header("Location: ". $forward );

    }

}

?>

現在,我已經多次使用這種方法,但是唯一的區別是,在我對程序進行程序編程之前,現在我已經將相同的代碼放入了一個類中。 在大多數情況下,使用相同的語法將類放入類后,cookie設置不正確。 任何幫助將不勝感激!

謝謝

我看到您使用expLogin作為cookie的過期時間。 如果為常數,則應驗證其值(秒)。 如果未定義expLogin(可能是$ expLogin的錯字?),則php會認為它是字符串,並且在轉換為整數時,其值為0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM