that's some of my scripts I once used.
have a look at it. it won't run instantly, since I just
took them from a collection of classes

hth
/Dorgon

the most interesting part will be:

// initialize global session object
session_start();

if (isset($_SESSION)) { // php versions >= 4.1.0
        if (!isset($_SESSION['userSess'])) {
                $_SESSION['userSess'] = new UserSession();
                $sess =& $_SESSION['userSess'];
                $sess->init();
        }
        else {
                $sess =& $_SESSION['userSess'];
                $_SESSION['userSess']->reuse();
        }
}
else { // for downward compatibility to old php version (< 4.1.0)
        if (!isset($userSess)) {
                $userSess = new UserSession();
                $sess =& $userSess;
                $userSess->init();
                session_register("userSess");
        }
        else {
                $sess =& $userSess;
                $userSess->reuse();
                session_register("userSess");
        }
}

Lee Herron Qcs wrote:
Does anyone know of a working snippet to manage sessions using any
combination of Register_Global and pre/post php v4.1?

Would love to look at some examples ..


<?php
/** class Session
 *
 * This abstract class provides additional session handling functions beside
 * PHP4's already built-in routines for session treatment.
 * functions reuseAction(), loginAction(&$user), loginFormAction(), logoutAction()
 * have to be implemented by child classes!
 * All functions are commented below.
 *
 * @author      [EMAIL PROTECTED]
 * @version     $Id: $
 *
 */

define("__SESSION_CLASS", 1);

class Session {
        var $userID;
        var $userData;
        var $msg;

        var $params;

        /**
     * after starting a new native php4-session,
     * the Session object should be created
     */
        function Session() {
                $this->params = array();
                $this->msg = "";
                $this->userID = null;
                $this->userData = array();

        }

        /**
     * reuse() should be called on each page request within session scope
         * get-parameters which should be stored in the session can be submitted
         * by a single get-variable: params=KEY1_VALUE1,KEY2_VALUE2,KEY3_VALUE3...
         * ater that the login action is called and
         * finally the proper reuseAction() function of the child class is called
     */
        function reuse() {
                if (isset($GLOBALS['_GET']['params'])) {
                        $doubles = explode(".",$GLOBALS['_GET']['params']);
                        foreach ($doubles as $par) {
                                $par = explode("_",$par);
                                $this->params[$par[0]] = $par[1];
                        }
                }
                $this->doLogin();
                $this->reuseAction();
        }

        /**
     * login action
         * proper functions loginAction(), log
     */
        function doLogin() {
                if (isset($GLOBALS['_GET']['dologin'])) {
                        switch($GLOBALS['_GET']['dologin']) {
                                case "form":
                                        $this->loginFormAction();
                                break;
                                case "login":
                                        if ($GLOBALS['_POST']['user_login'] == "") {
                                                $this->msg = "Please enter your login 
name.";
                                                return false;
                                        }
                                        else {
                                                if(!defined("__USEROBJECT_CLASS")) 
include $GLOBALS['conf']->get("dir_objects")."userobject.inc";
                                                $user = new UserObject();
                                                
$user->loadUserByLogin($GLOBALS['_POST']['user_login']);
                                                if 
($user->checkPwd($GLOBALS['_POST']['user_password'])) {
                                                        if 
($this->loginAction(&$user)) {
                                                                $this->userData = 
$user->getRecord();
                                                                $user->unload();

                                                                $this->userID = 
$this->userData['user_id'];
                                                                
$this->params['last_selflogin_time'] = $this->userData['last_login_time'];
                                                                
$this->params['last_selflogin_host'] = $this->userData['last_login_host'];

                                                                $user->loadLastAdmin();
                                                                
$this->params['last_userlogin_name'] = $user->get("user_first_name")." 
".$user->get("user_last_name");
                                                                
$this->params['last_userlogin_time'] = $user->get("last_login_time");
                                                                
$this->params['last_userlogin_host'] = $user->get("last_login_host");
                                                                
$user->update($this->userID, Array("last_login_time" => "NOW()", "last_login_host" => 
"'".$GLOBALS['_SERVER']['REMOTE_ADDR']."'"), $foo);
                                                                $user->unload();
                                                                $grant = true;
                                                        }
                                                        else $grant = false;
                                                }
                                                else $grant = false;
                                                if (!$grant) $this->msg = "Login 
failed. Please retry.";
                                                $user->unload();
                                                return $grant;
                                        }
                                break;
                                case "logout":
                                        $this->userID = null;
                                        $this->userData = array();
                                        $this->params = array();
                                        $this->logoutAction();
                                        return true;
                                break;
                        }
                }
        }

