You can't do it that way. PHP header must sent out before any content, so the correct order is:
header("location: login6.php");
echo "Please Log In First";
But these codes will redirect instantly and wouldn't let you see the content. So I would do the redirection by JavaScript:
echo "Please Log In First";
echo "<script>setTimeout(\"location.href = 'http://www.example.com';\",1500);</script>";
Answer from Sunry on Stack OverflowYou can't do it that way. PHP header must sent out before any content, so the correct order is:
header("location: login6.php");
echo "Please Log In First";
But these codes will redirect instantly and wouldn't let you see the content. So I would do the redirection by JavaScript:
echo "Please Log In First";
echo "<script>setTimeout(\"location.href = 'http://www.example.com';\",1500);</script>";
You can use header refresh. It will wait for specified time before redirecting. You can display your message then.
header( "refresh:5; url=login.php" ); //wait for 5 seconds before redirecting
Display thank you message and redirect to another page
asp.net mvc - How to display message before redirecting to another page? - Stack Overflow
How to display message before redirect
display a message before (or after) a redirect javascript - Stack Overflow
You could create a splash page that gets displayed above the current content and then use a timeout to redirect the user.
Edit: Moved the text to the CSS rule. If you do not want this, add the text inside the <div class="thanks"> tag and remove the :after CSS rule.
function redirect() {
document.querySelector('.thanks').classList.remove('thanks-hidden');
setTimeout(() => {
window.location.href = 'https://www.w3schools.com/';
}, 3000); // Wait 3 seconds and then redirect.
}
.thanks {
display: flex;
position: absolute;
top: 0;
left: 0;
z-index: 999;
width: 100%;
height: 100%;
background: #FFF;
justify-content: center;
align-items: center;
font-size: 2em;
}
.thanks:after {
content: "Thanks";
}
.thanks-hidden {
display: none;
}
<input type="button" onclick="redirect()" value="Submit">
<div class="thanks thanks-hidden"></div>
You can use setTimeout
function two() {
alert('Thanks');
setTimeout(function() {
window.location.href = 'https://www.w3schools.com';
}, 4000 /* ms to delay for*/);
}
Header( 'Location: Database.php?success=1' );
And in the Database.php page :
if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
// treat the succes case ex:
echo "Success";
}
Store it in the session as sort of a "flash"-message:
$_SESSION['message'] = 'success';
and show it in Database.php after the redirect. Also delete its content after displaying it:
print $_SESSION['message'];
$_SESSION['message'] = null;
The advantage of this is, that the message won't be shown again every time the user refreshes the page.
You may use Session, QueryString, Cookie for this purpose.
But I would suggest show you message on same Register page. And put continue button on page, that will redirect user to Home or other page.
Why I`m saying show message on same page, because one thing is fixed you are going to redirect user if registration was successful, if not you will show the errors.
Session, QueryString will be easy and handy option for this:
Session["RegisterMessage"] = "Hello User, You have successfully registered";
Or
Response.Redirect("Home.aspx?msg=Hello User, You have successfully registered");
You may show this value on label on the page.
Use Query String to pass the Message to Login Page and show with the help of Label.
See this ..
Server code (PHP -> redirect) is fired up before Client Code (JS -> alert). There are several solutions to this but the easiest one is to use one type of code for the functionality (either PHP or JS) ..
Let's use PHP to fire up JS for now, since that's what you're using ..
echo "<script>alert('Duplicate Users') ; window.location.href = 'auth/login'</script>"
<script>
alert("Duplicate Users");
window.location.href = "auth/login";
</script>