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 6 of 6

Thread: Session Help

  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    United States
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Session Help

    Please. could someone show me a few lines of code on:
    How to create a session containing the word "Test"
    How to delete a session
    How to erase the data in the session
    How to check if a session exists
    How to read a session

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To create a session use the function :
    session_start();

    To put some info in the session first declare a variable containing the info
    i.e. $variable = "test";
    Then register the variable to the session using
    session_register()
    i.e. session_register("variable");

    To erase the data in a session use the function session_unregister().
    i.e. session_unregister("variable");

    In my knowledge there is no specific function to check if a session is running what you could do is :
    PHP Code:
    session_start(); //start the session
    $running "1"// create a variable saying the session is running
    session_register("running"); // registering the variable to the session so that you can check on other pages if the session is running.
    // Few lines later, when you want to check if the session exists : //
    if($running=="1"){
    // do that
    }else{
     
    // do this 

    By "reading" a session I'll suppose you mean how to access the variables saved in a session.

    First start the session ( session_start() ), you must do this on every page where you do session manipulations. Then use the variable in the session variables as normal variables.
    i.e. echo $variable; // that will print "test" on the screen

    To summarize it all :
    PHP Code:
    // start the session
    session_start();
    $running 1;
    session_register("running");

    // make a few variable
    $variable1  "blah";
    $variable2 "test";

    // register them to the session
    session_register("variable1");
    session_register("variable2");

    // check if the function is started
    if($running){
     
    // print out the variables
     
    echo $variable1 "<br>" $variable2;
    }else{
     echo 
    "The session isn't working"


    Hope that will help you.

    For more information on sessions : http://www.php.net/manual/en/ref.session.php
    I don't suffer from insanity, I enjoy every single minute of it!

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the seesions part in the PHP manual arent actually that clear. and another thing is if you are registering a variable with seesion_register() then there is no point putting session_start() because session_register calls it automatily (as it is said in the PHP manual). if you are checking session variables on each page just dump the checking code in a file and use request() or include() to have the code in the page you have just put request() or include() in, it will save you alot of time when it comes to going through your code
    photoshop too expensive? use the GIMP! www.gimp.org

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's true that session_register() implicitly starts the session but it's a better habbit to always use session_start(); It can always happen that you change the code to call a variable from the session before you register a variable and forget to add session_start().
    I don't suffer from insanity, I enjoy every single minute of it!

  • #5
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts

    Deleting a session

    I like to use the session_destroy() method to clear and delete any and all session data (ie logout of a member area)

    Just thought I'd add that as well.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    ColoRockyz
    Posts
    1,646
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Using session_destroy(); does just that...However, it doesn't destroy your SID. I came back in and the same SID was assigned to me (?) yet the session data retained was different.

    Umm...I asked for session help for two weeks here and never received a single reply. I had to figure it all out myself...Grrrrrrr


  •  

    Posting Permissions

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