2

I am trying to redirect my page after successful user login using php header,But i am unable to redirect the page.

echo '<span style="color:#E02F2F;text-align:center;font-size:25px;padding-left:38%;">Username or Password is valid</span>';     
header('Location:http://example.com/templates/georamble/index.php'); // Redirecting To Other Page

my login page is inside the "georamble" folder. where i am wrong?

Amit Horakeri
  • 747
  • 5
  • 18
Nayana
  • 747
  • 8
  • 27
  • did you try keeping an `exit;` after header – akashBhardwaj Oct 14 '15 at 10:50
  • Other page of the same project? – Tushar Gupta Oct 14 '15 at 10:50
  • 5
    `echo` + `header` doesn't work. If you've already sent output to the client, your headers have been sent and you can't alter them anymore. Either use output buffering, or set your headers before you send output. If you want/need a _"successful login"_ message to be displayed, you can do so on the page you're redirecting to, and by using a session to keep track of things like that. [Read all about it here](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987#8028987) – Elias Van Ootegem Oct 14 '15 at 10:52
  • 1
    Possible duplicate of [PHP Redirect to another page after form submit](http://stackoverflow.com/questions/17157685/php-redirect-to-another-page-after-form-submit) – Chris Oct 14 '15 at 11:00
  • What do you get is there any error outputs? If there is update your question. – André Ferraz Oct 14 '15 at 12:07

3 Answers3

2

If echo statement use before header so not redirect page. so for that you can also try this. echo <script>window.location='url'</script>

Hussy Borad
  • 626
  • 5
  • 20
  • It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem): – Hussy Borad Oct 14 '15 at 11:05
  • above is not proper solution. but with use this solved your issue. – Hussy Borad Oct 14 '15 at 11:33
0

You are probably getting an error, because you are printing information before redirecting. Your user will never see that information.

A similar question and answer.

So, after your script executes successfully implying login then pass parameters in your header redirect if you want to display some type of information. Thought since you are just echoing valid login, I don't think it is needed. You had it right, just can't print anything to the screen before a redirect.

Community
  • 1
  • 1
Chris
  • 1,091
  • 11
  • 32
0

You cannot use, echo before header. To overcome this situation, you can make use of ob_start() and ob_end_flush() functions as below:

ob_start();
echo '<span style="color:#E02F2F;text-align:center;font-size:25px;padding-left:38%;">Username or Password is valid</span>';
header('Location:http://example.com/templates/georamble/index.php'); // Redirecting To Other 
ob_end_flush();

Hope this helps you out.

Amit Horakeri
  • 747
  • 5
  • 18