简单的PHP Cookie [英] Simple PHP Cookie

查看:63
本文介绍了简单的PHP Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验cookie,并且正在做一个简单的示例,

I am experimenting with cookies and i am doing this quick example,

<html>
<head>
    <meta charset="UTF-8">
    <title>Cookies</title>
</head>
<body>
    <!-- Start of FORM -->
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> 
        Username: <input type="text" name="username"><br>
        <input type="submit" name="submit" value="Submit">
    </form>
    <!-- End of FORM -->
    <hr>
    <?php
    if (isset($_POST['username'] )) {
        setcookie('username', $_POST['username'], time() + 1000, '/');
        if(isset($_COOKIE['username'])){
            echo "Hello " . $_COOKIE['username'];
            unset($_COOKIE['username']);
        }
    }
    ?>
</body>

可以,但是我必须单击两次提交按钮以显示我的消息,为什么?

It works but i have to click the submit button twice for my message to display, why is that?

推荐答案

PHP文档。


直到下一次加载应显示Cookie的
页时,Cookie才可见。要测试是否成功设置了
cookie,请在cookie
过期之前在下一个加载页面上检查cookie。

Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires.

因此,当您第二次单击该按钮时,实际的负载生效了,您可以看到它了( cookie )。

So whilst you clicked the button the second time, the actual load was in effect and you were able to see it(the cookie).

这篇关于简单的PHP Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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