OOP - sessions and PHP

I'm a little confused, I started learning php 5 OO Way last year. The first projects are done using the Zend Framework.

But some of my friends are talking about storing objects in the $ _SESSION supergelobal. But I can’t find a good example, why or when is this unscooked?

It would be great if someone can give me a hint as to why store objects in sessions.

+2


source to share


2 answers


A session is essentially a variable that you can store in it, where you want to store, number, character, characters (string), structure objects ...

It is also a variable that remains constant across the pages. Its most widely used for storing information registered by the user.

How I use Session objects:

Suppose I have a users table in my DB and I need to display first and last name on basically all of my pages.

What can I do is two things ... Old way:

$result = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE userId=1"));
$_SESSION['userId'] = $result['userId'];
$_SESSION['firstname'] = $result['firstname'];
$_SESSION['lastname'] = $result['lastname'];

      

Now you can do this, which is fine, but it would be much better if you said the following code:

class user{
    _construct($userId){

       $qry_str = mysql_query("SELECT * FROM users WHERE Id=$userId");

       $result = mysql_fetch_array($qry_str);
       $this->userId = $userId;
       $this->firstname = $result['firstname'];
       $this->lastname = $result['lastname'];
    }

    public $userId, $firstname, $lastname;
}

      



And for initialization

$_SESSION['user'] = new user(1);

      

Or tell multiple users:

$_SESSION['user'][1] = new user(1);
$_SESSION['user'][2] = new user(2);
$_SESSION['user'][3] = new user(3);

      

And then in your code, all you have to do is assign a user by username when logged in, and then use the class that is stored to display all of your information.

There are much more powerful things you can do with classes. But the above shows you a simple and effective solution.

Try this: add if statuses and stamps in your construct for user class ... that when constructing user you can check for error

Hope it helps

+4


source


HTTP is stateless. If you want data persist between different page fetch, you need a different mechanism. Cookies are one way, but because they are stored on the client, they are not trustworthy on the server.$_SESSION

is one approach to persistent, trustworthy repository. Most common use for login systems. Note that the session IDs come from the client, so they are not trustworthy, but you can be sure that the data stored in the session itself is not tampered with. Another option would be to use a database, but then you would have to add serialization / non-serialization code or otherwise convert the data stored in the database to and from PHP objects. One of the advantages of databases is that the data cannot be shared between sessions.

When it comes to storing objects as opposed to other types, it is simply a matter of what your particular application is using, but it is not necessary. Taking the login example, if you are storing authenticated user information (such as user ID and client IP) in an object, you can store that object in $_SESSION

.



For another example, suppose you are writing a messaging application (e.g. forum, email) that uses objects to model messages. Messages can have attachments, but the attachment form is a separate page, so you save the message object to $_SESSION

. Not the most user-friendly design, but common, although less so with the advent of AJAX.

+2


source







All Articles