初来乍到,PHP setcookie怎么能正确使用?我写的代码只能设置两个中的一个,怎么办?
<?php session_start(); $flag="True"; setcookie("flags",$flag,time()+3600); $name=$_SESSION['name']; setcookie("uname",$name,time()+3600*24*30); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>十六进制之火种</title><script type="text/javascript" language="javascript">flag="false";function getCookie(name){ var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null){ return unescape(arr[2]); }else{ return null; } } if(!getCookie("flags")){ alert('失败'); }else{ alert(getCookie("flags"));} if(!getCookie("uname")){ alert('失败'); }else{ alert(getCookie("uname"));} dell=getCookie("uname"); flag=getCookie("flags");function change(){ if(flag == "True"){ document.getElementById("benph").innerHTML=dell; var hp='<a href="logout.php">退出</a>'; document.getElementById("lan").innerHTML=hp; }else{ var huawei='<a href="denglu.html">登录1</a>'; var htc='<a href="register.php">注册1</a>'; document.getElementById("benph").innerHTML=huawei; document.getElementById("lan").innerHTML=htc; } } var iID =setInterval("change()",1000);</script></head><body><p id="benph"><a href="denglu.html">登录</a></p><p id="lan"><a href="register.php">注册</a></p></body></html>
回复讨论(解决方案)
function getCookie(cookie_name)
{
var allcookies = document.cookie;
var cookie_pos = allcookies.indexOf(cookie_name); //索引的长度
// 如果找到了索引,就代表cookie存在,
// 反之,就说明不存在。
if (cookie_pos != -1)
{
// 把cookie_pos放在值的开始,只要给值加1即可。
cookie_pos += cookie_name.length + 1; //这里容易出问题,所以请大家参考的时候自己好好研究一下
var cookie_end = allcookies.indexOf(";", cookie_pos);
if (cookie_end == -1)
{
cookie_end = allcookies.length;
}
var value = unescape(allcookies.substring(cookie_pos, cookie_end)); //这里就可以得到你想要的cookie的值了。。。
}
return value;
}
你获取cookie的方法有问题,用这个吧
谢谢我试试!

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

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.

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 article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
