954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Invalid Session when clicking a link

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']);
?>
<a href="http://localhost:8081/testesession.php">Again</a>
<?php
}
echo "" . session_id();
?>
razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

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.

razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

im looking at the code and im not recognizing your ++$_SESSION['test']. 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?

ProfessorPC
Posting Whiz in Training
270 posts since Dec 2007
Reputation Points: 31
Solved Threads: 29
 

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..

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hi... Thank you for your response.

++$_SESSION['test'] is equals to $_SESSION['test'] = $_SESSION['test'] + 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...

razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

iFuseDan
Newbie Poster
4 posts since Jul 2006
Reputation Points: 10
Solved Threads: 1
 

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
razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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.

razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Nothing new about this???

Thank you ;-)

razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

silviuks
Junior Poster in Training
96 posts since Apr 2006
Reputation Points: 10
Solved Threads: 15
 
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....

razer.anthom
Newbie Poster
9 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You