E
E
Evgeny Ivanov2018-06-06 08:32:25
PHP
Evgeny Ivanov, 2018-06-06 08:32:25

Why are sessions needed?

After reading about 20 articles about sessions, I did not understand what they are for. On practice.
I have been developing web projects in php for about 3-4 years and use identification through my cookies, as well as storing data in a database.
The session mechanism looks strange and completely unnecessary to me.
In fact, the entire essence of the session mechanism comes down to writing a single PHPSESSID cookie with a unique identifier.
And then the service superglobal array $_SESSION can be filled with any values. For this browser, it will be "associated" with the given cookie - PHPSESSID.
A kind of one cookie (key) on the client, the rest of the "cookies" on the server. Access to them by this key cookie.
Why does the session mechanism look strange and completely unnecessary to me.
A possible option (but they don't).
-------------- you don't have to read----------
Firstly, all this can be done using cookies. All cookies are on the client.
There are no restrictions. The maximum number of cookies is about 2000. The Internet bandwidth is high, the traffic is almost unlimited, the amount of information in cookies is almost nothing.
You can do that. And it will be no worse than sessions. All the same, but on the client.
This is just an example of a technical implementation. I understand safety.
But it can be provided with https for example, when transferring.
And storing cookies on the client and storing the same information on the server - it depends on the client's software / OS, settings, because both the server and the client can be hacked. It's just 2 computers on the network.
------------
As usual I do.
I store all the data, user settings in the database. Information to be kept? Welcome to the database.)
A database is something that is created to store data. Something that can be quickly accessed, changed, etc.
I create one unique cookie for the user at authorization.
Are the username and password (hashes) correct? Ok, you are identified. Get a cookie.
The value of this unique cookie is written (updated) in the database, "opposite" all user data.
id-name-hash_pass-surname-unical_cookie
1-Fedor-Sfr5ttrew-Ivanov-sD56Jgffg9856Sdfg87656vfg
And now php requests this cookie on every call. Is there a line with this cookie in the database?
Yes - it means we can provide the user with his data. For example, display the text - hello Fedor.
Actually, what for then sessions?
Can someone explain what sessions are for and give an example?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DevMan, 2018-06-06
@logpol32

Sessions have only one purpose - sharing data between requests and various components.
Cookies are not suitable for this for several reasons:
1 and the main one - cookies will be available only after the page is reloaded.
2 unnecessary traffic. this is not fatal, but only for now the project is only for hamsters.
3 is not secure. You can of course encrypt, but why?
4 cookies are not rubber, they have a limit.

A
Alexander Aksentiev, 2018-06-06
@Sanasol

The maximum number of cookies is about 2000.

Congratulations, you've invented the knee bike that comes out of the box in more work-friendly sessions.

D
Denis, 2018-06-06
@sidni

Many frameworks have thought of everything for you how and where to store the session, all your logic can work almost out of the box and has been tested by many developers and users both in terms of convenience and security

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question