--- Bob <[EMAIL PROTECTED]> wrote:

> Should a session variable work, if used in an include file?
> i.e. I set session_start(); in the first line of the main page.
> 
> In the include file, I check if the session variable is set.
> if (isset($_SESSION['search']))
> {
>   $search = $_SESSION['search'];
> }
> then check for normal characters.
> 
> In the last line of the include file, I pass the session back.
> $_SESSION['search'] = $search;
> 
> This works locally on my computer, but not on the server.
> I've moved the session variables to the main page now, and it works.
> 
> Is this normal for sessions not to work in included pages, or is my server
> setup differently? $_POST and $_GET work in included files, so why shouldn't 
> $_SESSION
> Thanks, Bob.

An include() or require() works as if you pasted the code in at that point at
runtime.

The same rules apply to session_start() or any other function which writes to
the HTTP headers (like set_cookie()).  These functions must be called ~before~
any dynamic or static output is sent to the browser.  The usual way to handle
this is to start the opening PHP tag on the first line and first character
position and to make any session_start() or set_cookie() calls before any print
or echo statements.  Even a blank line or stray character before the opening
PHP tag can lead to the dreadded "headers already sent" errors.

You need to be more specific when you say "don't work".  Are you seeing an
error message?  If so, which one?  What are the actual symptoms.  Otherwise we
are playing guessing games and writing about potential issues which may not
relate to your actual problem.

James

Reply via email to