3

I used to use this function to redirect from one php page to another:

header( 'Location: student.php?cnp='.$_REQUEST['name']) ;

In my localhost it does work, but if testing it in internet, it doesn't redirect. I've also tried to give the full path (like http://.../student.php?...) but still it doesn't work. Does anyone know why and what should I do?

12
  • are you receiving any error messages? perhaps "Cannot modify header information - header already sent by ..." ??? Commented Jan 2, 2012 at 17:49
  • no...it just remains on the same page! Commented Jan 2, 2012 at 17:52
  • 3
    Check your error log. Commented Jan 2, 2012 at 17:55
  • 1
    are you exiting from the script immediately after the header statement? Commented Jan 2, 2012 at 17:55
  • 1
    @madrugada: read the error message. you cannot do ANY output before calling header(), or you'll get the warning and failed headers. Commented Jan 2, 2012 at 18:03

6 Answers 6

6

Try this:

header('Location: http://www.example.com/someurl.php', true);

Second parameter replaces previous location header (if any).

Also check what HTTP response code you receive (it should be 30X)

If nothing helps you can always redirect by javascript:

echo "<script>window.top.location='http://www.example.com/someurl.php'</script>"

This is not so professional but works even if headers has already been sent.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all, it worked. It's strange but after looking here[1] i saw there should not be any blank space before <?php and after ?> [1]forum.mamboserver.com/showthread.php?t=49427
I know it's an old question but as I came here I assume others will too. I had the same problem but the provided solutions didn't help. After an hour of searching I've found out that a colleague modified the file with a different editor that changed encoding to 'UTF8 with BOM'. Sending a location header with index.php in this encoding wasn't possible anymore. After converting it back to UTF8 (without BOM) everything works as before. I hope this information helps others saving time.
3

Try adding session_write_close(); before and exit() after:

session_write_close();

header( 'Location: student.php?cnp='.$_REQUEST['name']) ;

exit();

Just saw the error message in the comments. Add ob_start(); to the top of your pages, under the session_start() if you have that.

2 Comments

i dont have any session...shall i still add what you said?
Yes you should still add it. Put it at the top of your main file.
0

I tried this solution and it didnt work

header('Location: http://www.example.com/someurl.php', true);

So I suggest people to go with these two possible below solutions which worked for me.

using JS:

echo "<script>window.top.location='http://www.example.com/someurl.php'</script>"

using META:

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.example.com/someurl.php">';

These two both worked for me. But for some reason header thing doesnt work on the net but works locally perfectly fine. I am gonna be using meta solution.

Comments

0

Your header() is not working because the header was already sent to the browser. Use ob_start() at the beginning of your page (even before the DOCTYPE declaration). And at the end, use ob_end_flush()

Comments

0

Try This www.psychocodes.in

<?php
ob_start();

//more code
header("Location:URL");

ob_end_flush();
?>

Comments

0

I just had the same problem this advice worked for me. Place this function at the top of your code:

function move_to(){
header('Location: student.php?cnp='.$_REQUEST['name']);
}

and call that function anywhere with:

move_to()

Btw, I'm not gonna take credit for it. I just found it at: Header function not working PHP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.