2012-11-01 91 views
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作爲發展的理由。

感謝 托馬斯

+0

歡迎來到SO :)我建議你修改一下你的問題,這樣你會得到更多的回覆。你可以更容易閱讀(你的PHP代碼可以做一些縮進)。您可以 - 也可能應該 - 將其降低到重現問題所需的最低限度。我得到的印象是,這裏的許多人會回答專注的問題,而不是調試別人的代碼。祝你好運! – hashchange

回答

0

嘗試從改變你的PHP代碼:

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

到:

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

或:

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

我嘗試了兩種變化,並得到相同的結果。你自己嘗試過整個劇本嗎?我注意到第一個腳本中的受訪者在第二個腳本中得到驗證,所以這是一個合理的建議。但它仍然會重定向到該視頻。 –

+0

如果幫助任何人試圖幫助我,我的問題是:「爲了正確重定向,需要更改哪部分代碼?」我已經發布了所有的代碼,因爲我不知道什麼取決於什麼。我希望「1答案」能夠奏效,但它沒有任何區別。謝謝,不過,對於這個建議,不過。 –

+0

這是什麼:if(isset($ _ POST ['YES']))我沒有看到名稱爲「YES」的輸入字段 – Relentless

0

KI發現了問題,似乎THA t $ _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> 
+0

我可能會錯過一些東西,但我無法讓它爲我工作。 –

+0

如果你複製並粘貼到它自己的index.php頁面,你會看到它是如何工作 – Relentless

+0

或嘗試更改你的cookie名稱 – Relentless

0

我能夠做下面的工作,讓我吃驚。我認爲在編碼方面我沒有什麼更好的,只是更加勤奮和不願放棄。 這是我的決心: 請記住,我正在Joomla!平臺,所以index.php是網站中每個頁面的基礎頁面。因此,開始驗證不管是什麼網頁訪問者使用入門,初始代碼必須進入模板/?模板的名稱?的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提供的所有幫助。我希望發佈這些內容對其他人有價值。如果您有問題或想告訴我我做了一些不可饒恕的事情,我願意提供所有信息。謝謝。