php cookie在第一次读取时不工作 [英] php cookie does not work at the first time reading

查看:124
本文介绍了php cookie在第一次读取时不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的初学者,学习使用cookie登录。请任何正文请检查我的代码,看看我的问题是什么,或让我如何解决这个问题。



当我第一次打开网页时,不管用。它会工作,当我重复打开这个链接。但是,我仍然无法使它的工作后,我使用函数包括和头一个代码是:



一个代码cookie.php是:

 <?php 
setcookie(cookiename,$ _ REQUEST ['name']);
if(isset($ _ COOKIE ['cookiename'])){
$ cookieSet ='Cookie''。 $ _COOKIE ['cookiename'];
} else {
$ cookieet ='没有设置Cookie';
}
setcookie(cookiepwd,$ _ REQUEST ['pwd']);
print_r($ _ COOKIE);
?>

当我第一次运行这个代码时,它不会显示任何东西。我可以第二次看到cookie数据。从某些网站,据说cookie不会在同一页面上读取。



所以我把print_r($ _ COOKIE)移动到第二个php文件以及添加函数include ()或header()到上述文件,但两者都不工作。



Cookie2.php:

 <?php 
setcookie(cookiename,$ _ REQUEST ['name']);
if(isset($ _ COOKIE ['cookiename'])){
$ cookieSet ='Cookie''。 $ _COOKIE ['cookiename'];
} else {
$ cookieet ='没有设置Cookie';
}
setcookie(cookiepwd,$ _ REQUEST ['pwd']);
include('printcookie.php');
//或头(位置:printcookie.php)
?>

printcookie.php:

 <?php 
print_r($ _ COOKIE);
?>

非常感谢您提前回答!



Michelle

解决方案

setcookie 只设置标题正在发送到客户端。它不会更改 $ _ COOKIE 超全球。



另一方面 - $ _ COOKIE



因此,在第一步 - 您将Cookie设置为 setcookie 并且在 $ _ COOKIE 中没有任何内容,因为客户端还没有发送它,只会在下一个请求。 / p>

没有办法做你想要的,而不是手动修改 $ _ COOKIE
$ b

PS:把用户的密码放在cookie中是一个坏主意


I am a beginner for PHP and studying to use cookie for login. Would any body please check my code to see what is my problem, or let me how to fix this problem.

When I open the page at the first time, the cookie will not work. It will work when I repeated to open that link. However, I still could not make it work after I use function include and header One of codes is :

One code cookie.php is :

<?php
setcookie("cookiename",$_REQUEST['name']);
if(isset($_COOKIE['cookiename'])){
$cookieSet = ' The Cookie is ' .  $_COOKIE['cookiename'];
} else {
$cookieset = ' No Cookie has been set';
}
setcookie("cookiepwd",$_REQUEST['pwd']);
print_r($_COOKIE);
?>

When I run this code first time, it will does not show any thing. I can see cookie data at second time. From some website it is said that cookie would not be read at the same page.

So I moved print_r($_COOKIE) to second php file as well as added function include() or header() to above file, but both neither works.

Cookie2.php:

<?php
setcookie("cookiename",$_REQUEST['name']);
if(isset($_COOKIE['cookiename'])){
$cookieSet = ' The Cookie is ' .  $_COOKIE['cookiename'];
} else {
$cookieset = ' No Cookie has been set';
}
setcookie("cookiepwd",$_REQUEST['pwd']);
include(‘printcookie.php’);
//or header("Location: printcookie.php")
?>

printcookie.php:

<?php
print_r($_COOKIE);
?>

Thank you very much for answering in advance!

Michelle

解决方案

setcookie only sets up the header, that is being sent to the client. It doesn't change the $_COOKIE superglobal.

In other hand - $_COOKIE is filled up with the cookies sent from the client

So at first step - you set the cookie with setcookie and have nothing in $_COOKIE because client hasn't sent it yet, and will only on the next request.

And there is no way of doing what you want, rather than modifying $_COOKIE manually

PS: it is a bad idea to put user's password in the cookie

这篇关于php cookie在第一次读取时不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