2

经过大量搜索以及反复试验,我难住了。重定向不允许我访问实际站点的代码有什么问题?我无法获取年龄输入以将我引导至主站点或 index.php。如果答案错误,无论输入的日期如何,都会重定向到用户会去的地方——在这种情况下是 youtube。

这是我在本地工作的 index.php 中的内容:

<?php

@session_start();
$url = 'http://localhost/J2.5/index.php';

if (!isset($_COOKIE['Visited'])) {
    $_COOKIE['Visited'] = 1;
    $url = 'http://localhost/J2.5/ageverificationone.php';
}

header("Location: {$url}");

?>

该部分非常适合弹出年龄验证脚本所在的实际页面-ageverificationone.php(可以是任何名称..尝试了很多变体):

<?php
session_start(); 

if($_SERVER['REQUEST_METHOD']=='POST') 
{ 
if(isset($_POST['YES'])) 
{ 
$redirect=isset($_GET['return'])?urldecode($_GET['return']):'./'; 
$expire=isset($_GET['x']) && is_numeric($_GET['x'])?intval($_GET['x']):-1; 
if($expire==-1) 
{ 
$_SESSION['verified']="yes"; 
header("location: ".$redirect); 
exit(0); 
} 
if($expire==0) 
{ 
setcookie("verified", "yes",mktime(0,0,0,01,01,date("Y")+30)); 
$_SESSION['verified']="yes"; 
header("location: ".$redirect); 
exit(0); 
} 
setcookie("verified", "yes",(time()+$expire)); 
$_SESSION['verified']="yes"; 
header("location: http://www.superiorvaping.com/"); 
exit(0); 
}else{ 
header("location: http://www.youtube.com/watch?v=gppbrYIcR80"); 
exit(0); 
} 
} 

?>
<html>
<head>
<title>Age Verification</title>
<script language=Javascript>
  <!--
  function isNumberKey(evt)
  {
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
  }
  //-->
</script>
<script type="text/javascript">
function jumpField(elmnt,content)
{
if (content.length==elmnt.maxLength)
{
next=elmnt.tabIndex
if (next<document.forms[0].elements.length)
    {
    document.forms[0].elements[next].focus()
    }
}
}
</script>
<script src="http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js"></script> 
<script src="js/fade.js" type="text/javascript"></script>
<link href="js/ageverify.css" type="text/css" rel="stylesheet" />
</head>
<body id="body" OnLoad="document.ageVerifyForm.month.focus();">
<img src="images/logoVerify.png" alt="Superior Vaping" id="logoVerify" /><br />
<p id="welcome">Welcome! Please enter your date of birth.</p>
<form id="ageVerifyForm" name="ageVerifyForm" action="" method="POST" onsubmit="return     formCheck(this);">
<input type="text" tabindex="1" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="month" name="month" maxlength="2" size="2" required="required" value="MM" onfocus="value=''" />
<input type="text" tabindex="2" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="day" name="day" maxlength="2" size="2" required="required" value="DD" onfocus="value=''"  />
<input type="text" tabindex="3" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="year" name="year" maxlength="4" size="4" required="required" value="YYYY" onfocus="value=''" />
<br />
<input type="submit" value="Enter Superior Vaping" id="submit" name="submit" />
</form>
<script language="JavaScript">
<!--
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("month", "day", "year");
// Enter field description to appear in the dialog box
var fieldDescription = Array("month", "day", "year");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
    var obj = formobj.elements[fieldRequired[i]];
    if (obj){
        switch(obj.type){
        case "select-one":
            if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
                alertMsg += " - " + fieldDescription[i] + "\n";
            }
            break;
        case "select-multiple":
            if (obj.selectedIndex == -1){
                alertMsg += " - " + fieldDescription[i] + "\n";
            }
            break;
        case "text":
        case "textarea":
            if (obj.value == "" || obj.value == null){
                alertMsg += " - " + fieldDescription[i] + "\n";
            }
            break;
        default:
        }
        if (obj.type == undefined){
            var blnchecked = false;
            for (var j = 0; j < obj.length; j++){
                if (obj[j].checked){
                    blnchecked = true;
                }
            }

        }
    }
}

