ajax php session失效,使用AJAX / jQuery设置$_SESSION | PHP会话不起作用?

我正在为我的网站编写一个登录脚本.我编写了登录脚本,并通过jQuery通过

AJAX调用将表单绑定到它.

这是表单调用的PHP:

# Make sure form data was passed to the script

IF (isset($_POST) && isset($_POST['username']) && isset($_POST['password'])){

# Connect to the database

REQUIRE('../../../../my_db.PHP');

# Define variables

$given_username = $_POST['username'];

$given_password = $_POST['password'];

$hashed_password = md5($given_password);

$matched_username = "";

$matched_password = "";

# See if there is matching info in the database

$sql = 'SELECT username,pass FROM users WHERE username="'.$given_username.'" AND pass = "'.$hashed_password.'"';

$result = MysqL_query($sql);

WHILE($row = MysqL_fetch_assoc($result)){

$matched_username = $row['username'];

$matched_password = $row['pass'];

};

# If there was a match

IF ($matched_username != "" && $matched_password != ""){

# Double check the values match

IF ($given_username == $matched_username && $hashed_password == $matched_password){

# If there is only one result returned

$session_sql = 'SELECT * FROM users WHERE username="'.$matched_username.'" AND pass = "'.$matched_password.'"';

$session_result = MysqL_query($session_sql);

IF(count(MysqL_fetch_assoc($session_result)) != 0 && count(MysqL_fetch_assoc($session_result)) < 2){

# If they do,start a session

if(!isset($_SESSION)) {

session_start();

session_regenerate_id();

};

# Set our session values

WHILE($session_row = MysqL_fetch_assoc($session_result)){

$_SESSION['id'] = $session_row['id'];

$_SESSION['last_login'] = $session_row['last_login'];

$_SESSION['username'] = $session_row['username'];

$_SESSION['signup_date'] = $session_row['signup_date'];

};

# Set users last login date and time to this login

$update_sql = 'UPDATE users SET last_login = NOW WHERE username="'.$matched_username.'" AND pass = "'.$matched_password.'"';

$update = MysqL_query($update_sql);

echo json_encode(array("success"=>"user logged in","session"=>$_SESSION));

}ELSE

echo json_encode(array("error"=>"More than one user with the same information. What did you do?!"));

}ELSE

echo json_encode(array("error"=>"invalid login provided"));

}ELSE

echo json_encode(array("error"=>"invalid login provided"));

}ELSE

echo json_encode(array("error"=>"you must supply a username and password"));

?>

但是如果我做console.log(result.session)我得到[],这让我觉得通过ajax设置会话变量是不可行的,或者会话本身不能正常工作.

我没有从这段代码中得到任何错误.

有人能指出我正确的方向吗?

我不认为我可以访问PHP.ini,但我记得很久以前你必须设置会话在一个文件中运行,但对于我的生活我找不到一个例子.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值