0

我要做的就是在用户登录后几秒钟将用户重定向到我的主页。这是我的代码

<?php 
    include_once("config.php");
?>

<?php if( !(isset( $_POST['login'] ) ) ) { ?>



<!DOCTYPE html>
<html>
    <head>
        <title>Codecall Tutorials - Secured Login with php5</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>

        <header id="head" >
                <p>Codecall tutorials User Login</p>
            <p><a href="register.php"><span id="register">Register</span></a></p>
        </header>

        <div id="main-wrapper">
            <div id="login-wrapper">
                <form method="post" action="">
                    <ul>
                        <li>
                            <label for="usn">Username : </label>
                            <input type="text" maxlength="30" required autofocus name="username" />
                        </li>

                        <li>
                            <label for="passwd">Password : </label>
                            <input type="password" maxlength="30" required name="password" />
                        </li>
                        <li class="buttons">
                            <input type="submit" name="login" value="Log me in" />
                            <input type="button" name="register" value="Register" onclick="location.href='register.php'" />
                        </li>

                    </ul>
                </form>

            </div>
        </div>

    </body>
</html>

<?php 
} else {
    $usr = new Users;
    $usr->storeFormValues( $_POST );

    if( $usr->userLogin() ) {

        echo "Welcome"; 


    } else {
        echo "Incorrect Username/Password"; 

    }
}
?>

此外,在用户注册后创建用户页面会非常好。我已经设置好了注册码,但这是我想要实现的。有任何想法吗?

4

5 回答 5

1

这个代码片段:

header( 'Location: http://www.yoursite.com/new_page.html' );

如果您需要,这是一个参考链接 - http://php.net/manual/en/function.header.php

于 2013-04-11T03:37:45.117 回答
1

以下是 PHP 重定向的方法:

 header('location:index.php');
于 2013-04-11T03:37:56.633 回答
1

尝试添加

header();phpfunction 将您的页面重定向到您想要的特定位置。

if( $usr->userLogin() ) {
        ob_start(); // use output buffering to avoid "header already sent error"
        echo "Welcome"; //should try to remove this if you want because its unecessary now since your redirecting your page
        header('Location: pagetoredirect');
        ob_end_flush(); //now the headers are sent

    } else {
        echo "Incorrect Username/Password"; 

    }

来源(PHP.NET

于 2013-04-11T03:38:01.680 回答
0

您可以像这样通过 header() 重定向您的页面

header("location:yoursite.com?$msg=welcome user");

重要的是要注意必须在看到任何实际输出之前调用 header() ......

于 2013-04-11T05:06:21.270 回答
0

You will need to clear your html first:

  1. Make your top part codes into:

    <?php 
        include_once("config.php");
    
        if( !(isset( $_POST['login'] ) ) ) { ?>
    
  2. Add header where you want to redirect:

        if( $usr->userLogin() ) {
            header('Location: /path/to/new/page.php');
        } else {
            echo "Incorrect Username/Password"; 
        }
    
  3. Another way is to use javascript:

        if( $usr->userLogin() ) { ?>
    
            <div class="message">
                Thank you for logging in. You will be redirected in few seconds.
                Click <a href="/path/to/new/page.php">here</a> if not redirected.
            </div>
            <script type="text/javascript">
                window.location = "/path/to/new/page.php";
            </script>
        <?php
        } else {
            echo "Incorrect Username/Password"; 
        }
    
于 2013-04-11T03:41:59.337 回答