if (alertMsg.length == l_Msg){
    return true;
}else{
    document.getElementById("error").innerHTML="All Fields Must be Filled out";
    return false;
}
}
// -->
</script>

</body>
</html>

我在 Joomla 基础中使用它。这是几个脚本拼凑在一起,至少可以发挥它的作用。如前所述,缺少最后一步 - 使用 cookie 获得正确的重定向,因此用户只能获得一次年龄验证,并且无论进入页面如何,它都会出现。这是针对电子烟网站的,他们要求进行年龄验证。

我正在使用 WAMP 作为开发基础在本地执行此操作。

谢谢托马斯

4

3 回答 3

0

尝试从以下位置更改您的 php 代码:

if (!isset($_COOKIE['Visited'])) {

至:

if (!isset($_COOKIE['verified'])) {

或者:

if($_SESSION['verified'] != "yes"){
于 2012-11-05T19:44:19.443 回答
0

KI发现了问题,好像不能使用$_COOKIE["Visited"],肯定是系统标准之类的东西,所以要改成别的东西:visitedherebefore。这是我如何让它工作的:

<?php

@session_start();
$showform = false;

    //created my own date function
function age($dob){ 
    $date1 = new DateTime(date("Y-m-d", strtotime($dob)));
    $date2 = new DateTime(date("Y-m-d"));
    $interval = $date1->diff($date2);
    return $interval->y;
}

    //if a form is submitted do the following
if(array_key_exists("submit", $_POST)){
    $m = $_POST["month"];
    $d = $_POST["day"];
    $y = $_POST["year"];

    $age = age($y."-".$m."-".$d);
    //check age here
    if($age > 17){
                    //set cookie here and redirect here if you want
        setcookie('visitedherebefore', 'Cookies seems legit', time() + 3600);
        die("Old enough... nuff said...");
    }else{
                    //redirect here as well
        die("Too Young Sorry! you will now be redirected");
    }
}

    //check if user has been here before, redirect here as well if you want
if (isset($_COOKIE['visitedherebefore'])) {
  print "welcome back!";
  die();
}

?>

<form action="index.php" method="post">
    <input type="text" tabindex="1" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="month" name="month" maxlength="2" size="2" required="required" value="MM" onfocus="value=''" />
    <input type="text" tabindex="2" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="day" name="day" maxlength="2" size="2" required="required" value="DD" onfocus="value=''"  />
    <input type="text" tabindex="3" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="year" name="year" maxlength="4" size="4" required="required" value="YYYY" onfocus="value=''" />
    <br />
    <input type="submit" value="Enter Superior Vaping" id="submit" name="submit" />
</form>
于 2012-11-27T21:42:36.203 回答
0

令我惊讶的是,我能够完成以下工作。我不认为我在编码方面做得更好,只是更加勤奋和不愿放弃。这是我的决心:记住,我正在开发 Joomla!平台,因此 index.php 是站点中每个页面的基础页面。因此,无论访问者使用哪个页面进入验证,初始代码都必须进入 templates/?template-name?/index.php

<?php   
    if(!isset($_COOKIE['legal'])) {         
    header("Location: verifyage.php");   
    }

    if($_COOKIE['legal'] == "no")
                    {
                    header("Location: http://www.google.com/");  
                    }  

?>

然后,在 verifyage.php 中,我使用了以下内容:

<?php
    /*if the cookie already exists from a previous remember me button click then skip this page*/                   
    error_reporting(E_ALL ^ E_NOTICE); //I don't know if that's right, but it works to keep the 1 error off the page - not by fixing, but hiding the error of legal not defined

    if($_COOKIE['legal'] == "yes")
                    {
                    header("Location: index.php");   
                    }                       

    if (isset($_POST['checkage'])) 
    {
    $day = '';
    $month = '';
    $year = '';
    $minage = '18';


    if(isset($_POST['day']))
            {
            $day = $_POST['day'];
            }

    if(isset($_POST['month']))
            {
            $month = $_POST['month'];
            }

    if(isset($_POST['year']))
            {
            $year = $_POST['year'];
            }



    $birthstamp = mktime(0, 0, 0, $month, $day, $year);
    $diff = time() - $birthstamp;
    $age_years = floor($diff / 31556926);

    if($age_years >= $minage) 
            {
            if($rembox=="yes")
                    {
                    setcookie('legal', 'yes', time() + 31556926);
                    }
                    else{
                    setcookie('legal', 'yes', 0);

                    }
                    $url = 'index.php';
            } 
    else 
            {
            setcookie('legal', 'no', 0);
            $url = 'http://www.google.com/';
            }

    header('Location: '.$url.'');
    }

?>

在同一页面中,我添加了以下 HTML,它提供了灯箱效果并创建了输入字段:

<html>
<head>
<title>Age Verification</title>
<script language=Javascript>
  <!--
  function isNumberKey(evt)
  {
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
  }
  //-->
</script>
<script type="text/javascript">
function jumpField(elmnt,content)
{
if (content.length==elmnt.maxLength)
{
next=elmnt.tabIndex
if (next<document.forms[0].elements.length)
    {
    document.forms[0].elements[next].focus()
    }
    }
}
</script>
<script src="http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js"></script> 
<script src="js/fade.js" type="text/javascript"></script>
<link href="js/ageverify.css" type="text/css" rel="stylesheet" />
</head>
<body id="body" OnLoad="document.ageVerifyForm.month.focus();">
<img src="images/logoVerify.png" alt="Superior Vaping" id="logoVerify" /><br />
<p id="welcome">Welcome! Please enter your date of birth.</p>
<form id="ageVerifyForm" name="ageVerifyForm" action="" method="POST" onsubmit="return formCheck(this);">
<input type="text" tabindex="1" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="month" name="month" maxlength="2" size="2" required="required" value="MM" onfocus="value=''" />
<input type="text" tabindex="2" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="day" name="day" maxlength="2" size="2" required="required" value="DD" onfocus="value=''"  />
<input type="text" tabindex="3" onkeypress="return isNumberKey(event)" onkeyup="jumpField(this,this.value)" id="year" name="year" maxlength="4" size="4" required="required" value="YYYY" onfocus="value=''" />
<br />
<input type="submit" value="Verify Your Entry" id="submit" name="checkage" />
</form>
<script language="JavaScript">
<!--
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("month", "day", "year");
// Enter field description to appear in the dialog box
var fieldDescription = Array("month", "day", "year");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
    var obj = formobj.elements[fieldRequired[i]];
    if (obj){
        switch(obj.type){
        case "select-one":
            if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
                alertMsg += " - " + fieldDescription[i] + "\n";
            }
            break;
        case "select-multiple":
            if (obj.selectedIndex == -1){
                alertMsg += " - " + fieldDescription[i] + "\n";
            }
            break;
        case "text":
        case "textarea":
            if (obj.value == "" || obj.value == null){
                alertMsg += " - " + fieldDescription[i] + "\n";
            }
            break;
        default:
        }
        if (obj.type == undefined){
            var blnchecked = false;
            for (var j = 0; j < obj.length; j++){
                if (obj[j].checked){
                    blnchecked = true;
                }
            }

        }
      }
    }

if (alertMsg.length == l_Msg){
    return true;
    }else{
    document.getElementById("error").innerHTML="All Fields Must be Filled out";
    return false;
    }
}
// -->
</script>
<p id="error"></p><br />
</body>
</html>

当我在这里发布代码时,我可以看到“可能”有一些代码或行是不必要的。但我可以说它运行良好,我已经将代码实现到了实时站点中。如果有人认为有一些“不要那样做”或“我不会……”,请随时插话,因为我并没有声称自己是任何专家。我只是在一个月前发布了我所寻求的解决方案。我很确定可以删除 Onload。我感谢 Relentless 提供的所有帮助。我希望发布此内容对其他人有价值。如果您有任何疑问或想告诉我我做了一些不可原谅的事情,我愿意提供所有信息。谢谢。

于 2012-11-28T00:54:04.697 回答