经过几秒钟的延迟后,php自动重定向到一个页面

时间:2017-01-11 13:11:43

标签: php html redirect

我正在制作注册表单,当您成功填写所有字段时,它会重定向到thank_you_register.php。但我希望在几秒钟后,thank_you_register.php页面会自动重定向到另一个页面。我该怎么做呢? HTML代码是这样的:

<form class="register form" action="../app/controller/authController.php" method="POST">
            <div class="form-group">
                <select name="region" id="">
                    <option value="Americas">Americas</option>
                    <option value="Europe">Europe</option>
                    <option value="Asia">Asia</option>
                </select>
            </div>
            <div class="form-group">
                <input type="text" name="first_name" id="" placeholder="First Name">
                <?php if(isset($_SESSION['first_name_error'] )) : ?>
                    <span class="error"><?php echo $_SESSION['first_name_error']; ?></span>
                <?php unset($_SESSION['first_name_error']); endif; ?>
            </div>
            <div class="form-group">
                <input type="text" name="last_name" id="" placeholder="Last Name">
                <?php if(isset($_SESSION['last_name_error'] )) : ?>
                    <span class="error"><?php echo $_SESSION['last_name_error']; ?></span>
                <?php unset($_SESSION['last_name_error']); endif; ?>
            </div>
            <div class="form-group">
                <input type="email" name="email" id="" placeholder="E-mail Address">
                <?php if(isset($_SESSION['email_error'] )) : ?>
                    <span class="error"><?php echo $_SESSION['email_error']; ?></span>
                <?php unset($_SESSION['email_error']); endif; ?>
            </div>
            <div class="form-group">
                <input type="email" name="confirm_email" id="" placeholder="Confirm E-mail Address">
                <?php if(isset($_SESSION['confirm_email_error'])) : ?>
                    <span class="error"><?php echo $_SESSION['confirm_email_error']; ?></span>
                <?php unset($_SESSION['confirm_email_error']); endif; ?>
            </div>
            <div class="form-group">
                <input type="password" name="password" id="" placeholder="Password">
                <?php if(isset($_SESSION['password_error'] )) : ?>
                    <span class="error"><?php echo $_SESSION['password_error']; ?></span>
                <?php unset($_SESSION['password_error']); endif; ?>
            </div>
            <div class="form-group">
                <input type="password" name="confirm_password" id="" placeholder="Confirm Password">
                <?php if(isset($_SESSION['confirm_password_error'] )) : ?>
                    <span class="error"><?php echo $_SESSION['confirm_password_error']; ?></span>
                <?php unset($_SESSION['confirm_password_error']); endif; ?>
            </div>
            <div class="form-group">
                <input type="text" name="age" id="" placeholder="Age">
                <?php if(isset($_SESSION['age_error'] )) : ?>
                    <span class="error"><?php echo $_SESSION['age_error']; ?></span>
                <?php unset($_SESSION['age_error']); endif; ?>
            </div>
            <div class="form-group">
                <input type="submit" name="type" value="Register" class="register button">
            </div>
        </form>

表单的PHP代码是这样的:

if ($_POST['type'] == 'Register') {

    $i=0;
    $names = array('first_name', 'last_name', 'email', 'confirm_email', 'password', 'confirm_password', 'age');
    foreach($names as $field) {
        if (empty($_POST[$field])) {

            $_SESSION[$field.'_error'] = "Field cannot be empty!";
            $i++;
        }

        else
        {
          unset($_SESSION[$field.'_error']);
          $user->redirect('register.php');
        }

    }

    if (sizeof($names) == $i) {
        $user->redirect('register.php');
    }

    if ($_POST['email'] != $_POST['confirm_email']) {
        $_SESSION['confirm_email_error'] = "E-mail Address does not match!";
        exit();
    }

    if ($_POST['password'] != $_POST['confirm_password']) {
        $_SESSION['confirm_password_error'] = "Password does not match!";
        exit();
    }

    function add($region, $first_name, $last_name, $email, $confirm_email, $password, $confirm_password, $age) {
        var_dump($_POST);
        $database = Database::getInstance();
        $sql = "INSERT INTO `users`
                (`region`, `first_name`, `last_name`, `email`, `confirm_email`, `password`, `confirm_password`, `age`)
                VALUES (:region, :first_name, :last_name, :email, :confirm_email, :password, :confirm_password, :age)";

        $stmt = $database->pdo->prepare($sql);
        $stmt->bindParam(':region', $region);
        $stmt->bindParam(':first_name', $first_name);
        $stmt->bindParam(':last_name', $last_name);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':confirm_email', $confirm_email);
        $stmt->bindParam(':password', $password);
        $stmt->bindParam(':confirm_password', $confirm_password);
        $stmt->bindParam(':age', $age);
        $stmt->execute();

    }

    $user->redirect('');

我还没有填写$user->redirect('');,因为我不知道那里应该是什么。

thank_you_register页面的HTML代码是:

<?php require realpath(__DIR__ . '/header.php'); ?>

<div class="main-content">
    <div class="messageBox">
        <div class="MB_head">
            <h2>Thank You!</h2>
        </div>
        <div class="MB_content">
            <p>You are registered.</p>
        </div>
    </div>
</div>

6 个答案:

答案 0 :(得分:1)

您可以使用

<meta http-equiv="refresh" content="5; url=http://example.com/" />

或(通过javascript)

<script type="text/javascript">
   setTimeout(function() { 
       window.location.href = "http://example.com";
   },5000);
</script>

答案 1 :(得分:1)

在thank_you_register.php中添加此脚本

<script>setTimeout(function() { location.replace("otherpage.html")},3000);</script>

在3秒后更换thankyouregister。

注意:我使用location.replace来破坏thankyouregister的后退按钮

答案 2 :(得分:1)

一个简单的解决方案:

thank_you_register.php 页面中,将其放入<head>标记

<meta http-equiv="refresh" content="5; URL=http://your_site.com/">

5是秒。

答案 3 :(得分:0)

最简单的方法是使用javascript来做到这一点

window.location = "http://www.yoururl.com";

答案 4 :(得分:0)

为此使用JavaScript。它提供了更好的用户体验。

示例:

<div class="main-content">
    <div class="messageBox">
        <div class="MB_head">
            <h2>Thank You!</h2>
        </div>
        <div class="MB_content">
            <p>You are registered.</p>
        </div>
        <div>
            <p>Redirecting you back to the home page...click <a href="/url/to/redirect/to">here</a> if you are not automatically redirected</p>
        </div>
    </div>
</div>

<script>
   setTimeout(function () { 
        window.location.href = "/url/to/redirect/to";
   }, 3000); // 3000 milliseconds until it calls function

</script>

答案 5 :(得分:0)

您可以使用以下Php脚本来实现上述内容

  <?php
      header('Location: http://www.example1.com/');
       sleep(20);
      header('Location: http://www.example2.com/');

 ?>