簡體   English   中英

$ _SESSION沒有在頁面php之間傳遞值

[英]$_SESSION is not passing values between pages php

我有3個檔案。 (在login.php )( loginFunctions.phplogin.phpindex.php ),我獲取這些值並將其傳遞給loginFunction.php以檢查這些值,然后從准備好的語句中選擇userID並將其返回給login.php 之后, login.php調用一個名為(get('value'))來傳遞userID並返回用戶信息。 我從登錄頁面和索引頁面開始了會話。 我試圖回顯已存儲在會話中的信息,但未返回任何內容。

我的代碼:

loginFunction.php

<?php

function absolute_url($page = 'index.php') {
    //header('Location: http:\\localhost');
    //exit(); //terminates the script

    $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    $url = rtrim($url, '/\\');
    $url .= '/' . $page;

    return $url;
}

function checkLogin($email = '', $password = '') {
    $errors = array();

    if (empty($email)){
        $errors[] = 'You must enter your email';
    }
    if (empty($password)){
        $errors[] = 'You must enter a password';
    }
    if (empty($errors)) {
////set up database econnection
        require_once 'DO_Classes/mysqli_connect.php';

        $db = new Database();
        $dbc = $db->getConnection();

        $stmt = $dbc->prepare("SELECT user_ID FROM User WHERE user_email=? AND AES_DECRYPT(user_password, 'p0ly')=?");

        if ($stmt) {
            $stmt->bind_param('ss', $email, $password);

            if ($stmt->execute()) {
                $stmt->store_result();
                $stmt->bind_result($user_ID);
                $stmt->fetch();
                $stmt->close();
                if(!empty($user_ID)){
                    return array(true, $user_ID);
                }else{
                    /*
                     * <div class=\"alert alert-success alert-dismissable\">
                                <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>
                                Invalid email or password
                            </div>
                     */
                    $errors[] = '<div class="alert alert-danger alert-dismissable">
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                <p align="center">Invalid email or password</p>
                            </div>';
                }

            } else {
                $errors[] = 'Passwords do not match';
            }
        }else {
            echo '<p class="error"> Oh dear. There was a databse error</p>';
            echo '<p class = "error">' . mysqli_error($stmt) . '</p>';
        }
    }
    return array(false, $errors);
}

?>

login.php

<?php
if (isset($_POST['submitted'])) {
    //require_once is similar to 'include' but ensures the code is not copied multiple times
    require_once('Functions/loginFunctions.php');

    //list() is a way of assigning multiple values at the same time
    //checkLogin() function returns an array so list here assigns the values in the array to $check and $data 
    list($check, $data) = checkLogin($_POST['email'], $_POST['password']);


    if ($check) {
        //setcookie('FName', $data['FName'], time()+ 900 ) ;  //cookie expires after 15 mins
        //setcookie('LName', $data['LName'], time() + 900 ) ;  
        session_start();
        require_once 'Classes/DO_Users.php';
        $user = new DO_User();
        $user->get($data);
        //use session variables instead of cookies
        //these variables should now be available to all pages in the application as long as the users session exists 
        $_SESSION['userID'] = $user->userID;
        $_SESSION['userType'] = $user->userTypeID;
        $_SESSION['last_activity'] = time(); //your last activity was now, having logged in.
        $_SESSION['expire_time'] = 60 * 5; //expire time in seconds: three hours (you must change this)
        //to enable $_SESSION array to be populated we always need to call start_session() - this is done in header.php
        //print_r is will print out the contents of an array
        //print_r($_SESSION);  
        //
        //Redirect to another page

        $url = absolute_url('index.php');  //function defined in Loginfunctions.php to give absolute path for required page
        //this version of the header function is used to redirect to another page
        header("Location: $url"); //since we have entered correct login details we are now being directed to the home page
        exit();
    } else {
        $errors = $data;
    }
}


if (!empty($errors)) {
    //foreach is a simplified version of the 'for' loop
    foreach ($errors as $err) {
        echo "$err <br />";
    }

    echo '</p>';
}

//display the form
?> 

index.php ,我將require_once 'header.php';稱為require_once 'header.php'; 具有session_start(); 但是什么也沒回來,為什么呢?

我發現了問題,這是因為准備好的聲明。 准備好的語句不起作用,因為准備好的語句的聲明缺少值。

暫無
暫無

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

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