Redirect to page after true result
Hello thanks for your time. I have a file called get getresult.php it checks for username and password on mysql server for users that login.
I j开发者_如何学编程ust it to redirect to a page index.php if login is successful. Thanks for any help the can be provided.
<?php
// version 1.1
if (isset($_POST['password'])) // if the password is set then the form has been submitted on login.php page
{
include("config.php");
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$qstr = "SELECT * from members where username ='$username' and password ='$password'";
$result = mysql_query($qstr);
if (mysql_num_rows($result)) echo "<font color=#008000><Center><b>**Successful Login**</b></Center></font>"
**//// I thought maybe this would be the section for the redirection code****
;
else echo "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
mysql_close();
}
else echo "<font color=#ff8000><Center><b>**No login attempted**</b></Center></font>";
?>
after the check of login do this:
header("Location: newPage.php"); // replace newpage with whatever you want
UPDATE
put the header BEFORE any echoing:
header("Location: newPage.php"); // replace newpage with whatever you want
if (mysql_num_rows($result)) echo "<font color=#008000><Center><b>**Successful Login**</b></Center></font>"
You have to make sure that you don't echo or print anything before, and then you can do:
header("Location: http://www.yoursite.com");
Alternatively, if you want to redirect after having printed stuff, I usually have a function that prints javascript
function refresh_to( $location = 'index.php'){
$output = '<script> window.location.href = '.$location.'; </script>';
return $output;
}
And call it out afterwards.
header("Location: /my/new/location");
header("refresh:5;url=http://google.com");
// version 1.1
if ( isset($_POST['password']) ) { // if the password is set then the form has been submitted on login.php page
include("config.php");
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$qstr = "SELECT * from members where username ='$username' and password ='$password'";
$result = mysql_query($qstr);
if (mysql_num_rows($result)) {
$return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
} else {
$return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
}
mysql_close();
} else {
$return "<font color=#ff8000><Center><b>**No login attempted**</b></Center></font>";
}
// Do the output
print($return);
Remember separate Logic and Output, this will be good practice for your coding.
Notice i use header
and then do print
, also in the header
i put refresh
5 (seconds), this will give time for your visitor to read the notice you give (what the point, if you give notice but they didn't have time to read it.)
精彩评论