Hi all,

After moving my PHP-code from a Windows Server to a Linux one I have problems with the $_SESSION variables. When I login an click a menu option, all my $_SESSION variables get cleared and I have to login again.

Does anyone recognize this problem and have a solution for it ?
Can this be solved with an apache setting ??

When running the same code on the Windows server there were no problems!
This is my server configuration:
- Apache/2.2.11 (Ubuntu) DAV/2 SVN/1.5.4
- PHP/5.2.6-3ubuntu4.4 with Suhosin-Patch mod_ssl/2.2.11
- OpenSSL/0.9.8g

Thanks in advance,
Dirk

Do you have the following code at the start of your PHP code of every page ?

if (!session_id())
  session_start();

Do you have the following code at the start of your PHP code of every page ?

if (!session_id())
  session_start();

What do you mean by every page ? Is that every php-file, or just the ones that will actually generate a html-body

Every page that is not an included file only.

No, don't think it's included in every page. Is there an way to check if this is the actual problem ?
Still one question, why is it working without problems on the Windows Server ?? Couldn't it be an Apache setting ??

perhaps no write permissions on the session folder ?

Here are two short pieces of code which demonstrate my problem:

<?php
// file Name: page1.php
session_start();
echo "Welcome to page #1";
$_SESSION['favcolor'] = ‘green’;
$_SESSION['animal'] = ‘cat’;
$_SESSION['time'] = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">Test 1</a>';

$phpSessionId = session_id();
echo $phpSessionId;

// Or maybe pass along the session id, if needed
//echo ‘<br /><a href="page2.php?’ . SID . ‘">page 2</a>’;
echo '<br /><a href="page2.php?PHPSESSID=' . $phpSessionId . '">Test 2</a>';
?>

<?php
// page2.php
session_start();
$phpSessionId = session_id();
echo $phpSessionId."<br>";

echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo "<br>";
echo $_SESSION['animal']; // cat
echo "<br>";
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>

This is the output of page1.php:
Welcome to page #1
Test 1ee044e58209ee241706d7ab18c6bdbdc
Test 2

This is the output of the first link:
e7fac3fb965b1e73851860d73946a2b4
Welcome to page #2


1970 01 01 01:00:00
page 1

This is the output of the second link:
ee044e58209ee241706d7ab18c6bdbdc
Welcome to page #2
�green�
�cat�
2010 01 15 13:20:21
page 1


So the first link doesn't seem to have access to the $_SESSION variables. Any idea's ?

(The demo from the php site.) I think it may have to do with session.auto_start. See the first comment on the page where you got the example. Perhaps it was turned on on your windows machine, but off on your linux machine. The last comment may also point to your problem.

(The demo from the php site.) I think it may have to do with session.auto_start. See the first comment on the page where you got the example. Perhaps it was turned on on your windows machine, but off on your linux machine. The last comment may also point to your problem.

On both servers the auto_start was turned Off! Switched it on on Linux, but no luck!
I got the example from a forum so don't know what you mean by 'The last comment may also point to your problem.'

Appreciate your help!!

Sorry about the comment. Now I'm at home I can no longer find the one I meant earlier. I'm sorry, I don't know any other possible problems at this time.

Well first of all place the following at the top of every php file but not the files which are being included (eg not include.php).

<?php
session_start();

After that make sure you have cookies enabled. If you have cookies disabled then sessions won't work. Why? Sessions send a single cookie containing the session id to identify which computer the data belongs to and if you have cookies disabled your computer is unable to identify itself. Alternatively you can link to a page like the following without cookies and sessions might work.

echo '<a href="index.php?'.SID.'">test</a>';

So the main message - enable cookies and put session_start(); at the top of each non-include file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.