Helpful Information
 
 
Category: PHP
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

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 :
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 :


// 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

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

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 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.

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 :mad:










privacy (GDPR)