Redirecting PHP
This question already has an answer here:
Try using the header()
function as so:
if ($num == 1)
{
header("Location: http://www.example.com");
}
Obviously change http://www.example.com to your location of choice and place it above any HTML in your page otherwise you will get a headers already set error.
With the above in mind you would need to re-arrange your code as follows:
<?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>`
In addition I would urge you to look at PDO or mysqli and prepared statements with bound values before proceeding with this type of thing as you are opening yourself up to SQL injection attacks .
您只需更改标题并退出脚本:
header("Location: http://www.example.com");
die();
Just use
header("Location: http://www.test.com");
Please note, that you can only set a header-redirect, if there is no output sent before. No single word, no html syntax and not even a simple whitespace.
More details: header function
So you have to put the PHP-part on the top of your script and store the success/error-messages in a variable instead of a direct "echo".
Example: Please note the variable "$return_message" in your if-clauses. There is no "echo" anymore, but in the HTML-context you will find the echo for the output.
<?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/34700.html
上一篇: 在HTML页面加载之前重定向
下一篇: 重定向PHP