3

This is my code on page1:

session_start();
if(isset($_SESSION[$login])){
    header("Refresh: 5; location:page2.php");
    echo "Welcome back!";
}

I thought this was the way to have a redirect on page2 AFTER 5 seconds of pause, in which the user can see the message "Welcome Back". Am I wrong?

The result of my code is a correct identification of the login, with the consequent showing of echo "Welcome Back", but the redirect isn't happening. I remain in page1 forever.

Where is the error?

ps: I made a search of similar threads, but I hadn't be able to solve the problem, so I'm posting here guys, thank you!

noobsharp
  • 867
  • 2
  • 7
  • 15
  • http://forums.digitalpoint.com/showthread.php?t=415725 – Javascript Coder May 23 '12 at 12:05
  • 2
    Don't use delayed redirects. Either a message is important enough to display to the user until they have read it and clicked, or it isn't important enough to delay the user for X seconds (during which time they may or may not be looking at the screen). – Quentin May 23 '12 at 12:06
  • I understand what you say, but it doesn't work even if I cut the "Refresh". This means I'm making a mistake with the header function, but I don't get where... – noobsharp May 23 '12 at 12:09
  • 1
    Hey guys, I found the error! I am a donkey -.- It's !isset , with the "!" cause it's negate. ALL DONE, thank you very much to you all! – noobsharp May 23 '12 at 12:33

5 Answers5

1

I believe you need to use url= rather than location:

header("Refresh: 5; url=page2.php");

Unfortunately, this is a non-standard way of doing redirection (and not a great one at that), so I have been unable to find any documentation for it.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • http://stackoverflow.com/questions/18305258/display-message-before-redirect-to-other-page#comment35908148_18305289 led me to http://stackoverflow.com/questions/283752/refresh-http-header which says that as well as not being standard the refresh header also causes performance issues in Internet Explorer. – Edward Apr 06 '16 at 19:03
0

Try using JavaScript to do this.

Here is a nice tutorial on redirecting with javascript: http://www.tizag.com/javascriptT/javascriptredirect.php

ONOZ
  • 1,390
  • 2
  • 11
  • 19
0

This could be due to the fact that you are using "location" instead of "url".

Try this: header("Refresh: 5; URL=page2.php");

"location" is used in Javascript to perform a redirect.

kristof_w
  • 302
  • 2
  • 11
0
session_start();    
if(isset($_SESSION[$login])){    
   header("Refresh: 5;page2.php");    
   echo "Welcome back!";     
}

This is the code I checked out. In page2.php you should use the relative path.

bummi
  • 27,123
  • 14
  • 62
  • 101
MuteX
  • 183
  • 1
  • 1
  • 13
  • Add brief explanation of what have been changed and why. – Ziumin Dec 29 '14 at 09:37
  • If we use url or location before the page name then it goes for the url as i wrote here, but if we use just the path then it goes for the page what i write here. – MuteX Dec 29 '14 at 09:46
-2

use php's sleep function. this should get the job done:

session_start();
if(isset($_SESSION[$login])){
sleep(5);
header("location:page2.php");
echo "Welcome back!";
}
DeltaTango
  • 821
  • 2
  • 9
  • 19
  • Don't. The page load time will be 5 seconds, and the redirect will immediately happen after the page loads. Be very careful when using sleep()! – ONOZ May 23 '12 at 12:02