会话未保存为id
basically I have been pulling my hair out over an unknown problem with my PHP code.
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();
}
}
}
?>
Question: Why is it that, when I get redirected to profile.php via header(), $_SESSION['key'] value is null? I have been using var_dump() to prove this. I want my session key to have a value of the user's id in my database and I see no problems with my code.
基本上我用PHP代码解决了一个未知问题。 p> \ n
CODE( 问题:当我被重定向时,为什么会这样? 通过header()来分析profile.php,$ _SESSION ['key']值是否为空? 我一直在使用var_dump()来证明这一点。 我希望我的会话密钥在我的数据库中具有用户id的值,我发现我的代码没有问题。 p>
div> login_config.php code>): p>
&lt;?php
session_start(); // POST VARIABLES
$ submit = $ _POST ['login_submit'];
$ password = $ _POST ['login_password'];
$ email = $ _POST ['login_email'];
\ nif(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 [] =' - 帐户不存在';}
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);
// 存储ID作为会话
而($ row1 = mysql_fetch_array($ entered_user)){
//问题在这里
$ row1 ['id'] = $ id;
}
// ERRS ARRAY E STABLISHED
$ errors = array();
//进一步验证
if(空($ password)|| 空($ email))
{
$ errors [] ='请不要将字段留空';
}
elseif($ num_rows!= 1)
{
$ errors [] =' - 帐户不存在';
}
elseif($ num_rows == 1)
{
$ id = $ _SESSION ['key'];
标题('Location:profile.php');
exit();
}
}
}
?&gt;
code> pre>
First of all $id
variable is not defined anywhere.
Doing $row1['id'] = $id;
means assigning $id value to $row1['id']
If you want to store user id in $id
then you should use
$id = $row1['id'];
The same bug with $id = $_SESSION['key'];
This means assiging $_SESSION['key'] to $id
. If you want to store $id
value in a session:
$_SESSION['key'] = $id;
There are 2 problems in your code as far i can see, both are you are running loop to fetch data so here is the code and i marked the lines where you are doing wring
<?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();
}
}
}
?>
Always include
ini_set("display_errors", "On");
ERROR_REPORTING(E_ALL);
As the first two lines in your code while testing, or change your test environment php.ini accordingly. This will give you all possible help/error statements from php
Second, remove any white space before the opening Php-tag When using session_start(); If there are spaces, as your code suggests, php will behave erratically.
Then look at your code again. /niels ml