Session "how-to"

Anyone witha good tutorial about basic session management with PHP /PHPlib? I want/need one that has some examples aswell.
LVL 12
j2Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
maxkirConnect With a Mentor Commented:
Aha. It is a little bit more sofisticated.

Then you need per-user variables, i.e. class My_User.
Also u have to tune class My_Auth and class My_Session in local.inc.
Class My_Auth must be setup  to tune per-user authentication,
class My_Session for setup general session variables as in previous example. Class My_User provides per-user persistent variables, i.e. if u relogin to server with same username, registred variables will be restored automatically.
The most tricky part is to setup My_Auth class - it does the job
of checking user against database.

Setup may be as follows:

In local.inc:

class My_User extends User {
       var $classname = "My_User"; ## Persistence support
       var $that_class = "CT_Sql";
}
class My_Session extends Session {
       var $classname = "My_Session"; ## Persistence support
       var $that_class = "CT_Sql";
       var $mode      = "cookie";
       var $lifetime  = 0;                     ## Per-session cookie
}
class My_Auth extentd Auth {
  // Read docs, it is specific.
}
-------------------------------------------------------------------
USAGE:
The persistent variable is $the_var
In every page:
<?php
page_open(array("sess" => "My_Session", "auth" => "My_Auth", "user" => "My_User"));

# register per-user variable.
$user->register("the_var");

page_close();
?>
0
 
freshmeatCommented:
try this site:
http://phplib.netuse.de/about/sessions/index.php3

hope it help u!
0
 
maxkirCommented:
Do you have probably more concrete problem ?
 All basic stuff is presented on the PHPLIB site mentioned above ...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
j2Author Commented:
freshemeat: Seen that, but it doesnt give me a howto.

maxkir: Mostly curiosity so far, i just want to learn how to maintain a session for a user while they are at a site.
0
 
maxkirCommented:
If you did correct installation of PHPLIB, then ... assume you have simple PHP page b.php3:

<?  // file b.php3
require ($_PHPLIB["libdir"]. "prepend.php3");
$sess_class = "Example_Session";

page_open(array ( "sess"=>$sess_class);
// common header ends here

// content:

$test_var = "Value to save through session. ".
                 "It may be changed later, though";

// register PHP variable $test_var as session variable
$sess->register("test_var");

// Make link to another PHP page:
printf ("A HREF='%s'>a.php3</A>");

//footer:
page_close();

// end of file b.php3
?>

Now, file a.php3:

<?  // file a.php3
require ($_PHPLIB["libdir"]. "prepend.php3");
$sess_class = "Example_Session";

page_open(array ( "sess"=>$sess_class);
// common header ends here

// content:

//Now we can use stored value of variable $test_var;
print $test_var;


//footer:
page_close();

// end of file a.php3
?>
0
 
j2Author Commented:
Im upping the points, because i realize that what i really like is "persistent" data, so how to incorporate mySQL into the scenario?
0
 
freshmeatCommented:
j2, what do u mean "the scenario"?
0
 
j2Author Commented:
scenario = question
0
 
maxkirCommented:
What do u mean by persistent data? If data should be persistent for some period of time ? Or for current session ?
Variable $test_var in my example is persistent through the session. But, if u want it to be persisten through some perion of time u have to setup local.inc file and setup $lifetime variable correctly.
0
 
j2Author Commented:
I meant, i wish to match the user to a mySQL database, to be able to customize the pages.
0
 
j2Author Commented:
Thanks! That seems about right, i will try it out in a day or two.. But right now i am busy with a horrible case of the flu :(

But, i think i see how the above is meant to work.
0
 
j2Author Commented:
After carefully studying the phplib docs, and your example code, i have gotten a(fair) unerstanding of the concept of sessions. Thankyou.
0
All Courses

From novice to tech pro — start learning today.