Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display message before redirect to other page

Tags:

I have a simple question but yet i don't know. I would like to display message first before redirect to other page, but my code just directly redirect the page without display the message, or the display time is too short am not sure.. my code as below.. pls advise. thank much.

echo "Please Log In First";
header("location: login6.php");
like image 323
user2649074 Avatar asked Aug 19 '13 01:08

user2649074


People also ask

How do I get alert messages before redirecting a page?

RegisterStartupScript(this, this. GetType(), "alert", "<script> alert('User details saved sucessfully');window. open('frmDisplayUsers. aspx');</script>", true);

Can PHP redirect to another page?

In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous section.

Can you redirect a page to another page using JavaScript How?

It is quite simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows.


2 Answers

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>";
like image 153
Sunry Avatar answered Oct 22 '22 09:10

Sunry


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
like image 42
Konsole Avatar answered Oct 22 '22 08:10

Konsole