Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts

    session id usage

    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
    Last edited by low tech; 08-16-2012 at 08:39 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    low tech (08-16-2012)

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    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
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,361
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Quote Originally Posted by felgall View Post
    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).
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    low tech (08-16-2012)

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    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

    Thanks

    LT
    Last edited by low tech; 08-16-2012 at 02:26 PM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,361
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Quote Originally Posted by low tech View Post
    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
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    low tech (08-16-2012)

  • #7
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    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
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,361
    Thanks
    61
    Thanked 530 Times in 517 Posts
    PHP Code:
    //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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    low tech (08-17-2012)

  • #9
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi tangoforce

    That's great.

    I'll definitely try that code out.

    Thanks

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •