无法在PHP中销毁会话和cookie(Fail to destroy session and cookies in PHP)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
无法在PHP中销毁会话和cookie(Fail to destroy session and cookies in PHP)

目前我使用以下代码将用户注销。

session_start(); setcookie("u", "", time() - 999999); setcookie("i", "", time() - 999999); setcookie(session_name(),"",time()-999999); session_destroy();

$_COOKIE['u']和$_COOKIE['i']用于自动登录用户。

我的问题是当我将这个注销文件直接放在web目录下(即www/ )时,它运行得很好。 但是,当我将它放在辅助目录(即www/includes/ )或将这些代码与其他PHP页面(即更改密码然后登录用户)组合在includes文件夹中时,用户无法成功注销。

Currently I am using the following code to log users out.

session_start(); setcookie("u", "", time() - 999999); setcookie("i", "", time() - 999999); setcookie(session_name(),"",time()-999999); session_destroy();

$_COOKIE['u'] and $_COOKIE['i'] are used to automatically log in users.

My problem is when I put this logout file directly under the web directory (i.e. www/), it works very well. However, when I put it in the secondary directory (i.e. www/includes/) or combined these codes with other PHP pages (i.e. change password and then log user out) in includes folder, user is not able to log out successfully.

最满意答案

看起来你的cookie被设置在不同的路径上。 第4个参数是域路径,第5个是域。

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

见http://www.php.net/manual/en/function.setcookie.php

在使用cookie时明确设置这些是个好主意。

It seems like your cookies are being set on different paths. The 4th parameter is the domain path and the 5th is the domain.

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

See http://www.php.net/manual/en/function.setcookie.php

It's a good idea to explicitly set these when working with cookies.

更多推荐

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

发布评论

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

>www.elefans.com

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