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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.