Cookie
因为在本地测试完全没有问题,所以ssh到远程服务器上(不是发布服务器,建议不要直接在Publish Server上直接改东西),进行了一下断点的测试,最后发现是一个比较复杂的逻辑中有个函数在构造函数中调用了登陆验证。没有验证通过所以就跳走了。下面我们通过程序来看一下php cookie的使用,也许你看完上面那段还纳闷,这个和cookie有什么联系呢?
大体说一下在我们的MVC结构中所用的登陆验证:
在controller中的构造函数用有一个专门验证登陆的函数,根据模块是否需要登陆验证来加如此函数。
bug程序:在setcookie后,当前页面进行了登陆验证。
bug原因:当前页面是获得不了在此页刚刚设置的cookie的
下面我们来用程序测试一下:
这里是一个设置cookie的函数,我们现在用的,偷懒了 呵呵
复制代码 代码如下:
function dsetcookie($var, $value, $life = 0) {
global $_cookie_domain, $_cookie_path, $_timestamp, $_SERVER;
setcookie($var, escape($value), $life ? $_timestamp + $life : 0,
$_cookie_path, $_cookie_domain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}
然后建立test1.php,内容如下:
复制代码 代码如下:
$td = date('d');
dsetcookie("testvar",$td,30*24*60*60);
print_r($_COOKIE);
?>
将会得到一个空的数组array();
那我们在设置完后什么时候才能第一次使用这个$_COOKIE变量呢?
现在我们把test1.php这个程序稍微改动一下:
复制代码 代码如下:
$td = date('d');
dsetcookie("testvar",$td,30*24*60*60);
print_r($_COOKIE);//这个要注释掉 要不然会报header警告
$location = "test2.php";
header("Location: ".$location);
?>
然后我们在test2.php中来显示$_COOKIE看一下
复制代码 代码如下:
print_r($_COOKIE);
?>
这里我们将会得到:
Array
(
[testvar] =10
)
这里就能够获得并使用这个cookie值了。
为什么呢?
你可以这么理解:当前设置的Cookie不是立即生效的,而是要等到下一个页面时才能看到.
这是由于在设置的这个页面里Cookie由服务器传递给客户浏览器,在下一个页面浏览器才能把Cookie从
客户的机器里取出传回服务器的原因。
先写到这里:
你可以通过下面的几个链接更加深入的了解一下COOKIE
PHP COOKIE及其使用
Netscape公司关于Cookie的官方原始定义的网址:http://www.netscape.com/newsref/std/cookie_spec.html

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
