Solved

Switching sessions mid-stream

Posted on 2004-03-20
5
193 Views
Last Modified: 2006-11-17
I have an app that stores the user's IP and useragent with the session... then also stores the session ID in the database.

Every time a page is accessed, I check the current session against the one in the DB. If they're differerent, voila, I can tell the account is logged in from two different computers at the same time.

What I'm trying to do now is switch over to the canned session and fetch the old IP and useragent so I can log them. My code is as follows:


$oldsess = (fetched from user record in db)
$newsess = session_id();

if ($oldsess != $newsess) {

      session_id($oldsess);            // switch to the one we found in the db
      session_start();
      $oldIP = $_SESSION['ip'];
      $oldUserAgent = $_SESSION['useragent'];
      
      session_id($newsess);            // switch back to the current one
      session_start();
      $newIP = $_SESSION['ip'];
      $newUserAgent = $_SESSION['useragent'];

The problem is that it's not really 'switching' sessions when I try to go to the old one-- at the end of the code above, $oldIP == $newIP and $oldUserAgent == $newUserAgent.

Why is this failing? Does switching sessions require a page refresh (so http headers are sent), or something like that?

E

p.s. I'm trying to avoid storing the IP address in the db, though that would be a different way to solve this.
0
Comment
Question by:pnoeric
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:jkna_gunn
Comment Utility
does session_id not need to be called before session_start??, so when you try to switch back it fails.
0
 

Author Comment

by:pnoeric
Comment Utility
eh? not sure I follow. session_id sets the session, then session_start "kicks it into place." or so I believe that's how it works. or are you saying try it without the session_start after switching it with the _id?
0
 
LVL 6

Accepted Solution

by:
jkna_gunn earned 125 total points
Comment Utility
i had never used session_id before so checked on php.net, and it says that it needs to be called before session_start
but since you are calling session_id and session_start twice then i dont think it would work. as the second time you call session_id the session has already started. try session_destroy before using session_id the second time.
0
 

Author Comment

by:pnoeric
Comment Utility
Ugh. I tried it-- then I get "Session object destruction failed" on the line with session_destroy

Very, very strange. Any other ideas? I can't tell if the problem is that sessions are buggy or the documentation is just really fuzzy.

best
Eric
0
 

Author Comment

by:pnoeric
Comment Utility
Never was able to really get this working :-( Sesssions seem to either be broken in PHP or not documented well enough that I could figure out how to switch 'em in mid-stream. I ended up just storing the IP address and running with that.

I'm crediting jkna_gunn the points since s/he tried to help me out :-)

best
Eric
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now