PHP 5.3教程cookie练习


PHP 5.3 Tutorial cookies excercise

嘿,伙计们,我是php的初学者,我正在学习cookie,我现在正在学习教程,但我在调试这个假设存储名称和位置cookie的练习时遇到了问题。

这是我的php脚本

<?php 
    /**
    * In this example, you create a script that can store the visitor’s first name and location in two browser cookies, 
    * retrieve and display the information from the cookies, and delete the cookies on request.
    **/
    define('URL', 'https://collabbro-bvcxtds.c9.io/app/php-practice/remember-me.php');
    if (isset($_POST['sendInfo'])) {
        storeInfo();
    } elseif (isset($_GET['action']) and $_GET['action'] == 'forget'){
        forgetInfo();
    } else {
        displayPage();
    }
    function storeInfo() { 
        if (isset($_POST['firstName'])) {
            setcookie('firstName', $_POST['firstName'], time() + 60 * 60 * 24 * 365, '', '', false, true);
        }
        if (isset($_POST['location'])) {
            setcookie('location', $_POST['location'], time() + 60 * 60 * 24 * 365, '', '', false, true);
        }
        header('Location:' . URL);
    }
    function forgetInfo() {
        setcookie( 'firstName', '', time() - 3600, '', '', false, true);
        setcookie( 'location', '', time() - 3600, '', '', false, true);
        header('Location:' . URL);
    }
    function displayPage() {
        $firstName = (isset($_COOKIE['firstName'])) ? $_COOKIE['firstName'] : '';
        $location = (isset($_COOKIE['location'])) ? $_COOKIE['location'] : '';
    }
?>

这是我的html

<!DOCTYPE html>
<html>
    <head>
        <title>Remeber me user information with cookies</title>
    </head>
    <body class="homepage">
        <!-- nav -->
        <!-- end nav -->
        <h1>Remember User infomration with cookies</h1>
        <?php if ($firstName or $location)  { ?>
        <?php echo '<pre>'; var_dump($firstName); exit(); ?>
        <?php echo '<pre>'; var_dump($location); exit(); ?>

        <p>Hey, <?php echo $firstName ? $firstName : 'visitor' ?> <?php echo $location ? 'in $location' : '' ?>! </p>
        <p>Here's a little nursery rhyme I know:</p>
        <p><em>Hey diddle diddle, <br> 
        The cat played the fiddle <br>
        the cow jumped over the moon <br>
        the little dog laughed to see such sport <br>
        and the dish ran away with the spoon  </em></p>
        <p><a href="<?php URL ?>?action=forget">Forget about me</a></p>
        <?php } else { ?>
        <form action="<?php URL ?>" method="post">
            <input type="text" name="firstName" id="firstName" placeholder="please enter your first name"/>
            <input type="text" name="location" id="location" placeholder="Where do you live?"/>
            <input type="submit" value="Submit" name="sendInfo" id="sendInfo"/>
        </form>
        <?php } ?>
        <!-- footer -->
        <!-- end footer -->
    </body>
</html>

以下是我的调试步骤

我做的第一件事是vardump()$firstName&$location在我的条件声明中<?php if ($firstName or $location) { ?>

这给了我什么,所以我检查我的张贴方法,看看它是否发送回页面(据我所知)

但是看着我处理输入的代码,一切看起来都很好,不知道发生了什么,谢谢你的帮助!

我觉得你的代码不错。尝试将forgetInfo()中的-3600设置为-3600*24。时间函数在您的服务器中执行,cookie存储在您的计算机中(因此cookie时间在您的电脑中进行评估)。这可能是许多小时的时差。