在 AJAX 请求中设置 cookie?

编程入门 行业动态 更新时间:2024-10-24 23:24:19
本文介绍了在 AJAX 请求中设置 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用对 PHP 的 jQuery AJAX 调用验证登录表单.在 php 中,我创建了一个会话,如果他们选中了记住我"复选框,我想创建一个 cookie.这是php代码:

I'm validating a login form with jQuery AJAX call to PHP. In php, I create a session and if they checked the 'remember me' checkbox, I want to create a cookie. Here's the php code:

<?php include '../includes/connection.php'; date_default_timezone_set('GMT'); $name = $_POST['username']; $pass = $_POST['password']; $query = mysql_query("SELECT id, username, password FROM users WHERE username = '$name' LIMIT 1"); if(mysql_num_rows($query) == 0) { echo 'error'; exit; } while($row = mysql_fetch_array($query)) { if($row['username'] == $name && $row['password'] == $pass) { session_start(); $_SESSION['username'] = $row['username']; $_SESSION['usrID'] = $row['id']; echo 'success'; if($_POST['remember']) { setcookie('username', $row['username'], $exp); setcookie('password', $row['password'], $exp); setcookie('usrID', $row['id'], $exp); } } else { echo 'error'; exit; } } ?>

会话设置成功,但是cookie根本没有设置.我已经尝试设置所有值(域、路径等),但这并没有改变任何东西.有什么明显的我遗漏了吗?

The session is set successfully, however the cookie is not set at all. I've tried setting all the values (domain, path, etc.) but that didn't change anything. Is there anything obvious I'm missing?

推荐答案

这里有一些建议:

  • 确保您指定了正确的日期到期格式
  • 在重定向页面上设置 cookie 时,必须在调用 header('Location: ....'); 后设置 cookie,例如:

  • Make sure that you are specifying the correct expiration format of date
  • When setting a cookie on a page that redirects, the cookie must be set after the call to header('Location: ....'); eg:

header('位置:www.example/');setcookie('asite', $site, time()+60*60, '/', 'site');

如果您有像 www.domain/path1/path2/ 这样的人工网址,那么您必须将 cookie 路径设置为/以适用于所有路径,而不仅仅是当前路径.

If you have human urls like www.domain/path1/path2/, then you must set cookie path to / to work for all paths, not just current one.

setcookie('type_id', $new_type_id, time() + 60*60*24*30, '/');

注意参数中的最后一个 /.

Notice the last / in the arguments.

来自 PHP 手册:

服务器上的路径cookie 将可用.如果设置为'/', cookie 将可用整个域内.如果设置为'/foo/', cookie 只会是在/foo/目录中可用以及所有子目录,例如/foo/bar/域的.默认的值是当前目录正在设置 cookie.

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain . If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain . The default value is the current directory that the cookie is being set in.

  • setcookie() 定义了一个与其余 HTTP 标头一起发送的 cookie.与其他标头一样,cookie 必须在脚本的任何输出之前发送,这意味着在此之前不应该有 html/code echo 语句.
    • setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script meaning there should be no html/code echo statements before that.

更多推荐

在 AJAX 请求中设置 cookie?

本文发布于:2023-11-26 08:05:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AJAX   cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!