gpt4 book ai didi

php - session 未保存为 id

转载 作者:行者123 更新时间:2023-11-29 12:05:50 25 4
gpt4 key购买 nike

基本上,我一直在为我的 PHP 代码的未知问题而烦恼。

代码(login_config.php):

    <?php   
session_start(); //POST VARIABLES
$submit = $_POST['login_submit'];
$password = $_POST['login_password'];
$email = $_POST['login_email'];



if(isset($submit)){
require 'password_config.php';
require 'connect.php';

//PASSWORD VERIFYING
$pass_query = "SELECT password FROM users WHERE email='$email'";
$queried = mysql_query($pass_query);
while($row = mysql_fetch_array($queried)){
$user_pass = $row['password'];
$veri_password = password_verify($password, $user_pass);
}
if($veri_password != true){$errors[] = '-Account does not exist ';}
elseif($veri_password = true){

//CHECKING NUM ROWS
$sql = "SELECT id, username FROM users WHERE email='$email'";
$entered_user = mysql_query($sql);
$num_rows = mysql_num_rows($entered_user);
//STORING ID AS SESSION
while($row1 = mysql_fetch_array($entered_user)){
//PROBLEM IS HERE
$row1['id'] = $id;
}

//ERRS ARRAY ESTABLISHED
$errors = array();

//FURTHER VERIFYING
if( empty($password) || empty($email) )
{
$errors[] = 'Please do not leave fields empty';
}
elseif( $num_rows != 1 )
{
$errors[] = '-Account does not exist ';
}
elseif( $num_rows == 1 )
{
$id = $_SESSION['key'];

header('Location: profile.php');
exit();
}
}

}
?>

问题:为什么当我通过 header() 重定向到 profile.php 时,$_SESSION['key'] 值为空?我一直在使用 var_dump() 来证明这一点。我希望我的 session key 具有数据库中用户 ID 的值,并且我发现我的代码没有问题。

最佳答案

首先,$id 变量在任何地方都未定义

执行$row1['id'] = $id;意味着将$id值分配给$row1['id']如果你想将用户 ID 存储在 $id 中,那么你应该使用

$id = $row1['id'];

$id = $_SESSION['key']; 相同的错误这意味着将$_SESSION['key']分配给$id。如果您想在 session 中存储 $id 值:

$_SESSION['key'] = $id;

关于php - session 未保存为 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com