1

I cannot figure out what could be causing this error. My Apache log is not recording any errors in the access log or error log regarding the page, yet somehow whenever I uncomment the header() line I get a 500 Internal Server Error. It can't be coming from PHP's fatal error when content is outputted before header() is called, that wouldn't cause a 500 would it?

header("Location: /offices/page-".ceil($cache->size() / 15));

I tested $cache->size() and it's returning 22, so it should append a 2 to the end of the string and redirect. I did have ob_start() called before the header() but I tried calling ob_end_clean() right before it and it still did nothing...

I even tried putting header("Location: /offices"); at the very beginning of the file and it still gives me a 500.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
animuson
  • 53,861
  • 28
  • 137
  • 147
  • reason might be - you need to remove space after location : header("location :/ –  Apr 13 '20 at 20:57

6 Answers6

8

Another reason might be - you need to remove space after location :

header("location :
header("location:

If that's not your problem, then try to check the php & apache error logs.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
4

Try to use a fully formed URL:

header("Location: http://{$_SERVER['HTTP_HOST']}/offices/page-".ceil($cache->size() / 15));
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 2
    I tried putting in `header("Location: http://www.example.com");` still 500 error. – animuson Mar 01 '10 at 05:28
  • @animuson Ok, that's weird. :) – deceze Mar 01 '10 at 05:31
  • Whoever it was that said put `exit` immediately after it, seems to have worked. I put `exit` right after the `header` call and it's redirecting just fine now... – animuson Mar 01 '10 at 05:32
  • Were you possibly outputting any other headers after this one? – deceze Mar 01 '10 at 05:33
  • No, the original `header` call that I was using was fairly near the end of the script execution. It would not have been possible for another `header` call to be sent that late in the script. All, if any, of them are sent right at the beginning of script execution. – animuson Mar 01 '10 at 05:38
4

The script continues to execute after your header() call. You need put exit; immediately after it. This shouldn't cause a 500 error, though.

Edit: Evidently this worked - meaning your problem is probably in some related code further down the page?

jasonbar
  • 13,333
  • 4
  • 38
  • 46
1

I had the same thing going on. After repeatedly banging my head on the monitor, it isn't the header("Location: myPage.php"); line that is broken - it is myPage.php that is broken! Try redirecting to another page. I hope I save someone else time

RaininDown
  • 11
  • 1
1

I literally had the same problem,but it seems that I wrote header("Location : page.php")instead of header("Location: page.php");. YES that one space makes a difference!

-1

In my case, the culprit was a pesky extra comma in the function's arguments.
It worked fine locally but not when deployed. Here it was:

function myFunction ($a,$b,$c, <- this comma){
 foreach(etc) {
   ...
   return header($x);
};