...

View Full Version : session id usage



low tech
08-16-2012, 09:08 AM
Hi all


edit for clarity

The function session_id()

I understand what it does, but how is it used practically within a script?

Any explanation of practical usage or a concise example
would be appreciated.

LT

felgall
08-16-2012, 09:17 AM
A session id maintains state between pages. Each of the pages is identified as being a part of the same session and so has access to the same data. Without the session there is nothing to identify that the same person has visited both pages so anything they entered into the first would need to be either passed to the second directly or re-entered there.

The most common example would be someone who logs in on one page is then logged in for all the other pages for that session until they log out.

low tech
08-16-2012, 09:38 AM
Hi felgall

Thanks, I understand that a session uses a session id. I think I was unclear.

What I mean is how is the session_id() function used practically within a script?

ie when would I need to call that function, session_id(), and for what purpose?


I saw
$sesid = session_id();
within a script, but then the $sesid was never used so that didn't help.

i'm unclear how to use the function session_id() practically.

Hope i've made myself clear.


LT

tangoforce
08-16-2012, 02:29 PM
A session id maintains state between pages. Each of the pages is identified as being a part of the same session and so has access to the same data. Without the session there is nothing to identify that the same person has visited both pages so anything they entered into the first would need to be either passed to the second directly or re-entered there.

The most common example would be someone who logs in on one page is then logged in for all the other pages for that session until they log out.

While you've provided a good explanation of how sessions work, you've not explained the answer to the original question - what purpose does the session_id() function have?

@low_tech: You could use this function for all sorts of things from recording IP addresses and their corresponding session into a DB (so that you can later recall the session data from the file on disk) or simply so that you can record the session id itself. When a user logs back in you may wish for them to resume their previous session so set the session_id to the old session id before starting the session.

There are also other things you may want to use it for. EG you may want to record the creation and destruction times of a session so you would need the session id then too.

The session_id() function on php.net isn't brilliantly explained but session_id() also lets you open another session file that is stored on disk (EG if you're an admin and want to see what your users session data is for instance).

low tech
08-16-2012, 03:08 PM
Hi Tangoforce

Just what I was looking for.

Loads of stuff there some of which I can apply and some of which I'd need to learn more about such as using it to open another session file that is stored on disk.

When you say on disk, do you mean on the server?

much appreciated:thumbsup:

Thanks

LT

tangoforce
08-16-2012, 03:32 PM
When you say on disk, do you mean on the server?


Yes. The hard drive on the server (or any other drive that your apache / php / mysql system is on - such as a usb drive OR whereever your system is set to store session data.

Sessions are basically text files on the disk. If you have a wamp setup, look in your php.ini file and locate the session folder. Look in that folder at some of the session files - you will see serialized data that is stored in the session.

All that session_start() does is to create a new file if it exists OR reopen a file if it is specified with a session cookie sent with the http request. PHP then opens the file with your call to start_session() and stores the contents into the $_SESSSION superglobal array. Any writes to the session are automatically written to the file by php and finally when the script terminates, php will automatically close the file again.

That really is all there is to it really. I have some sample code switching between sessions that I will try to post here later (at work on netbook at the moment and there is every chance I will forget so a PM may be useful!).

Tango

low tech
08-16-2012, 03:51 PM
Hi tangoforce

I use xampp

I have a reasonable understanding of sessions, how they are serialized, how to create and detroy them etc. I've done the background reading.

Basically, I only code for myself and I only use sessions for my various login scripts
and I haven't found a need for session_id(), so not easy for me think of how to use session_id() outside the realms of my use of sessions.

Thanks for the further explanation on sessions. Yeh i'll check them out in xampp which
I hadn't thought of doing.

I'd be interested to see your sample code since sessions are my focal point at this moment.

LT

tangoforce
08-16-2012, 08:27 PM
//Target Session - Set your target session here
$Target = '';

$_SESSION['id'] = session_id();
print session_id() .'<br>';
var_dump($_SESSION);
$MySession = session_id();
session_write_close();

session_id($Target);
session_start();
$_SESSION['id'] = session_id();
print session_id() .'<br>';
var_dump($_SESSION);
session_write_close();

session_id($MySession);
session_start();
print session_id() .'<br>';
var_dump($_SESSION);


That code will switch from your current session to a target session and back again - in the process assigning each session id into the session and also printing it to the screen and dumping the session.

Note that you need the session id of the session you wish to target. Hope that helps.

low tech
08-17-2012, 01:06 AM
Hi tangoforce

That's great.

I'll definitely try that code out.

Thanks

LT



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum