can someone explain to me the complete uses of session variables? [closed]
What's the use of session variables in php? i know the basics, but how can I tell if a user is online for example? what's the session id? in general, i just use sessions to "carry" a variable throughout my website but that's all. Are there any more useful properties?
thanks
From the PHP website:
Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site. All information is in the Session reference section.
From Wikipedia:
In computer science, in particular networking, a session is a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user (see Login session). A session is set up or established at a certain point in time, and torn down at a later point in time. An established communication session may involve more than one message in each direction. A session is typically, but not always, stateful, meaning that at least one of the communicating parts needs to save information about the session history in order to be able to communicate, as opposed to stateless communication, where the communication consists of independent requests with responses. An established session is the basic requirement to perform a connection-oriented communication. A session also is the basic step to transmit in connectionless communication modes. However any unidirectional transmission does not define a session.
Basically, sessions are used to maintain your user's state during a (relatively) short term stay on your website / with your web app. They accomplish this by saving variables such as login information, relevant preferences, and usage data for a certain period of time. This period of time is the amount of time between when a user initializes their session (perhaps by logging in), and destroys their session (perhaps by expiry, or by leaving the website). Ultimately, the session is whatever you as the developer choose to make of it, because in most languages (like PHP) the session is essentially just variable space tied to a given client.
An example session (from the PHP website):
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';
// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
The use of PHP Session variables is so that you can associate a group of variables with every user on the site. So when you do $_SESSION['some_var']
you know you will get the value associated with some_var
that has been assigned to that particular visitor.
You say you use them to "carry" variables throughout the time a visitor is on your site. That's exactly what they're for, and you're not missing anything. You're using them appropriately.
A session is the representation of a time-limited, one-to-one relationship between client and server. In server-side languages (such as PHP), you can attach values to this session. That's the extent of it. There are a million and one ways you can use this concept, or you can depend on it only lightly and use some other technology for tracking persistent values. Each session has a unique identifier so you can refer to discretely if desired - this is your "session ID".
Here is an article covering the basics of sessions, but you've pretty much got it right in your question - there is nothing magical about sessions.
If you had to boil it down to an analogy, think of it as a person checked into a hotel for a single night's stay. That day's date is the session ID - today can only happen once. Everything that happens in that hotel is part of the session - when they go to the restaurant, when they use the ice machine, etc. When they check out in the morning, the session is over.
Now, if they come back another time, the hotel might have kept a record of the last stay (user state saved in database), but this is a new stay and thus a new session.
精彩评论