Pradip
Pradip

Reputation: 1327

Space before PHP tag

I have written an application in PHP. It includes
1: login form
2: Welcome page
3: Logout form which is redirected to login form.

My register_global =On in php.ini.

My application has working properly on LAMP and WAMP. But it has not setting session on live server.

But, after checking by line by line, in session setting file, there was single space before starting '<?PHP ...... ?>'. After removing that space my application has working properly on live server.

What can be the reason behind that space before the ‘<?PHP ...... ?>' by which session is not setting?

Upvotes: 1

Views: 2526

Answers (2)

El Yobo
El Yobo

Reputation: 14946

You can't send any content before sending the response headers; the session is sent in the response headers. Because you sent a space beforehand, you are therefore unable to start a session.

From the documentation on session_start, "To use cookie-based sessions, session_start() must be called before outputing anything to the browser.". This includes the single space at the start.

Basically, the first thing in any script should always be <?php

Upvotes: 1

Bart van Heukelom
Bart van Heukelom

Reputation: 44104

The space is output, i.e. sent to the browser. For opening a session you need to send a so-called HTTP header, which you need to do before anything is output. That's why it didn't work. Your error message probably was "headers already sent".

Upvotes: 5

Related Questions