PHP密码保护页面 - 重定向

时间:2014-01-22 15:29:09

标签: php password-protection

我正在使用http://www.fullypixel.com/page/tutorials.html/_/fully-pixel-forum-faq/simple-php-password-protection-for-a-single-page-r27的PHP密码保护代码段,它看起来像......

<?php    
if (!isset($_POST['txtAccCode']))
{
//If not isset -> set with dummy value
$_POST['txtAccCode'] = "undefine";
}
// Define your user array
$access_codeArray = array("john","paul","george","ringo","b4dh39gsv55x");
$access_code = $_POST['txtAccCode'];
$result = in_array($access_code, $access_codeArray); 
  if ($_POST['txtAccCode'] != $result) {
  ?>
  <style type="text/css">
  #login {margin:0 auto; width:500px;}
  .login {font-family:"Verdana", sans-serif;border:2px solid #3753f5;}
.login p {font-size:13.0px;}
.login p {padding-left:10px;}
h2.login {padding:10px;}
</style>
<div id="login">
<h2 class="login">Enter Access Code to view content</h2>  
<form class="login" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">     
<p><label for="txtAccCode">Enter Access Code:</label>    
<br /><input type="text" title="Enter Access Code" name="txtAccCode" /></p>     
<p><input type="submit" name="Submit" value="Login" /></p>  
</form> 
</div> 
<?php  
echo '<script> alert ("Please enter a valid access code to continue.");; </script>';
}  else {  ?>

一切都很好但我现在正在尝试修改它,以便在输入正确的密码时重定向,而不是显示隐藏的内容。

这是我应该用PHP做的事情还是我需要使用javascript来做这件事?

2 个答案:

答案 0 :(得分:1)

在示例的最后,您有else,所以:

// ...
} else {
  header('Location: http://google.com');
  exit;
}

答案 1 :(得分:1)

一旦用户满足记录所需的条件,您应该重定向:

if(empty($errors) && $_POST)    #not erros, user validated then
        {
 exit(header('Location: logeed_user_landing.php));
}