2016-10-03 147 views
1

中添加帐户验证过程中的延迟。他/她立即重定向到仪表板。但是我想给出一个确认消息,说明他/她的账户已经过了几秒钟的验证。 这里是我的代码当用户点击验证链接时,如何在php

    <?PHP 
require_once 'include/Functions.php'; 
$db = new DB_Functions(); 




if(isset($_GET['username'])&&isset($_GET['q'])) 
{ 
$username= $_GET['username']; 

$hash= $_GET['q']; 




$salt = "498#2D83B631%3800EBD!801600D*7E3CC13"; 

     $resetkey = hash('sha512', $salt.$username); 


     if($hash==$resetkey){ 

      $user = $db->activateAccount($username); 

      if ($user != false) { 
     // user is found 
     echo '<script>'; 
    echo 'document.getElementById("result_status").innerHTML = "<strong>Congratulations! Your Account has been verified .</strong>"'; 

    echo '</script>'; 


     $passwords=$db->getPasswordFromUsername($username); 

    $users = $db->loginUserWithMdfPassword($username, $passwords); 
    if ($users != false) { 
     // password is found 

     $properlyLogged=true; 


if($properlyLogged) { 
    // season for storing data start here 

session_start(); 

$_SESSION['username']=$username; 


header('Location: http://localhost/mywebsite/dashboard.php'); 

exit(); 
// season for storing data end here 

    }} 


} 

     }else { 
      echo '<script>'; 
     echo 'document.getElementById("result_status").innerHTML = "<strong>Session has been expired.</strong>"'; 

    echo '</script>'; 




}} 



?> 
+1

瓶坯重定向可以使用睡眠功能:http://php.net/manual/en/function.sleep.php –

+0

我如何使用它在我的代码?你能告诉我吗? –

回答

0

这可能会更容易与JavaScript的方法。

代替header('Location: http://localhost/mywebsite/dashboard.php');

echo '<script>setTimeout(function(){window.location.href = "http://localhost/mywebsite/dashboard.php"}, 5 * 1000);</script>'

这将等待X * 1000毫秒(在我的例子,5秒),然后重定向到目的地。

+0

非常有帮助谢谢 –

1

sleep不会为你想要的工作。 您正在使用header('location:...');重定向您的用户,并且您在输出数据(即 - 向用户显示消息)后无法修改标题信息。 你必须使用JavaScript与setTimeout

<?PHP 
require_once 'include/Functions.php'; 
$db = new DB_Functions(); 
if(isset($_GET['username'])&&isset($_GET['q'])) 
{ 
    $username= $_GET['username']; 
    $hash= $_GET['q']; 
    $salt = "498#2D83B631%3800EBD!801600D*7E3CC13"; 
    $resetkey = hash('sha512', $salt.$username); 
    if($hash==$resetkey){ 
     $user = $db->activateAccount($username); 
     if ($user != false) { 
    // user is found 
    echo '<script>'; 
    echo 'document.getElementById("result_status").innerHTML = "<strong>Congratulations! Your Account has been verified .</strong>";'; 
// set timeout for 3 seconds - then redirect 
    echo "setTimout(function(){window.location.href = 'http://localhost/mywebsite/dashboard.php';},3000)" 
echo '</script>'; 

    $passwords=$db->getPasswordFromUsername($username); 

$users = $db->loginUserWithMdfPassword($username, $passwords); 
if ($users != false) { 
    // password is found 

    $properlyLogged=true; 


    if($properlyLogged) { 
     // season for storing data start here 
     session_start(); 
     $_SESSION['username']=$username; 
     //comment out header redirect 
     //header('Location: http://localhost/mywebsite/dashboard.php'); 
     exit(); 
     // season for storing data end here 
}} 
+0

非常感谢它的作品。 –