php - 如何使用年龄验证脚本正确重定向?

标签 php javascript

经过大量搜索和反复试验后,我被难住了。 重定向不允许我访问实际站点的代码有什么问题?我无法获取年龄输入以将我定向到主站点或 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 作为开发基础在本地执行此操作。

谢谢 托马斯

最佳答案

尝试更改您的 php 代码:

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

到:

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

或:

if($_SESSION['verified'] != "yes"){

关于php - 如何使用年龄验证脚本正确重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13173160/

相关文章:

javascript - 为什么容器边框会扰乱 Masonry.js 元素的放置?

javascript - 默认添加 babel-polyfill 吗?

javascript - 如何让 jQuery 仅触发精确绑定(bind)事件

php - 只需插入 1 次即可在同一列中的 MySQL 数组中插入不同的值

php - 无法将行更新为与当前相同的值吗?

javascript - 在 chrome 中工作的边界框高度在 Jsfiddle 中不起作用

javascript - jQuery - 加载所有页面后执行的 append 脚本

php - mysql_connect() : The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

php - 如何使用第一个选择中的 $_REQUEST 来填充下拉 sql 查询中的第二个选择

PHP:如何使用::而不是\访问命名空间类?