简体   繁体   English

进入网站之前请先达成用户协议-PHP,重定向,Cookie

[英]Make a user agreement before enter site - 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: 我需要有关php重定向,Cookie等的帮助。要指定我确切希望执行的操作,请查看说明:

I have created files: index.php, contact.php, info.php etc. .. I have also make agecheck.php 我创建了文件:index.php,contact.php,info.php等..我也创建了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 . 所以我要做什么,当您转到index.php,contact.php,info.php等时,它将重定向到agecheck.php,您可以在其中单击两个按钮YESNO 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: 如果单击YES ,它将返回到上一页面,如果您单击NO ,它将停留在agecheck.php上,并带有以下注释:

you have to be 18 to enter the site. 您必须年满18岁才能进入站点。

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. 但是我也想启用cookie,它会记住您之前是否单击过YES ,因此不必在每次进入站点时都进行重定向。

You can set a cookie or use a session, but this will not work if your user's browser does not accept cookies. 您可以设置Cookie或使用会话,但是如果您的用户的浏览器不接受Cookie,则此操作将无效。

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) Cookie的优点是您可以将其设置为在用户关闭浏览器后继续存在(但用户可以禁用此行为)

session (also requires that user allows cookies) 会话(还要求用户允许cookie)

<?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 或类似的cookie,可以将其设置为更长的时间

<?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']);
}
?>

To redirect, use header() : 要重定向,请使用header()

header("Location: agecheck.php");

Then to check which button is pressed, you will have to use some JavaScript: 然后要检查按下了哪个按钮,您将必须使用一些JavaScript:

<script type = "text/javascript">
function yesbutton()
{
window.location.assign("Yourpage.php");
}
function nobutton()
{
document.write("You must be over 18 to view this page");
}
</script>

<input type = "button" onclick = "yesbutton()" value = "yes">
<input type = "button" onclick = "nobutton()" value = "no">

Then you can set a JavaScript cookie in the yesbutton() function. 然后,您可以在yesbutton()函数中设置一个JavaScript cookie。

The reason for using JScript is that the buttons are on the client side, and PHP is on the server side. 使用JScript的原因是按钮在客户端,而PHP在服务器端。 This means they can't interact. 这意味着他们无法互动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM