在PHP和AJAX饼干的问题 [英] cookies problem in PHP and AJAX

查看:146
本文介绍了在PHP和AJAX饼干的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对我的剧本,我使用PHP和jQuery创建登录系统的一些问题。

I face some problem on my script that I use PHP and jquery to create login system.

首先,我有PHP页面包含的形式进行登录。当用户点击提交使用jQuery将数据发送到服务器

First I have PHP page contain form for login. when user click submit I use jquery to send data to server

$.post('server_login.php', {username:username.val(), password:password.val()}, function(data){
    alert(data);
}); 

在server_login.php我有功能做的登录用户。

in server_login.php I have function to doing login user.

if($_POST['username']=='username' && $_POST['password']=='1234'){
    $expire = time() + 60*60*24*30; //1 month expired.
    setcookie("user_id", $_POST['username'], $expire);
    echo true;
}

和jQuery的警戒1在我的登录页面。

and jquery alert "1" on my login page.

问题是,当我刷新我的网站,retieve的cookie,它不会告诉我。

the problem is when i refresh my website and retieve cookie, it not show me.

print_r($_COOKIE);

什么错吗?

推荐答案

如果您呼叫的脚本位于另一个文件夹服务器上(或通过URL看起来好像是在另一条路径重写),确保设置cookie的路径参数。

If the script you are calling is located in another folder on the server (or via url rewrite it appears as if it is under another path), make sure to set the path parameter of the cookie.

在默认情况下,的setcookie()设置cookie只为当前路径。

By default, setcookie() sets the cookie only for the current path.

如果你的网页是 www.domain.com 键,你让Ajax调用 www.domain.com/auth/login.php 的cookie将被设置为 / AUTH 键,将不提供外 / AUTH

If your page is www.domain.com and you make ajax call to www.domain.com/auth/login.php the cookie will be set to /auth and will not be available outside /auth.

所以尝试改变,以这样的:

So try changing to this:

setcookie("user_id", $_POST['username'], $expire, '/');

这篇关于在PHP和AJAX饼干的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