I am building a website which includes a login page. I need to redirect the user to their profile page once they\'ve logged in successfully, but I don\'t know how to do that
You could use a function similar to:
function redirect($url) {
ob_start();
header('Location: '.$url);
ob_end_flush();
die();
}
Worth noting, you should always use either ob_flush()
or ob_start()
at the beginning of your header('location: ...');
functions, and you should always follow them with a die()
or exit()
function to prevent further code execution.
Here's a more detailed guide than any of the other answers have mentioned: http://www.exchangecore.com/blog/how-redirect-using-php/
This guide includes reasons for using die()
/ exit()
functions in your redirects, as well as when to use ob_flush()
vs ob_start()
, and some potential errors that the others answers have left out at this point.
On click BUTTON action
if(isset($_POST['save_btn']))
{
//write some of your code here, if necessary
echo'<script> window.location="B.php"; </script> ';
}
----------
<?php
echo '<div style="text-align:center;padding-top:200px;">Go New Page</div>';
$gourl='http://stackoverflow.com';
echo '<META HTTP-EQUIV="Refresh" Content="2; URL='.$gourl.'">';
exit;
?>
----------
You can conditionally redirect to some page within a php file....
if (/*Condition to redirect*/){
//You need to redirect
header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
exit();
}
else{
// do some
}
You could use ob_start();
before you send any output. This will tell to PHP to keep all the output in a buffer until the script execution ends, so you still can change the header.
Usually I don't use output buffering, for simple projects I keep all the logic on the first part of my script, then I output all HTML
.
Just like you used echo to print a webpage. You could use also do the same with redirecting.
print("<script type=\"text/javascript\">location.href=\"urlHere\"</script>")