如何在PHP中添加延迟

时间:2016-10-03 15:05:47

标签: javascript 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>';




}}



?>

2 个答案:

答案 0 :(得分:1)

sleep无法满足您的需求。 您使用header('location:...');重定向您的用户,并且在您输出数据后(即 - 向用户显示消息)您无法修改标题信息。 您必须使用带有setTimeout

的JavaScript进行重定向
<?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
}}

答案 1 :(得分:0)

使用JavaScript方法可能会更容易。

而不是if (&test[0] == &test2[0]){

if (memcmp(test, test2, sizeof(test)) == 0)

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