PHP header location not working on X10hosting

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
I am trying to use the php header("Location:url") to redirect a user to a login page if they have not logged in yet. It works fine on my local copy but on X10 the redirect does not work. Is there some setting which does not allow PHP header or something on the X10 servers?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Anything that plays with the HTTP headers has to come first in the file (that is, no text or HTML can be sent before the headers). There can be no data -- not even white space -- before the opening <?php tag, and you can't echo anything before setting the headers.

You may have a problem with a Byte Order Mark in your file -- an invisible, unprintable character that will appear as the first two bytes of a Unicode text file to tell the system whether the file uses Big-endian or Little-endian byte orders. If you are suppressing errors, you will not be told about the "headers already sent" error, but any custom headers you try to send will not be sent.
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
Paste your script. It's a bug in the script - header location works fine for me.
 

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
I'm pretty sure it is what essellar said, although, I have the header on an included file so the header functions way down the line in the preproccessor when the page loads. Is there a workaround for this or a different way to work this?
 

ellescuba27

Member
Messages
273
Reaction score
3
Points
18
No, headers must be before any html.

Let me explain: When a computer goes to a website, it doesn't know what's on it, so it asks your server (which is what you get when you sign up for x10) "Hey, what's on this site?" And the server replies "Here's what's on the site!" So for example, when going to Gmail, the computer asks a Gmail server "Hey, what's on this site?" and the server answers "Ok, well here's the compose button, and here's the settings" and so on and so forth. All of this is done before the page loads. PHP is a special language. It changes what the server says to the computer. So maybe, when the computer asks "Hey, what's on this site?" the server might check if a user is logged in or not. If he is logged in, it will say "This button should be green" but if he's not, the server says "This button should be Red". That's what PHP does.

But what if your computer wants to go to another page, before the server tells you what's on this one? There is only ONE WAY that the computer can tell the server what to do. In order for the computer to tell the server what to do, the computer must use a header. A header is ALWAYS placed before html, because otherwise, the server will have already started telling the computer what to do, and the computer won't have a chance to tell the server what to do. So a proper header should ALWAYS be before html. So should cookies and sessions, because they both require a header to work. In this case, you are using a header, so it should be before the <html> tag.

Hope that solves your problem.
 

sikuneh

New Member
Messages
55
Reaction score
0
Points
0
Yes, I understand that part. I suppose there's no workaround in the way I described. But I think the way I'll do it is to have the link send me to a page that isn't included, then redirect it to the included pages based on the if statement on the top. Thanks.

I do have a second question: why does it work on my WAMP server?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
You haven't even described what you mean by "doesn't work".

If you give a URL that shows the problem, we can see it first hand.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The reason why WAMP doesn't throw the error is because output buffering is enabled by default in the php.ini. You don't have access to the php.ini here (or in most shared hosting environments), but you can modify your scripts to include output buffering by adding the lines:

PHP:
<?php
ob_start();

//everything else goes here

ob_end_flush();
?>

to all of your "host" pages (not the included or required pages). That is not the optimal solution though -- it would be far better to turn off output buffering in your local php.ini. That will cause the "headers already sent" error to happen in your WAMP development/testing environment, which will then force you to write your scripts in a way that is easily deployable to any host.
 
Top