求教这个关于cookie的代码该怎样写?
问题是这样:
比如有qq.html、baidu.html、so.html这3个页面,然后从rand.php里面随机打开其中一个。
请问如何添加cookie控制,在设置的时间范围内(比如24小时),同一个用户每次都只打开相同的页面,比如这个用户第一次随机打开了baidu.html,那么下次访问的时候,仍然只打开baidu.html,而不会随机到qq.html和so.html。
回复讨论(解决方案)
设置cookie,setCookie('name','value',time()+24*3600);
if(isset($_COOKIE['url'])){
header('Location:'. $_COOKIE['url']);
}else{
$urlArr = array('qq.html','baidu.html','so.html');
$urlId = array_rand($urlArr,1);
header('Location:'. $urlArr[$urlId]);
setcookie('url',$url,time()+24*3600);
}
$arr = array('qq.html','baidu.html','so.html');$page = '';if(isset($_COOKIE['page'])){ $page = $_COOKIE['page'];}else{ $page = $arr[mt_rand(0,2)]; setcookie('page',$page,time()+86400);}echo file_get_content($page);
用户第一次打开的页面,记录cookie值,下次访问通过cookie的标示访问对应的页面。
设置COOKIE的时间就可以了。过期就失效
使用cookie可以满足需求,但用户清掉cookie或是更换浏览器就有可能看到不同页面了
建议将访问者ip与当天0点的时间戳结合去求3的余数来决定
如果要求从访问时刻开始计时24小时,就要2种方法结合起来用了

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



The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

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

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 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�...
