4

你将如何从一个 PHP 页面重定向到另一个页面?

</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
    // 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"
            echo "Successful Login";
            $row=mysql_fetch_array($result);
            $_SESSION['Name'] = $row['Name'];
            $_SESSION['Address'] = $row['Address'];
        }
        else
        {
            //sends back a message of "failed"
            echo "Unsuccessful Login";
        }
    }

    ?>
        </div>
    </div>
</body>

</html>

因此,当用户登录时,它会将他们带到上面显示的页面。我需要它做的是,如果登录成功,我需要它去重定向到另一个 PHP 页面。


登录.php

<?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)
    {
    header("Location: http://helios.hud.ac.uk/u101010/PHP/details1.php");
    }
            //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";
        }
    }
    <html>
        <head>
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <meta name="apple-mobile-web-app-capable" content="yes" />
            <meta name="apple-mobile-web-app-status-bar-style" content="black" />
            <title>
            </title>
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
            <link rel="stylesheet" href="my.css" />
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
            </script>
            <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
            </script>
            <script src="my.js">
            </script>
            <!-- User-generated css -->
            <style>
            </style>
            <!-- User-generated js -->
            <script>
                try {

        $(function() {

        });

      } catch (error) {
        console.error("Your javascript has an error: " + error);
      }
            </script>

    ?>
    </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>

详情1.php

<!DOCTYPE html>
<?php
    session_start();
    ?>
    <html>

    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="my.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
        </script>
        <script src="my.js">
        </script>
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
        <script>
            try {

                $(function() {

                });

            } catch (error) {
                console.error("Your javascript has an error: " + error);
            }
        </script>
    </head>

    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
                <a data-role="button" data-theme="c" href="menu.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Main Menu
                        </a>
                <h3>
                    Your details
                </h3>


            </div>
            <div data-role="content">
                Name:
                <?php echo $_SESSION['Name'];?>
                <br /> Address:
                <?php echo $_SESSION['Address'];?>



            </div>
    </body>

    </html>

详细信息.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="my.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
    </script>
    <script src="my.js">
    </script>
    <!-- User-generated css -->
    <style>
    </style>
    <!-- User-generated js -->
    <script>
        try {

            $(function() {

            });

        } catch (error) {
            console.error("Your javascript has an error: " + error);
        }
    </script>
</head>

<body>
    <!-- Home -->
    <div data-role="page" id="page1">
        <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="c" href="menu.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Main Menu
                        </a>
            <h3>
                Your details
            </h3>
        </div>
        <form name="form1" method="post" action="details1.php">
            <strong>Details</strong>
            <br />
            <br /> Name: <input type="text" name "Name" />
            <br /> Address: <input type="text" name="Address" />
            <br />
            <input type="submit" name="Submit" value="Book appointment" />
            <br />
            <input type="submit" name="Submit" value="Cancel appointment" />
        </form>

    </div>
</body>

</html>

登录.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>
    </title>
    <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="my.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
    </script>
    <script src="my.js">
    </script>
    <!-- User-generated css -->
    <style>
    </style>
    <!-- User-generated js -->
    <script>
        try {

            $(function() {

            });

        } catch (error) {
            console.error("Your javascript has an error: " + error);
        }
    </script>
</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
            </h3>
        </div>
        <div data-role="content">
            <h4>
                Enter login details below:
            </h4>

            <form name="form1" method="post" action="login.php">
                <strong>Patient Login </strong>
                <br />
                <br /> Username: <input name="username" type="text" id="username" />
                <br /> Password: <input name="password" type="password" id="password" />
                <br />
                <br />
                <br />
                <input type="submit" name="Submit" value="Login" />
            </form>
            <br />
            <div data-role="content">
                <h4>
                    Please ensure your username and password are kept secure.
                </h4>

                <br />
            </div>
        </div>
</body>

</html>
4

4 回答 4

3

尝试使用该header()功能:

if ($num == 1)

{

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

}

显然将http://www.example.com更改为您选择的位置并将其放在页面中的任何 HTML 上方,否则您将收到 headers already set 错误。

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

    <?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 注入攻击

于 2013-03-27T22:40:33.507 回答
3

您所要做的就是更改标题并退出脚本:

header("Location: http://www.example.com");
die();
于 2013-03-27T23:20:49.873 回答
2

只需使用

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>
于 2013-03-27T22:42:48.187 回答
2

你可以使用 JavaScript

   <script type="text/javascript">window.location = 'PathToYourPage.extension'</script>
于 2013-03-27T22:44:14.713 回答