drevls8138 2013-05-08 09:26
浏览 76
已采纳

在进入网站之前签订用户协议 - PHP,Redirect,Cookies

I need some help with php redirection, cookies, etc. To specify what I exactly want it to do please take a look at description:

I have created files: index.php, contact.php, info.php etc. .. I have also make agecheck.php

And so I what it to, when you go to index.php, contact.php, info.php, etc., then it shall redirect to agecheck.php, where you have the opportunity to click on two buttons YES or NO. If you click YES, it returns you to the previous page that you were redirected from, and if you click NO, it shall just stay on agecheck.php with a note that says:

you have to be 18 to enter the site.

But I will also like to have cookie on, that will remembers if you had clicked YES before, so you shouldn't have to be redirected every time when you enter the site.

  • 写回答

2条回答 默认 最新

  • drws65968272 2013-05-08 09:35
    关注

    You can set a cookie or use a session, but this will not work if your user's browser does not accept cookies.

    The advantage of a cookie is that you can set it to continue to exist after the user closes the browser (but the user can disable this behavior)

    session (also requires that user allows cookies)

    <?php
    // This check must be at the top of every page, e.g. through an include
    session_start();
    if(!isset($_SESSION['agecheck']) || !$_SESSION['agecheck']){
        $_SESSION['agecheck_ref'] = $_SERVER['REQUEST_URI'];
        header("Location: http://your.site/agecheck.php");
        die();
    }
    ?>
    
    <?php
    // You need to set the session variable in agecheck.php
    session_start();
    if($age >= 18){
        $_SESSION['agecheck'] = true;
        if(!isset($_SESSION['agecheck_ref'])) {
            $_SESSION['agecheck_ref'] = "/";
        }
        header("Location: http://your.site" . $_SESSION['agecheck_ref']);
    }
    ?>
    

    Or similar with cookies, which you can set to last longer

    <?php
    // This check must be at the top of every page, e.g. through an include
    session_start();
    if(!isset($_COOKIE['agecheck']) || $_COOKIE['agecheck'] != "true"){
        $_SESSION['agecheck_ref'] = $_SERVER['REQUEST_URI'];
        header("Location: http://your.site/agecheck.php");
        die();
    }
    ?>
    
    <?php
    // You need to set the cookie in agecheck.php
    session_start();
    if($age >= 18){
        setcookie("agecheck", "true", time()+60*60*24*90); // Remember answer for 90 days
        if(!isset($_SESSION['agecheck_ref'])) {
            $_SESSION['agecheck_ref'] = "/";
        }
        header("Location: http://your.site" . $_SESSION['agecheck_ref']);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?