会话值未传递到下一页

时间:2017-10-08 05:31:28

标签: php session session-variables

<?php
session_start();
ob_start();
include '../config.php';
if (isset($_POST['email']) && isset($_POST['password'])) {
$username = $_POST['email'];
$password = $_POST['password'];`enter code here`
$username = mysqli_real_escape_string($conn , $username);
$password = mysqli_real_escape_string($conn , $password);
$password_md5 = md5($password);
$query = "select * from admin where `email` = '$username' AND `password` = '$password_md5'";
$result = mysqli_query($conn , $query);
$row = mysqli_fetch_array($result , MYSQLI_NUM); 
$rows = mysqli_num_rows($result);
if ($rows == 1) {
$_SESSION["id"] = $row['id'];
$_SESSION["role_id"] = $row['role_id'];
//$_SESSION["role"] = $row['role'];
$_SESSION["email"] = $row['email'];
//$_SESSION['admin'] = "admin";
header("Location: dashboard.php?page=dashboard");
}else{
$fmsg = "invalid username and password";
}
}
?>



<?php
$login_session = $_SESSION['id'];
$query = "select * from admin where id = '$login_session'";
$result = mysqli_query($conn , "$query");
$row = mysqli_fetch_array($result , MYSQLI_NUM);
?>

我正在尝试使用session登录。我已成功登录但会话值未传递到下一页。我已经尝试了很多时间可以帮助解决这个问题..

1 个答案:

答案 0 :(得分:1)

使用mysqli_fetch_array获取数据时遇到问题。正如您指定MYSQLI_NUM一样,它将返回数字数组。您想要关联数组因为您正在使用其密钥

$row = mysqli_fetch_array($result , MYSQLI_ASSOC);  //<----change here
$rows = mysqli_num_rows($result);
if ($rows == 1) {
$_SESSION["id"] = $row['id'];
$_SESSION["role_id"] = $row['role_id'];
//$_SESSION["role"] = $row['role'];
$_SESSION["email"] = $row['email'];
//$_SESSION['admin'] = "admin";
header("Location: dashboard.php?page=dashboard");
}else{
$fmsg = "invalid username and password";
}