2013-03-27 106 views
3

如何將PHP頁面重定向到另一個頁面?重定向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頁面。

在此先感謝!

的login.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> 

Details1.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> 

details.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> 

的login.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> 
+0

複製, (HTTP://計算器。com/q/768431/425313) – 2013-03-27 22:45:27

回答

3

嘗試使用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 injection attacks的類型的東西。

+0

在OP的情況下不起作用; PHP代碼被埋葬的HTML標籤裏面,內容已經發送到瀏覽器 – thaJeztah 2013-03-27 22:41:50

+0

請記住,你寫艾米HTML或echo'ed數據頁面 – 2013-03-27 22:42:09

+0

真之前,你只能用這個,只是移到高於所有代碼HTML,編輯了答案來反映。 – 2013-03-27 22:43:05

2

只需使用

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

請注意,您只能設置一個頭重定向,如果沒有之前發送輸出。沒有單詞,沒有html語法,甚至沒有簡單的空白。

更多細節:header function

所以,你必須把你的腳本頂端的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> 
+0

如果在重定向header()後不包含'exit;',頁面上的其餘代碼仍然會被執行。 – Tigger 2013-03-27 23:19:41

+0

我出事了。在php之後,我是否會開始會議?它也說未定義。標題是否在頂部。對不起,我是新來的PHP。 – user2216325 2013-03-27 23:25:52

1

您coud使用JavaScript

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

所有你需要做的就是改變標題和退出腳本:?如何使PHP中的重定向]

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

這個頭文件在php頁面上? – user2216325 2013-03-27 23:33:08

+0

它可以去你想要的地方。它會停止頁面的其餘部分(在這種情況下使用die())並將用戶重定向到函數中指定的位置。確保在調用函數之前沒有HTML輸出。您可以在頁面頂部放置一個ob_start()函數來解決任何錯誤消息,說明「頭文件已經發送」 – 2013-03-28 00:18:31