PHP $_SESSION 问题
PHP $_SESSION issue
我在使用 $_SESSION 时遇到问题,我会粘贴代码并进行解释。
我有一个索引,其中包括
<div class="confidential__field text-align-center">
<table>
<tbody>
<tr>
<td><label for="email">Email</label></td>
<td><input type="text" id="email" name="email"></input></td>
</tr>
<tr>
<td><label for="pwd">password</label></td>
<td><input type="password" id="pwd" name="pwd"></input></td>
</tr>
<tr>
<td><a href="?action=register">Don't have an account ?</a></td>
<td><button form="form" id="login">Send</button></td>
</tr>
</tbody>
</table>
</div>
index.php 我们有这个(简化版,是的,我在 index.php 的开头有 session_start()):
<?php
session_start();
include("_include/_stuff/config.php");?>
<!doctype html>
<html>
<?php include("_general/head.php");?>
<body>
<?php
include("_include/login.php");
if(isset($_SESSION['ID']) && !empty($_SESSION['ID']))include("_include/header.php");
?>
</body>
</html>
然后是脚本:
$(document).ready(function(){
$("#login").click(function(){
$.post("_ajax/clientMain.php",{
login: 1,
email: $("input#email").val(),
pwd: $("input#pwd").val()
},
function(data){
if(data==1){
//alert(data);
// alert("SESSION SET");
location="index.php";
}else{
alert(data);
}
}
)
});
});
然后要检查的实际 php:
session_start();
if(isset($_POST['login']) && !empty($_POST['login'])){
$conn = db_connect();
$email = db_quote($_POST['email']);
$pwd = db_quote($_POST['pwd']);
$query_attendance = mysqli_query($conn, "select * from user where email=$email and password=$pwd");
if(mysqli_num_rows($query_attendance) > 0){
$credentials = mysqli_fetch_array($query_attendance, MYSQLI_ASSOC);
if(!empty($credentials)){
$_SESSSION['ID'] = $credentials['userID'];
echo 1;
}else{
echo 82;
}
}else{
echo 45;
}
}else{
echo 111;
}
我 运行 XAMPP(localhost) windows 10.
注意:我没有对密码进行编码,因为我只是在做基本测试,我几乎不会失败!
编辑: 将 ID 归因于会话后,会话不会保存到 index.php,因为如果我执行 var_dump($ _SESSION) 或 echo $_SESSION["ID"] 我什么也没收到,但如果我在登录脚本中用数据响应而不是 echo 1
我会 echo $_SESSION["ID"]
然后我得到 session/id,但它没有以某种方式转移到 index.php 页面!
您有一个错字,请检查它是否只出现在您问题的代码中或您的实际代码中:
$_SESSSION['ID'] = $credentials['userID'];
应该是:
$_SESSION['ID'] = $credentials['userID'];
你多了一个S
我在使用 $_SESSION 时遇到问题,我会粘贴代码并进行解释。
我有一个索引,其中包括
<div class="confidential__field text-align-center">
<table>
<tbody>
<tr>
<td><label for="email">Email</label></td>
<td><input type="text" id="email" name="email"></input></td>
</tr>
<tr>
<td><label for="pwd">password</label></td>
<td><input type="password" id="pwd" name="pwd"></input></td>
</tr>
<tr>
<td><a href="?action=register">Don't have an account ?</a></td>
<td><button form="form" id="login">Send</button></td>
</tr>
</tbody>
</table>
</div>
index.php 我们有这个(简化版,是的,我在 index.php 的开头有 session_start()):
<?php
session_start();
include("_include/_stuff/config.php");?>
<!doctype html>
<html>
<?php include("_general/head.php");?>
<body>
<?php
include("_include/login.php");
if(isset($_SESSION['ID']) && !empty($_SESSION['ID']))include("_include/header.php");
?>
</body>
</html>
然后是脚本:
$(document).ready(function(){
$("#login").click(function(){
$.post("_ajax/clientMain.php",{
login: 1,
email: $("input#email").val(),
pwd: $("input#pwd").val()
},
function(data){
if(data==1){
//alert(data);
// alert("SESSION SET");
location="index.php";
}else{
alert(data);
}
}
)
});
});
然后要检查的实际 php:
session_start();
if(isset($_POST['login']) && !empty($_POST['login'])){
$conn = db_connect();
$email = db_quote($_POST['email']);
$pwd = db_quote($_POST['pwd']);
$query_attendance = mysqli_query($conn, "select * from user where email=$email and password=$pwd");
if(mysqli_num_rows($query_attendance) > 0){
$credentials = mysqli_fetch_array($query_attendance, MYSQLI_ASSOC);
if(!empty($credentials)){
$_SESSSION['ID'] = $credentials['userID'];
echo 1;
}else{
echo 82;
}
}else{
echo 45;
}
}else{
echo 111;
}
我 运行 XAMPP(localhost) windows 10.
注意:我没有对密码进行编码,因为我只是在做基本测试,我几乎不会失败!
编辑: 将 ID 归因于会话后,会话不会保存到 index.php,因为如果我执行 var_dump($ _SESSION) 或 echo $_SESSION["ID"] 我什么也没收到,但如果我在登录脚本中用数据响应而不是 echo 1
我会 echo $_SESSION["ID"]
然后我得到 session/id,但它没有以某种方式转移到 index.php 页面!
您有一个错字,请检查它是否只出现在您问题的代码中或您的实际代码中:
$_SESSSION['ID'] = $credentials['userID'];
应该是:
$_SESSION['ID'] = $credentials['userID'];
你多了一个S