Forum Moderators: coopster

Message Too Old, No Replies

Objects in sessions.

Have I understood this correctly?

         

Warboss Alex

11:15 am on Dec 16, 2004 (gmt 0)

10+ Year Member



Having done intense Java for the past six months, for one reason or another, I'm trying to make my user login system totally OO, which means having a user object on every page, instead of a user information array variable.

This means sessions, of course, and that's what I've been using so far for the array. As far as I know, serialisation/deserialisation is done automatically in PHP, so I don't need to do that (I'm using PHP 4.3.6).

That is to say, would this work?

//page 1
include 'userClass.php'
session_start();
$user =& new userClass();
//do login stuff, whatever.
$_SESSION['user'] = $user;

//page 2
include 'userClass.php';
session_start();
$_SESSION['user']->callMethod();

By the way, if anyone thinks the array's a better idea, do let me know.

Cheers,
Alex ...

henry0

1:09 pm on Dec 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use O O
but do not see an advantage in authentication
perhaps you can explain further why just declaring a session and carry it is not sufficient in your sript?

##### declare: ########
session_start();
$username=$_POST['username'];
$_SESSION['username'] = $username;

###### use it: #########
session_start();

Possibly I did not understood your quest?

regards

Henry

Warboss Alex

2:43 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



I doubt there's much of an advantage my friend, all I'd be doing is something like:

//get user name
print $_SESSION['user']->getUserName();

Instead of:

//get user name
print $_SESSION['user']['userName'];

There's really not much to it, I just seem to find that passing class methods around rather than variables seems to be a bit cleaner. Especially as if I change $_SESSION['userName'] to just $_SESSION['name'], it doesn't matter much if I was just calling an object method (since that wouldn't be renamed).

There's no real advantage, like I said. Cleaner code, possibly. Just asking if I'd got it right, that's all. Maybe in the next code overthrow I do of my site I might put this in.

Cheers,
Alex ...