PHP 5.3教程cookie练习

时间:2015-10-28 06:43:22

标签: php cookies php-5.3

嘿伙计们我是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&amp;我的条件声明中$location <?php if ($firstName or $location) { ?>

这没有给我什么,所以我检查我的帖子方法,看它是否发送回页面(据我所知)

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

1 个答案:

答案 0 :(得分:0)

我看到你的代码很好。 尝试将forgetInfo()中的-3600设置为-3600 * 24。时间功能在您的服务器中执行,并且cookie存储在您的计算机中(因此在您的计算机中评估cookie时间)。它可能是几个小时的时差。