----- Original Message ----- 
From: "James Keeline"
> --- 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.

Hi James, thanks for replying.
I've just done a little test on the server, and the session *is* getting passed 
in the include file.
<?php
session_start();
include ('search.php');
?>

:search.php:
<?php
if (isset($_SESSION['search']))
{
  $search = $_SESSION['search'];
  echo "<p>$search</p>";
}
echo "<p><a href='index.php'>Pass a SESSION variable</a></p>";
$_SESSION['search'] = "Hello";
?>

It appeared that the session wasn't getting passed in the include file (no 
errors).
When I moved it to the main file, it did work.
I must have gone wrong somewhere else. I'll check my code again.
Brain overload, sorry.
Regards, Bob.


Reply via email to