        function getSessionID() {
                return session_id();
        }

        function loggedOn() {
                if (isset($this->userID)) return true;
                else return false;
        }

        function catchMsg() {
                $msg = $this->msg;
                $this->msg = "";
                return $msg;
        }

        // adapter functions
        function reuseAction() {}                       // must be implemented, called 
on session-reuse (for shop function, etc.)
        function loginAction(&$user) {} // must be implemented, called after 
successful login, $user is given to function
                                                                                       
         // the loginAction() must return true/false -> on false login is denied
        function logoutAction() {}                      // must be implemented, called 
after logout
        function loginFormAction() {}           // must be implemented, called when 
login form is posted
        
}

?>
<?php
/** class UserSession
 *
 * extends class Session with page parameter and shop object
 * @author      [EMAIL PROTECTED]
 * @version     $Id: $
 */

if(!defined("__SESSION_CLASS")) include $conf->get("dir_maininc")."session.inc";
if(!defined("__VISITORTRACKER_CLASS")) include 
$conf->get("dir_frontinc")."visitortracker.inc";
if(!defined("__SHOP_CLASS")) include $conf->get("dir_frontinc")."shop.inc";
define("__USERSESSION_CLASS", 1);

class UserSession extends Session {
        var $shop;
        var $page;

        function UserSession() {
                $this->Session();
        }

        // after starting a new native php4-session, the UserSession object should be 
created and init() called
        function init() {
                $this->page = "homepage";
                $this->shop = new Shop();

                VisitorTracker::trackVisit($this->getSessionID());
        }

        // called when session is re-used
        function reuseAction() {
                if (isset($GLOBALS['_GET']['page'])) $this->page = 
$GLOBALS['_GET']['page'];
                if ($this->page != "login") $this->params['last_page'] = $this->page;

                if (isset($GLOBALS['_GET']['doshop']) || 
isset($this->params['doshop'])) {
                        if ($this->loggedOn()) {
                                $this->shop->doAction($this->params);
                                unset($this->params['doshop']);
                                unset($this->params['last_doshop']);
                                $this->page = "shop";
                        }
                        else {
                                if (isset($GLOBALS['_GET']['song'])) 
$this->params['song'] = $GLOBALS['_GET']['song'];
                                if (isset($GLOBALS['_GET']['doshop'])) 
$this->params['last_doshop'] = $GLOBALS['_GET']['doshop'];
                                if (isset($this->params['last_page'])) $this->page = 
$this->params['last_page'];
                                $this->msg = "Please login first:";
                                $this->page = "login";
                        }
                }

                echo "<!-- userID: ".$this->userID." -->";
                VisitorTracker::trackView($this->getSessionID(), $this->page, 
$this->userID);
        }

        function loginAction(&$user) {
                if (isset($this->params['last_page'])) $this->page = 
$this->params['last_page'];
                if (isset($this->params['last_doshop'])) $this->params['doshop'] = 
$this->params['last_doshop'];
                else $this->page = "homepage";

                // guest user login:
                if (isset($GLOBALS['_POST']['guest_name'])) 
$this->params['guest_name'] = $GLOBALS['_POST']['guest_name'];
                if (isset($GLOBALS['_POST']['guest_email'])) {
                        $this->params['guest_email'] = 
$GLOBALS['_POST']['guest_email'];
                        if(!defined("__GUESTOBJECT_CLASS")) include 
$GLOBALS['conf']->get("dir_objects")."guestobject.inc";
                        $guests = new GuestObject();
                        $guests->insert(Array("guest_name" => 
"'".$GLOBALS['_POST']['guest_name']."'", "guest_email" => 
"'".$GLOBALS['_POST']['guest_email']."'"), $foo);
                }

                $this->shop->init();
                return true;
        }

        function logoutAction() {
                $this->page = "homepage";
        }

        function loginFormAction() {
                $this->page = "login";
        }
}

// initialize global session object
session_start();

if (isset($_SESSION)) { // php versions >= 4.1.0
        if (!isset($_SESSION['userSess'])) {
                $_SESSION['userSess'] = new UserSession();
                $sess =& $_SESSION['userSess'];
                $sess->init();
        }
        else {
                $sess =& $_SESSION['userSess'];
                $_SESSION['userSess']->reuse();
        }
}
else { // for downward compatibility to old php version (< 4.1.0)
        if (!isset($userSess)) {
                $userSess = new UserSession();
                $sess =& $userSess;
                $userSess->init();
                session_register("userSess");
        }
        else {
                $sess =& $userSess;
                $userSess->reuse();
                session_register("userSess");
        }
}

?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to