$ _SESSION ['firstname']每次返回空白

时间:2018-11-16 21:56:54

标签: php session

当我尝试使用$ _SESSION获得名字时,它返回空白。 这是我的home.php的PHP端:

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';


sec_session_start();


?>

<a href="#" title=""><?php echo $_SESSION['firstname'];?>

我不确定,但是我认为sec_session_start()函数存在问题:

function sec_session_start() {
$session_name = 'sec_session_id';  

// The Below is set in a document which is included in the 
// functions document: include_once 'psl-config.php' with 
// define("SECURE", TRUE); 
$secure = SECURE;

// This stops JavaScript being able to access the session id.
$httponly = true;

// Forces sessions to only use cookies.
if (ini_set('session.use_only_cookies', 1) === FALSE) {
    header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
    exit();
}

// Gets current cookies params.
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);

// Sets the session name to the one set above.
session_name($session_name);

session_start();            // Start the PHP session 
session_regenerate_id();    // regenerated the session, delete the old one. 
}

我不知道是什么问题。有人可以帮我吗:)

*错误报告:
 我启用了错误报告功能,现在就给我这个错误

  

注意:未定义的索引:第239行的/----------/home.php中的名字

我已修复此错误*

设置$ _SESSION ['firstname']

if (isset($_SESSION['user_id'])) {
$user_id = $_SESSION['user_id'];

if ($stmt = $mysqli->prepare("SELECT firstname FROM members WHERE id = ? LIMIT 1")) {
        $stmt->bind_param('i', $user_id);
        $stmt->execute();   
        $stmt->store_result();
        $stmt->bind_result($_SESSION['firstname']);
        $stmt->fetch();
            } else {
    // Not logged in 
    return false;
}

页面现在为白色,没有任何错误报告

数据库的一些图片:

1 个答案:

答案 0 :(得分:0)

我已经解决了该问题,在}脚本的末尾缺少了一个set $_SESSION