Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  95] [ 2]  / answers: 1 / hits: 16997  / 8 Years ago, sun, december 25, 2016, 12:00:00

Quick Question: When you login to your account on a website what does it do to keep you logged in so you don't login again and again when you visit another page?


More From » php

 Answers
26

It is all about sessions.



Source




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
Web server session management
...
Hypertext Transfer Protocol (HTTP) is stateless: a client computer
running a web browser must establish a new Transmission Control
Protocol (TCP) network connection to the web server with each new HTTP
GET or POST request. The web server, therefore, cannot rely on an
established TCP network connection for longer than a single HTTP GET
or POST operation. Session management is the technique used by the web
developer to make the stateless HTTP protocol support session state.
For example, once a user has been authenticated to the web server, the
user's next HTTP request (GET or POST) should not cause the web server
to ask for the user's account and password again. For a discussion of
the methods used to accomplish this see HTTP cookie and Session ID



In situations where multiple web servers must share knowledge of
session state (as is typical in a cluster environment) session
information must be shared between the cluster nodes that are running
web server software. Methods for sharing session state between nodes
in a cluster include: multicasting session information to member nodes
(see JGroups for one example of this technique), sharing session
information with a partner node using distributed shared memory or
memory virtualization, sharing session information between nodes using
network sockets, storing session information on a shared file system
such as a distributed file system or a global file system, or storing
the session information outside the cluster in a database.



If session information is considered transient, volatile data that is
not required for non-repudiation of transactions and does not contain
data that is subject to compliance auditing then any method of storing session information
can be used. However, if session information is subject to audit
compliance, consideration should be given to the method used for
session storage, replication, and clustering.



In a service-oriented architecture, Simple Object Access Protocol or
SOAP messages constructed with Extensible Markup Language (XML)
messages can be used by consumer applications to cause web servers to
create sessions.




In raw php (most well known frameworks has session management middleware, so you shouldn't worry about it) if you want to manage a session, you have to include



session_start();


procedure on top of your pages. When you do this, you are creating a 24 minutes (1440 seconds) session (by default).



You can modify it to any integer from your php.ini file.



All session data in php stored in $_SESSION global. Hence, it is an array, so you can set session variables (aanything you want) like,



$_SESSION['user_name'] = 'ernesto';
$_SESSION['foo'] = 'bar';
...


At any time of your application, you can remove $_SESSION variables,



session_unset();


Assume, you've already set variables above,



print_r($_SESSION);


will print empty array as you've removed variables by unset procedure.



If you want completely to destroy a session,



session_destroy();


will do it for you.


[#59571] Thursday, December 22, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maurodannyr

Total Points: 126
Total Questions: 103
Total Answers: 105

Location: Maldives
Member since Sun, Feb 27, 2022
2 Years ago
maurodannyr questions
Fri, Jul 9, 21, 00:00, 3 Years ago
Thu, Sep 17, 20, 00:00, 4 Years ago
Sat, Sep 28, 19, 00:00, 5 Years ago
;