重定向PHP


Redirecting PHP

本问题已经有最佳答案,请猛点这里访问。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</head>

<body>
    <!-- Home -->
   
       
           
                        Back
                   
           
                     Home  
                   
           
                Login Process
           
       
       

            <?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";
        }
    }

    ?>
       
   
</body>

</html>

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

login.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?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" />
           
           
            <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 src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
           
            <script src="my.js">
           
            <!-- User-generated css -->
            <style>
            </style>
            <!-- User-generated js -->
           
                try {

        $(function() {

        });

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

    ?>
    </head>

    <body>
        <!-- Home -->
       
           
               
                        Back
                   
               
                     Home  
                   
               
                    Login Process
               
           
           

                <?php echo $return_message; ?>

           
       
    </body>

    </html>

Details1.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!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" />
       
       
        <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 src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
       
        <script src="my.js">
       
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
       
            try {

                $(function() {

                });

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

    <body>
        <!-- Home -->
       
           
               
                        Main Menu
                       
               
                    Your details
               


           
           
                Name:
                <?php echo $_SESSION['Name'];?>
                <br /> Address:
                <?php echo $_SESSION['Address'];?>



           
    </body>

    </html>

details.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!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" />
   
   
    <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 src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
   
    <script src="my.js">
   
    <!-- User-generated css -->
    <style>
    </style>
    <!-- User-generated js -->
   
        try {

            $(function() {

            });

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

<body>
    <!-- Home -->
   
       
           
                        Main Menu
                       
           
                Your details
           
       
        <form name="form1" method="post" action="details1.php">
            Details
            <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>

   
</body>

</html>

login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!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" />
   
   
    <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 src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
   
    <script src="my.js">
   
    <!-- User-generated css -->
    <style>
    </style>
    <!-- User-generated js -->
   
        try {

            $(function() {

            });

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

<body>
    <!-- Home -->
   
       
           
                        Back
                   
           
                     Home  
                   
           
                Login
           
       
       
            <h4>
                Enter login details below:
            </h4>

            <form name="form1" method="post" action="login.php">
                Patient Login
                <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 />
           
                <h4>
                    Please ensure your username and password are kept secure.
                </h4>

                <br />
           
       
</body>

</html>


您所要做的就是更改头部并退出脚本:

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


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

1
2
3
4
5
6
7
if ($num == 1)

{

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

}

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    <?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 -->
       
           
           
                    Back
               
               
                 Home  
               
               
                    Login Process
               
           
           


<?php  echo $error; ?>

           
       
    </body>
</html>`

此外,我还建议您在继续进行这种类型的操作之前,先查看pdo或mysqli,并准备好带有绑定值的语句,因为您正在向SQL注入攻击敞开大门。


你可以使用javascript

1
   <script type="text/javascript">window.location = 'PathToYourPage.extension'

只使用

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

请注意,如果之前没有发送输出,则只能设置头重定向。没有单个单词,没有HTML语法,甚至没有简单的空白。

更多详细信息:标题功能

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

示例:请注意if子句中的变量"$return_message"。不再有"echo",但是在HTML上下文中,您将找到输出的echo。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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 -->
       
           
           
                    Back
               
               
                 Home  
               
               
                    Login Process
               
           
           

<?php echo $return_message; ?>

           
       
    </body>
</html>