基本上我用我的PHP代码解决了一个未知问题。
CODE(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 [&#39; key&#39;]值为空?我一直在使用var_dump()来证明这一点。我希望我的会话密钥在我的数据库中具有用户ID的值,我发现我的代码没有问题。
答案 0 :(得分:1)
首先,$id
变量未定义。
执行$row1['id'] = $id;
表示assigning $id value to $row1['id']
如果您想在$id
中存储用户ID,那么您应该使用
$id = $row1['id'];
与$id = $_SESSION['key'];
相同的错误这意味着assiging $_SESSION['key'] to $id
。如果要在会话中存储$id
值:
$_SESSION['key'] = $id;
答案 1 :(得分:0)
我可以看到你的代码中有2个问题,你们都在运行循环来获取数据所以这里是代码,我标记了你正在做的行吗
<?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)){ //no need to run loop here
$row = mysql_fetch_array($queried);
$user_pass = $row['password'];
$veri_password = password_verify($password, $user_pass);
// } //remove this too
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)){//no need to run loop here
$row1 = mysql_fetch_array($entered_user);
//PROBLEM IS HERE
//$row1['id'] = $id; // you can't store values in $id like this
$id = $row1['id']; // right way to do
} //remove this too
//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']; //again wrong you cant load $id into session
$_SESSION['key'] = $id; //right way to load $id variable value into session
header('Location: profile.php');
exit();
}
}
}
?>
答案 2 :(得分:0)
始终包含
ini_set("display_errors", "On");
ERROR_REPORTING(E_ALL);
作为测试时代码中的前两行,或相应地更改测试环境php.ini。 这将为您提供来自php的所有可能的帮助/错误声明
其次,在打开Php-tag之前删除任何空格 使用session_start()时; 如果有空格,正如您的代码所示,php会表现不正常。
然后再看一下你的代码。 / niels ml