php 通过ajax设置php会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8694341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set php session via ajax
提问by siannone
I'm trying to build my AJAX login system but I'm having some problems with PHP sessions.
我正在尝试构建我的 AJAX 登录系统,但我在 PHP 会话方面遇到了一些问题。
This is the AJAX code I use in my index.php
:
这是我在我的中使用的 AJAX 代码index.php
:
$("#buttonLogin").click(function(){
$.post("<?php echo $AJAX ?>/ajaxLogin.php",{
Username : $("#loginUsername").val(),
Password : $("#loginPassword").val()
},
function(result){
if(result == "OK"){
window.location.href = "<?php echo $PUBLIC?>/home.php";
} else {
$("#loginMessageError").show();
}
});
});
And this is ajaxLogin.php
that is called via AJAX:
这是ajaxLogin.php
通过 AJAX 调用的:
<?php
require_once("../settings.php");
require_once($ABS_ENGINE."/classUser.php");
$user = new User();
if($user->loginUser($_POST["Username"], $_POST["Password"])){
$UserID = $user->getUserId($_POST["Username"]);
session_start();
$_SESSION['UserID'] = $UserID;
echo "OK";
} else {
echo "ERROR";
}
?>
When I'm in home.php
and I try to echo $_SESSION["UserID"]
, I get the following error:
当我进入home.php
并尝试 echo 时$_SESSION["UserID"]
,出现以下错误:
Notice: Undefined index: UserID in C:\xampp\htdocs\webname\resources\templates\headerHome.php on line 23
注意:未定义索引:UserID in C:\xampp\htdocs\webname\resources\templates\headerHome.php 第 23 行
Probably this is not correct because session must be set before any output but if i try to echo $_SESSION['UserID'] = $UserID;
line it's session variable is correctly displayed.
这可能是不正确的,因为会话必须在任何输出之前设置,但是如果我尝试回显$_SESSION['UserID'] = $UserID;
行,它的会话变量会正确显示。
回答by vkGunasekaran
You need to initiate the session first, like session_start().then only you can have the access to session variables. Have a look at this simple example , it might help you:
您需要先启动会话,例如 session_start()。然后只有您才能访问会话变量。看看这个简单的例子,它可能对你有帮助:
aj.php
php文件
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready( function(){
$.ajax({
type : 'GET',
url : 'sess.php',
data: {
user : 'guna',
},
success : function(data){
alert(data);
},
error : function(XMLHttpRequest, textStatus, errorThrown)
{alert ("Error Occured");}
});
});
</script>
</html>
sess.php
php文件
<?php
session_start();
$_SESSION['user']=$_GET['user'];
echo $_SESSION['user'];
?>
As other guys pointed out, better you can also check for session_start() in the page where you reading the session variables.
正如其他人指出的那样,您还可以在读取会话变量的页面中检查 session_start() 更好。
回答by Nicola Peluchetti
When I had this kind of problem, the thing that solved it was using exit();
当我遇到这种问题时,解决它的方法是使用 exit();
<?php
require_once("../settings.php");
require_once($ABS_ENGINE."/classUser.php");
$user = new User();
if($user->loginUser($_POST["Username"], $_POST["Password"])){
$UserID = $user->getUserId($_POST["Username"]);
session_start();
$_SESSION['UserID'] = $UserID;
echo "OK";
exit();
} else {
echo "ERROR";
}
?>
回答by Starx
Better check if session_start()
is present in home.php. Without this you will not be able to read the session data.
最好检查session_start()
home.php 中是否存在。没有这个,您将无法读取会话数据。
When you are doing echo $_SESSION['UserID'] = $UserID;
you will assigning and accessing at a same line, so it will obviously work.
当您这样做时,echo $_SESSION['UserID'] = $UserID;
您将在同一行分配和访问,因此它显然会起作用。