在 AJAX 请求中设置 cookie? [英] Setting a cookie in an AJAX request?

查看:48
本文介绍了在 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('位置:http://www.example.com/');setcookie('asite', $site, time()+60*60, '/', 'site.com');

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

If you have human urls like www.domain.com/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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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