16
Apr
08

PHP session storing Object

HttpSession on java allow me to store an object, then i try it on php and it can be done as well. Very simple as follow:

this listing show you the object registered in session


<?php

           $user = new User();
           $user = $userControl->getUser($userId,$password);

           # CREATE USE SESSION
            session_start();
            session_register("currentUser");
            $_SESSION['currentUser'] = $user;
            # GO TO THE OTHER PAGE
            header("Location: View/index.php");

?>

As you know that variable $user is an object, right? and it has been stored in session. Then in index.php writen as follow:


<?php
function __autoload( $u="user" ) {
include("../Model/".$u.".php");
}

$user = new User();
$user = $_SESSION&#91;'currentUser'&#93;;
echo $user->getUserName();

?>

Don’t forget to write session_start() above them. Function __autoload( <parameter> ) will automaticaly call the function which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

That’s all for this time.

ThanksAlot – Djöllkè

Advertisements

1 Response to “PHP session storing Object”


  1. October 2, 2009 at 6:00 pm

    function __autoload( $u=”user” ) {
    include(“../Model/”.$u.”.php”);
    }

    this trick does not work for all versions of PHP also code can lead to a fatal errors modifying by php.ini.

    please check it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: