cookie的有效期,不能正常执行,请高手看一下
文件目录结构 登陆界面与主文件在根目录,其它文件在 main文件夹中,
$name="张三";
setcookie("F_user",$name,,time()+900);
print_r($_COOKIE); 可以正常得到 F_user>张三
但运行中,发现不到900秒就退出了。
后来百度 查到 应该cookie中加上路径 ,于是改为
setcookie("F_user",$name,,time()+900,"/");
可这样以来,print_r($_COOKIE); 得到 F_user> 空, 根本得不到cookie值,导致无法登陆
请问是哪里的问题,该如何处理?
回复讨论(解决方案)
setcookie 的第三个参数为失效期,用于通知浏览器在再次访问该网站时是否传递相关的 cookie 变量
你的 setcookie("F_user",$name,,time()+900); 显然是错误的
不可能通过语法检查,自然也不会有正确的表现
你连关键的代码都隐藏了,所以不好说什么
setcookie 的第三个参数为失效期,用于通知浏览器在再次访问该网站时是否传递相关的 cookie 变量
你的 setcookie("F_user",$name,,time()+900); 显然是错误的
不可能通过语法检查,自然也不会有正确的表现
你连关键的代码都隐藏了,所以不好说什么
是手误,其实是: setcookie("F_user",$name,time()+900); 可以正常登陆并执行
setcookie("F_user",$name,time()+900,"/");
可这样以来,print_r($_COOKIE); 得到 F_user> 空, 根本得不到cookie值,导致无法登陆
查看下浏览器的cookie选项里有没有F_user
查看下浏览器的cookie选项里有没有F_user
var_dump($_COOKIE);的结果如下:
["F_user"]=> string(0) ""
浏览器的结果是这样:
你试下这样setcookie("F_user",$name,time()+900);
刚才,发现出现了两个F_user, 一个是根目录下的
一个是我程序文件夹下的
所有程序都是在这个文件夹下,但不知道为什么会出现这个F_user
我测试没有问题
已经查到原因,就是用的浏览器cookie查看方法
你先 setcookie("F_user",$name,time()+900);
再 setcookie("F_user",$name,time()+900,"/");
由于作用域不同,自然就是两个 F_user 了
这两个 F_user 传到 php 时,并没有路径的约束
按 php 的约定,数组的同名键是后来的覆盖先到的
结果就是你遇到的情况了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.
