/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] what is session in php and how it works.

Hi. This question might have been asked by lots of member in lots of forums, but I’m getting confused with some points, mentioned in w3schools.com and other impaortant websites.

1) What is session and why do we need to use it?

2) What is this statement meaning?

[QUOTE]

When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. [B]The computer knows who you are[/B]. It knows when you start the application and when you end. But on the internet there is one problem: [B]the web server does not know who you are or what you do[/B], [B]because the HTTP address doesn’t maintain state[/B].

[/QUOTE]

The first bold statement is “The computer knows who you are?” What is this mean?
2ndly: The web server does not know who you are and what you do? What is the meaning of server knows me and it knows that what i am doing? What this doing refers to?

Thirdly:
HTTP does not maintain state? What type of state it is referring to and why does it need to maintain state?

3).

[QUOTE]

Most sessions set a user-key on the user’s computer that looks something like this: 765487cf34ert8dede5a562e4f3a7e12. Then, when a session is opened on another page, it scans the computer for a user-key

[/QUOTE]

And finally what is this user key means and how our computer is being scanned?

If anyone can answer any of these three questions, It would be highly lauded. Thanks

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 25.2015 — sessions in php allow you to store data between pages, it means that you don't need a complicated method of forms, filtering POST fields from forms, etc., you issue the session_start(); call at the beginning of the script you execute then set or get if they are set, data from session variables you set. You don't have to uses it, its not mandatory, it however makes life simpler.

With reference to No.2 where did you get that statement form? Seems a bit of an overly complicated explanation for something that allows you to pass data between pages.

Reference to No. 3, where did you get the information?

index page:
[code=php]
session_start();
$id = isset( $_SESSION['userkey'] ) ? $_SESSION['userkey'] : md5( $_SERVER['REOMTE_ADDR'] . time() );
[/code]


what the above means is that the person who calls that page, if no key is set, one is made, from then on on every page you use session_start() you can access $_SESSION['userkey'] and access the key it contains. Be-warned, using a key based solely on the users IP address is dangerous, always pair things like that with someother element to create a bit of randomness to the MD5 result so that if you have two individuals from the same IP address, the time() or even using microtime will mean that each key will be different because of the separation by time.

Thats an overly simple demo but you should get the gist of it. Note that you have other uses for a session too and you are not just limited to one variable as the $_SESSION variable is an array of data.

As for the terminology, the person who wrote the clip you provided seems to be trying to sound like they are some kind of code Guru.
Copy linkTweet thisAlerts:
@AdqusitauthorJan 26.2015 — All this info is on famous www.w3schools.com.

I did not understand still on question 2, about maintaining state?

2ndly, If a visitor (not a user/member) is visiting a site webdeveloper.com, so the php will maintain session for this user too? Is that true?

OR sessions are just created for those who have account in a website?
Copy linkTweet thisAlerts:
@ginerjmJan 26.2015 — #1 - w3schools is NOT the favored source of info for developers. Please don't use them as your guide.

#2 - A session is a convenient place to keep track of your activity on a site or in an application. The meaning of "state" is knowing where you are in a process after going away and coming back to it. Think about how a web appl works. The server is called by a browser sending it an address (url). The server responds by executing the requested script file and that script does something and sends it to the client. That's it - nothing else happens. The server just sits there doing absolutely nothing for you. You are sitting at your desktop and looking at this new screen that you just got. You do something on that screen and submit that 'form' to the server. All the server sees AGAIN is a url asking it (the server) to do something. So - the server does it. It loads a script and that script does what it is setup to do - it reads the input from the form, it does some calculations or some database queries and it sends some info back to the client. But it doesn't know anything about what went on before this and will not know anything about what goes on after it without some kind of memory. That memory is the session. You write your apps to store important things in the session vars, you use the session vars to tell the server and the new script it is running what may have happened before. Like a login. When a login script logs someone in, how will the server know that the next request from that client is from an already-logged in user? It looks in the session for a var made up by the application. The session is the 'memory' for an application. Without it you don't know the status of the user. It's like a conversation between two people who can't remember anything. One person says something and the other person answers back. A minute later the person says something again but the other person doesn't remember what they are talking about. That is a "stateless" conversation. The SESSION helps maintain the state of the conversation by having a storage area only accessible (supposedly) by that client and the server. Nobody else shares that session unless specifically programmed into the application, which it usually isn't. Each client that has a session established has an id associated with it that the server uses to go and get that conversation's session memory and then the server knows what is going on.

Does this help?
Copy linkTweet thisAlerts:
@jsam757Jan 26.2015 — session is a variable store information in our system

$SESSIONS['id'] = $name;

you can use that user id any where in your application
Copy linkTweet thisAlerts:
@AdqusitauthorJan 27.2015 — @ginerjm

This is extremely helpful and very easily understandable explanation of session and its functioning.

Thank you so much and thanks to all who devoted theirself in answering my minor question.
×

Success!

Help @Adqusit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...