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条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?