声明php时会话未初始化

时间:2015-03-09 14:12:58

标签: php session mysqli session-variables session-state

我的代码

$password = md5($password);
$sql2="SELECT uid from users WHERE uemail='$emailusername' or uname='$emailusername' and upass='$password'";

//checking if the username is available in the table
 $result = mysqli_query($this->db,$sql2);
$user_data = mysqli_fetch_array($result);
$count_row = $result->num_rows;
if ($count_row == 1) {
    $_SESSION['login'] = true; 
    // this login var will use for the session thing 
    $_SESSION['uid'] = $user_data['uid'];
    $_SESSION['fullname'] = $user_data['fullname'];
    return true;
}else{
    return false;
}

从上面的代码中可以看出,它首先检查是否匹配uemail或uname,其中密码等于$ password,然后如果找到记录,则会话为true,创建会话uid和会话全名但似乎session fullname未初始化,因为我也试图回显会话全名(假设我成功匹配记录)并且它给我一个错误,就像它没有被声明。我的代码中的问题是什么?任何帮助将不胜感激。谢谢!

PS:假设我已将$ _POST [' emailusername']存储到$ emailusername变量中,并假设我已经通过在头部添加session_start()来启动会话声明。

1 个答案:

答案 0 :(得分:0)

您应该在每个页面的顶部添加此行以初始化会话:

session_start();

PHP doc:http://php.net/manual/en/function.session-start.php