I have a register.php, login.php, and main.php. How do i redirect user after after registration submit to login page and then login page submit to main page.
I generally add `die("Redirecting...");` after the header, just to make sure nothing else is outputted.
webdestroya
2010-04-30 02:09:03
@webdestroya - Isn't that a bit misleading, on account of if the user can see that, the redirect has failed?
anonymous coward
2010-04-30 03:52:10
The user rarely sees it, and if they do it just means their browser is slow, not that the redirect has failed.
webdestroya
2010-04-30 03:53:44
@webdestroya I don't think it means their browser is slow? More like it is refusing to follow `Location` headers. Perhaps I'm wrong though.
alex
2010-04-30 04:04:30
Maybe, but the probability that a browser will not respect the Location header is incredibly low.
webdestroya
2010-04-30 04:15:16
use `ob_start()` at the top and `ob_end_flush()` at the bottom of each php page where u use `header()` in between of page i.e if `header()` is called after any actual output is sent
diEcho
2010-04-30 05:58:10
+7
A:
header("Location: /login.php");
exit;
See the exit. Don't ever forget to do this. If you have sensitive data after this, it will be visible to anyone that doesn't follow location headers (such as some bots).
To stop you from forgetting, you could build a wrapper type function
function redirect($url) {
header('Location: ' . $url);
exit;
}
alex
2010-04-30 02:12:45
I'm not sure that Google wouldn't follow that, but I may be wrong. They follow 301's and 302's just fine... what's the default Status Code sent along with PHP via Location?
anonymous coward
2010-04-30 03:53:58
@anonymous coward I read a 'Daily WTF' recently where they had pages deleted from a custom CMS because the Internet Archive bot didn't follow the `Location` headers. I mixed up Google bot with them.
alex
2010-04-30 03:59:30
Well someone might have something like `if ( ! $loggedIn) { header('Location: login.php'); echo 'you must be logged in' }`. Not saying that is best practice but I'm sure some people may do it.
alex
2010-05-02 09:09:26
A:
Header("Location:login.php");
or:
echo "<meta http-equiv=refresh content='0; url=login.php'>";
or:
echo "<script language='javascript'>";
echo "location='login.php';";
echo "</script>";
Never count on Javascript to perform a mission critical redirect.
anonymous coward
2010-04-30 03:50:53
A:
You can do this:
func.php
<?php
function EmptyStr($value){
$str = strval($value);
$str = str_replace(" ", "", $str);
return (trim($str) == "");
}
function redirect($url) {
header('Location: ' . $url);
exit;
}
?>
register.php
<?php
include("func.php");
$username = $_POST["username];
$password = $_POST["password"];
$email = $_POST["email"];
if(!EmptyStr($username) && !EmptyStr($password) && !EmptyStr($email)){
sql = WRITE YOUR SQL SYNTAX HERE TO INSERT THE INPUT TO DB
redirect("/login.php");
}else{
$_SESSION["ErrMsg"] = "Error! All fields are required.";
}
echo $_SESSION["ErrMsg"];
$_SESSION["ErrMsg"] = "";
?>
Put your html regsitration form here
login.php
<?php
include("func.php");
$username = $_POST["username];
$password = $_POST["password"];
if(!EmptyStr($username) && !EmptyStr($password)){
sql = WRITE YOUR SQL SYNTAX HERE TO QUERY THE USERNAME AND PASSWORD TO DB
if ($rs && !$rs->EOF) { //user found and pass match
$_SESSION["username"] = $username;
redirect("/main.php");
}else{
$_SESSION["ErrMsg"] = "Invalid username or passsword!";
}
}else{
$_SESSION["ErrMsg"] = "Error! All fields are required.";
}
echo $_SESSION["ErrMsg"];
$_SESSION["ErrMsg"] = "";
?>
Put your html login form here
main.php
<?php
include("func.php");
if(EmptyStr($_SESSION["username"])){ //check if user has session (logged in)
$_SESSION["ErrMsg"] = "You need to logged in first to view this page!";
redirect("/login.php");
}
?>
Sofyan
2010-04-30 05:55:03