Hi all.

I am having a weird problem with PHP sessions. I create a session but when I click a link inside my site, other session is created. To test it I copy this code below and tried to run in same environment (WAMP at localhost). The same behavior was presented. Sometimes, clicking at that link works, but almost every time a new session is created.

What do you suggest me to do, to solve this?

Thank you very much.

<?php
session_start();
if (!isset($_SESSION['test'])) {
  echo "First activation: setting session variable";
  $_SESSION['test'] = 1;
} else {
  echo "SESSIONS ARE WORKING! activation: ", (++$_SESSION['test']);
?>
<br><a href="http://localhost:8081/testesession.php">Again</a>
<?php
}
echo "<br>" . session_id();
?>

Thank you for answer.

I tried to set a session name, right now, but the behavior is the same.

First time I access this page, session is created. Any CTRL-R to refresh page access the same session. Clicking at address bar and hitting ENTER give the same session too.

If I click at that link, sometimes it gets the same session, but at most it restart the session, as in first time.

Then, if I click at address bar and hit ENTER, it recover that first session opened. Very weird to me...

I am running WAMP (php 5+apache), on Windows XP, using Firefox and IExplorer (same behavior in both browsers).

Thank you for your attention.

im looking at the code and im not recognizing your ++$_SESSION. What is this doing?
if the session is set you are setting the session to ++ correct? Could this be why you are having the problems? What happens if you comment this area out?

Umm..That shouldn't reset the session.. But anyway, you can try with an echo statement..
But this is strange.. Are you sure you dont have session_destroy or session_regenerate_id in your script ? hmm..

Hi... Thank you for your response.

++$_SESSION is equals to $_SESSION = $_SESSION + 1

It does not reset the script, as you said.

That code I posted is one of my tests. If I run only that code, that problem happens... If I click at link (a href) new session is created... :( Then, if I go to address bar and hit ENTER, that old session (real one) is taken...

Hello,

Just a quick quesiton, are you a 100% sure the session data file is being stored locally on the machine? (had a similar issue when the /tmp folder on a linux box was full), ensure that session storage is correct and working - the code itself though is without fault. The other option would be to set a cookie, and using a session based cookie and comitting data to that (data stored in cookie, as opposed to an ID pointing to a server-side session file).

Kind Regards,
Dan

Yeap, I am 100% sure. I saw the session Id and saw the file sess_XXXXXX stored at "session.save_path".

I tested with "session.use_only_cookies" set , but the behavior is the same. I think I will re-install that wamp and try again :(

Here is my PHP.INI, maybe something wrong in it:

session
Session Support  enabled  
Registered save handlers  files user sqlite  
Registered serializer handlers  php php_binary wddx  

Directive Local Value Master Value 
session.auto_start Off Off 
session.bug_compat_42 Off Off 
session.bug_compat_warn On On 
session.cache_expire 180 180 
session.cache_limiter nocache nocache 
session.cookie_domain no value no value 
session.cookie_httponly Off Off 
session.cookie_lifetime 0 0 
session.cookie_path / / 
session.cookie_secure Off Off 
session.entropy_file no value no value 
session.entropy_length 0 0 
session.gc_divisor 1000 1000 
session.gc_maxlifetime 1440 1440 
session.gc_probability 1 1 
session.hash_bits_per_character 5 5 
session.hash_function 0 0 
session.name testao PHPSESSID 
session.referer_check no value no value 
session.save_handler files files 
session.save_path c:/wamp/tmp c:/wamp/tmp 
session.serialize_handler php php 
session.use_cookies On On 
session.use_only_cookies Off Off 
session.use_trans_sid 0 0

One more result, a new WAMP installed, new directory, etc, same behavior... Now, trying to install just PHP.

And, latest news: I installed Apache+PHP (separated directory, other save_path, etc), and the behavior is the same.....

Why my "A HREF" are not keeping the session?
:( OMG

I tried to change my link to add this:

?< ?php echo session_name().'='.session_id();?>

Thus, my HREF works.... But, how can I set to LINKs be modified automaticaly in PHP????

Thank you.

Nothing new about this???

Thank you ;-)

Hello,
I have tried your code and works fine....
Best,

Hello,
I have tried your code and works fine....
Best,

Thank you very much...

Now I have other behavior :( The session is not lost, but I need to wait 5 seconds untill press some link... Otherwise, new session is created o_O

Do you know why this can happens???

Thank you again....

Huh! Strange.. Your session settings in php.ini looks good.. Your script executes without a problem here.. :-/ I don't understand !

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.