Jump to content
xisto Community
Sign in to follow this  
fffanatics

Php Classes Access the same class instance over multiple pages

Recommended Posts

Hey everyone. I am having some trouble accessing the same php class instance in more that one page. This is for my login script and what i need to do is be able to call the classes logout function from a separate page. What i have tried to do is create a new .php page and include the class file and then make a new class and call logout. This does not work because it does not log out the current user which is my problem. Can anyone help me fix this or know of another way to do it?

Below is my class file

<?php     // member class     // handlers member logon     class member_class   {         var $message = '';     var $query_error = 'ERROR: something went wrong when accessing the database. Please consult your webmaster';           function member_class()   {   //constructor             if (!isset($_SESSION['id']))   {   //fills session with empty values                  $this->set_session_defaults();             }               if ($_SESSION['logged_in'])   {    //already logged in                 $this->check_session();             }               if (isset($_COOKIE['remember']))   {                   $this->check_remembered($_COOKIE['remember']);             }         }                  function register($username,$password, $email, $firstname, $lastname, $website, $show_email, $remember)      {             $username = mysql_escape_string($username);             $password = mysql_escape_string(md5($password));               $result=mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '{$username}'"), MYSQL_ASSOC);          if (!$result)       {  //insert record if user name doesn't exist      $date = date("F j, Y", time());               $insert = mysql_query("INSERT INTO user VALUES ('', '$username', '$password', '$firstname', '$lastname', '', '', '', '', '$date', '$email', '$website', '$show_email', 0)") or DIE ($this->query_error);     $result = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '{$username}' AND password = '{$password}'"), MYSQL_ASSOC) or DIE ($this->query_error);     header('Location: index.php');     $this->set_session($result,$remember,true); //log user on                 return true;             }//if     else     {              $this->message .= 'Username already exists! Please choose a different name.';                return false;             }//else         }           function check_login($username,$password,$remember)      {            $username = mysql_escape_string($username);            $password = mysql_escape_string(md5($password));            $result=mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '{$username}' AND password = '{$password}'"), MYSQL_ASSOC);            if ($result)     	{                $this->set_session($result,$remember,true);                return true;            }//if  	else   	{            	$this->failed = true;                $this->logout();             $this->message .= 'Incorrect username or password.';                return false;            }//else         }//check_login              function logout()   {             // blowup cookie             setcookie('remember',time()-3600);             $this->set_session_defaults();         }              function set_session($result,$remember,$init = true)   {             $id=$result['id'];             if ($init)   {                 $session = mysql_escape_string(session_id());                 $ip = mysql_escape_string($_SERVER['REMOTE_ADDR']);             $newtoken = $this->token(); // generate a new token             $update = mysql_query("UPDATE user SET session='{$session}', token='{$newtoken}', ip='{$ip}' WHERE id='{$id}'") or DIE ($this->query_error);             }                  $_SESSION['id'] = $result['id'];             $_SESSION['username'] = htmlspecialchars($result['username']);             $_SESSION['token'] = $newtoken;             $_SESSION['logged_in'] = true;                  if ($remember)   {                 $this->update_cookie($newtoken);             }              }              function update_cookie($token)   {             $cookie = serialize(array($_SESSION['username'],$token));             setcookie('remember',$cookie, time()+12099600);         }              function check_remembered($cookie)   {                  $serializedArray=$cookie;             $serializedArray = stripslashes($serializedArray);             list($username,$token) = unserialize($serializedArray);                  if(empty($username) or empty($token))   {                 return;             } else {                 $username = mysql_escape_string($username);                 $token = mysql_escape_string($token);                 $ip = mysql_escape_string($_SERVER['REMOTE_ADDR']);             $result = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username = '{$username}' AND token ='{$token}' AND ip = '{$ip}'"), MYSQL_ASSOC) or DIE ($this->query_error);                      if (!$result)   {                     $this->set_session($result,false,false);                 }else{                     $this->set_session($result,true,true);	                 }             }         }              function token()   {             // generate a random token             for($i=1;$i<33;$i++)   {                 $seed .= chr(rand(0,255));             }             return md5($seed);         }              function check_session()  {             $username = mysql_escape_string($_SESSION['username']);             $token = mysql_escape_string($_SESSION['token']);             $session = mysql_escape_string(session_id());             $ip = mysql_escape_string($_SERVER['REMOTE_ADDR']);         $result = mysql_fetch_array(mysql_query("SELECT * FROM user WHERE username='{$username}' AND token='{$token}' AND session='{$session}' AND ip='{$ip}'"), MYSQL_ASSOC) or DIE ($this->query_error);               if ($result != false){             }else{                 $this->logout();             }         }                   function set_session_defaults()   {             $_SESSION['logged_in'] = false;             $_SESSION['id'] = 0;             $_SESSION['username'] = '';    $_SESSION['password'] = '';             $_SESSION['cookie'] = 0;             $_SESSION['remember'] = false;         }     }  ?>

Share this post


Link to post
Share on other sites

Well since no one answered my post, i actually figured this one out myself. I just had to include the correct files and recreate the variable which would pick up the Session variables during its initialization and do the correct procedures. For any one interested or needs help doing something like this let me know.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.