Php simple Session code

Source: Internet
Author: User
This is a draft. it is very simple. I will go back to some sessions later. Allright, code on The code is as follows:
Class session
{
Static $ sessionObject;
/* $ _ SESSION ['user'] = 0, don't work.
* = 1, he is really user.
*/
Private function _ construct ()
{
If (! (Isset ($ _ SESSION ['user']) & $ _ SESSION ['user'] = 0 ))
Session_set_cookie_params (3600 );

Session_start (); // Initialize14} 15 private function _ clone ()
{
}
Public static function getObject ()
{
If (! Self: $ sessionObject instanceof self)
$ SessionObject = new session ();

Return $ sessionObject;
}

Public function set_false ()
{
$ _ SESSION ['user'] = 0; // for PHP> = 5
}
Public function set_true ()
{
If (isset ($ _ SESSION ['user'])
$ _ SESSION ['user'] = 1;
Else
$ This-> error ();
}
Public function get_status ()
{
Return $ _ SESSION ['user'];
}
Public function end_session ()
{
Session_destroy ();
}
Public function error ()
{

}
}
?>

General usage
Example:
Log in page: $ session = session: getObject ();
$ Session-> set_ture (); // if $ row ['password'] =
Next page:
The code is as follows:
$ Session = session: getObject ();
If ($ session-> get_status ())
//... The user is really
Else
//....

Log out: $ session = session: getObject ();
$ Session-> end_session ();
Get_status () returns whether the current user is logged on effectively.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.