Advertisement
skisr2000

session.php

Feb 1st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Session{
  5.  
  6.     private $signed_in = false;
  7.     public  $user_id;
  8.     public  $message;
  9.  
  10.  
  11.     function __construct(){
  12.         session_start();
  13.         $this->check_the_login();
  14.         $this->check_message();
  15.  
  16.     }
  17.  
  18. public function message($msg=""){
  19.  
  20.     if(!empty($msg)){
  21.  
  22.         $_SESSION['message'] = $msg;
  23.  
  24.  
  25.     }else{
  26.  
  27.         return $this->message;
  28.  
  29.     }
  30.  
  31. }
  32.  
  33.  
  34. private function check_message(){
  35.  
  36.     if(isset($_SESSION['message'])) {
  37.  
  38.         $this->message = $_SESSION['message'];
  39.         unset($_SESSION['message']);
  40.  
  41.  
  42.     }else{
  43.  
  44.         $this->message = "";
  45.  
  46.     }
  47. }
  48.  
  49.  
  50.  
  51.  
  52.     //call a getter function
  53.     public function is_signed_in(){
  54.  
  55.         return $this->signed_in;
  56.  
  57.     }
  58.  
  59.  
  60.     public function login($user) {
  61.  
  62.         if($user) {
  63.  
  64.             $this->user_id = $_SESSION['user_id'] = $user->id;
  65.             $this->signed_in = true;
  66.  
  67.         }
  68.  
  69.  
  70.     }
  71.  
  72.     public function logout(){
  73.  
  74.         unset($_SESSION['user_id']);
  75.         unset($this->user_id);
  76.         $this->signed_in = false;
  77.         //this unset code came from the discussin group
  78.         //unset($_SESSION['user_id']);
  79.  
  80.     }
  81.  
  82.  
  83.     private function check_the_login(){
  84.  
  85.         if(isset($_SESSION['user_id'])){
  86.  
  87.             $this->user_id = $_SESSION['user_id'];
  88.             $this->signed_in = true;
  89.  
  90.         }else{
  91.  
  92.             unset($this->user_id);
  93.             $this->signed_in = false;
  94.  
  95.         }
  96.  
  97.     }
  98.  
  99. }
  100.  
  101. $session = new Session();
  102. $message = $session->message();
  103.  
  104.  
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement