PHP Header Syntax Error

Taz

Seasoned Veteran
Joined
Mar 1, 2009
Messages
3,652
Reaction score
2
FP$
1,032
I am getting this error...
Parse error: syntax error, unexpected 'header'

Code:
header("location:login_success.php");

That's the code, what am I doing wrong?
 
Where is it being called from? Are there any lines before this statement in the file which might produce any output?
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. - Source
 
I would guess you have a syntax error f some kind. Did you forget the semicolon on the line before? The problem is almost certainly with the line before the one you quoted. If it were the issue Scarface mentioned the error would probably be "Cannot modify header information."
 
Simple. You need to add a space in between the location and the actually URL.

So...

Code:
header("Location: login_success.php");
 
Code:
header("location:login_success.php");
should be
Code:
header("location: login_success.php");
 
That wouldn't produce a syntax error, though. If the message he gave was right, then it means that the error occurred before that line, because the PHP parser did not expect the term "header" when it was parsing the code.

Example:

Code:
header("location:test.php");
Is valid PHP, and actually redirects properly in Google Chrome (latest version).

Code:
test
header("location:test.php");

Produces the error "Parse error: syntax error, unexpected 'header' (T_STRING) in /Applications/MAMP/htdocs/project/index.php on line 12" which is exactly what he got.
 
Can you please post whatever you have before
Code:
header("location:login_success.php");

It's always nice to give us some context. Sometimes the error could be on a different line or something on a different line is leading to the error on the current line. Either way, either link us to the faulty page or give us some more of the code, please 🙂
 
I am going to go ahead and archive this since there hasn't been a reply for close to a month now. Feel free to create a new topic or let a CT member know you would like this reopened. 🙂

Thanks! 😀
 
Back
Top Bottom