Loosing Session when clicking a link (a href)...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
razer.anthom
Forum Newbie
Posts: 7
Joined: Sun Jun 01, 2008 4:00 pm

Loosing Session when clicking a link (a href)...

Post by razer.anthom »

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.

Code: Select all

<?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();
      ?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Loosing Session when clicking a link (a href)...

Post by califdon »

You do realize that every script that employs session variables must have its own session_start(); statement, right?

Do all your linked scripts have it?
razer.anthom
Forum Newbie
Posts: 7
Joined: Sun Jun 01, 2008 4:00 pm

Re: Loosing Session when clicking a link (a href)...

Post by razer.anthom »

Hi, thank you for your response...

Yes, all my scripts have session_start(), PHP.INI also have the 'session.cookie_domain' setted to blank (I saw in some sites that this is needed to work in LOCALHOST).

For example, that script I wrote here is linked to itself. It has session_start() at the top.

I don't know what is happening :S

Thank you very much.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Loosing Session when clicking a link (a href)...

Post by Chris Corbyn »

Do you have cookies disabled in your browser?
razer.anthom
Forum Newbie
Posts: 7
Joined: Sun Jun 01, 2008 4:00 pm

Re: Loosing Session when clicking a link (a href)...

Post by razer.anthom »

Nope.. All allowed... I tested with IExplorer too, same behavior.

One more information, I tested in my real site, with domain etc et al, running linux, all configured well, and I get the same behavior. Maybe one of you can try that script in your servers... I think my script is wrong, but, I don't know where...

This is the 'session' configuration, get from phpinfo() in my localhost

Code: Select all

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
Forum Newbie
Posts: 7
Joined: Sun Jun 01, 2008 4:00 pm

Re: Loosing Session when clicking a link (a href)...

Post by razer.anthom »

Hi all...

I made two more tests:

1) Installing a new WAMP Server: Same behavior;
2) Installing PHP+Apache in separate: Same behavior.

Why my "A HREF" are not keeping my sessions??????

Tnx :(
razer.anthom
Forum Newbie
Posts: 7
Joined: Sun Jun 01, 2008 4:00 pm

Re: Loosing Session when clicking a link (a href)...

Post by razer.anthom »

NOthing new about this??? :(
razer.anthom
Forum Newbie
Posts: 7
Joined: Sun Jun 01, 2008 4:00 pm

Re: Loosing Session when clicking a link (a href)...

Post by razer.anthom »

Hi friends...

I have a new behavior... I noted that session is not lost.. But I need to wait 5 seconds (I counted) untill click a link. Otherwise, php creates another session....

Do you know why this is happening?

Thank you in advance...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Loosing Session when clicking a link (a href)...

Post by RobertGonzalez »

Wow, that is odd behavior. I would say ditch the crappy WAMP setup and load each component manually. Then try again.

By default PHP sessions work with not intervention needed by you, cookies or not.

And for the record, what leads you to believe sessions are not working?
razer.anthom
Forum Newbie
Posts: 7
Joined: Sun Jun 01, 2008 4:00 pm

Re: Loosing Session when clicking a link (a href)...

Post by razer.anthom »

Thank you for your response...

Well, for now, sessions are working, but, it takes 5 seconds to wait before clicking a link... If I click a link before
5 seconds, new session is created ....

What can cause this behavior????

Thank you in advance.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Loosing Session when clicking a link (a href)...

Post by RobertGonzalez »

I have never heard of that before so unfortunately I cannot help in this matter. Maybe someone else with more experience than me can take a stab at it.
Post Reply