重定向PHP

这个问题在这里已经有了答案:

  • 如何在PHP中进行重定向? 27个答案

  • 尝试使用header()函数,如下所示:

    if ($num == 1)
    
    {
    
    header("Location: http://www.example.com");
    
    }
    

    很明显,将http://www.example.com更改为您选择的位置,并将其放在页面中的任何HTML上方,否则您将得到一个标题已设置的错误。

    考虑到上述情况,您需要重新安排您的代码,如下所示:

        <?php
    
    // takes the variables from action script and assigns them php variables names
    $user = $_POST ['username'];
    $pass = $_POST ['password'];
    $error = '';
    
    // if there is a user name and password
    if ($user && $pass)
    {
        // connect to server
        mysql_connect("localhost", "", "") or die(mysql_error());
        //select database
        mysql_select_db("") or die(mysql_error());
    
        //Create a query that selects all data from the PATIENT table where the username and password match
        $query = "SELECT * FROM Patient WHERE Username = '$user' AND Password = '$pass'";
    
        //executes query on the database
        $result = mysql_query ($query) or die ("didn't query");
        //this selects the results as rows
    
        $num = mysql_num_rows ($result);
        //if there is only 1 result returned than the data is ok 
        if ($num == 1)
        {
            //sends back a data of "Success"
    header("Location: success.php");
    
        }
        else
        {
            //sends back a message of "failed"
            $error = "Unsuccessful Login";
        }
    }
    
    ?>
    
    <html>
    <head>
    </head>
        <body>
            <!-- Home -->
            <div data-role="page" id="page1">
                <div data-theme="a" data-role="header">
                <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Back
                    </a>
                    <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                     Home  
                    </a>
                    <h3>
                        Login Process
                    </h3>
                </div>
                <div data-role="content">
    
    
    <?php  echo $error; ?>
    
                </div>
            </div>
        </body>
    </html>`
    

    此外,我会敦促您在继续使用这种类型的事情之前查看PDOmysqli,并准备好带有限定值的语句,因为您正在打开自己的SQL注入攻击


    您只需更改标题并退出脚本:

    header("Location: http://www.example.com");
    die();
    

    只需使用

    header("Location: http://www.test.com");
    

    请注意,如果之前没有输出,您只能设置标题重定向。 没有单词,没有html语法,甚至没有简单的空白。

    更多细节:标题功能

    因此,您必须将PHP部分放在脚本的顶部,并将成功/错误消息存储在变量中而不是直接的“回显”中。

    例子:请注意你的if子句中的变量“$ return_message”。 没有“回声”了,但在HTML上下文中,您会找到输出的回显。

    <?php
    // takes the variables from action script and assigns them php variables names
    $user = $_POST ['username'];
    $pass = $_POST ['password'];
    
    // if there is a user name and password
    if ($user && $pass)
    {
        // connect to server
        mysql_connect("localhost", "", "") or die(mysql_error());
        //select database
        mysql_select_db("") or die(mysql_error());
    
        //Create a query that selects all data from the PATIENT table where the username and password match
        $query = "SELECT * FROM Patient WHERE Username = '$user' AND Password = '$pass'";
    
        //executes query on the database
        $result = mysql_query ($query) or die ("didn't query");
        //this selects the results as rows
    
        $num = mysql_num_rows ($result);
        //if there is only 1 result returned than the data is ok 
        if ($num == 1)
        {
            //sends back a data of "Success"
            $return_message =  "Successful Login";
            $row=mysql_fetch_array($result);
            $_SESSION['Name'] = $row['Name'];
            $_SESSION['Address'] = $row['Address'];
        }
        else
        {
            //sends back a message of "failed"
            $return_message = echo "Unsuccessful Login";
        }
    }
    
    ?></head>
        <body>
            <!-- Home -->
            <div data-role="page" id="page1">
                <div data-theme="a" data-role="header">
                <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Back
                    </a>
                    <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                     Home  
                    </a>
                    <h3>
                        Login Process
                    </h3>
                </div>
                <div data-role="content">
    
    <?php echo $return_message; ?>
    
                </div>
            </div>
        </body>
    </html>
    
    链接地址: http://www.djcxy.com/p/34699.html

    上一篇: Redirecting PHP

    下一篇: PHP page redirect