When you request a web page either by typing in who you are well clicking on a link or submitting afar
on the web browser sends the request to the web server and the web server sends a response back.
For example a page of Eight dml if you request the same page again are indeed another page on the same
site.
The web server doesn't know that it's already sent you something in a previous request.
As far as the web server is concerned.
Each request is unrelated and independent to any previous one.
This is because 8 TTP the protocol used between web browsers and servers is stateless.
Not having state isn't very useful.
However if we want to remember that someone has already been to our site which we need for luggin we
can add States by using sessions when using sessions.
The first time you visit a website the server generates a unique identifier that is sent back to the
browser along with the requested content.
When you request another page this identifier is sent back along with the request as well.
So the server knows you are really been there before and can respond accordingly.
So we can use this for logging in.
In P S P T U sessions we call the session start function.
This creates a new session resumes an existing one.
When a page that contains a call to the session start function is requested.
The server generates a unique session I.D. which is a random string of letters and numbers.
It then creates a file to store the session data identified by this eidy when the server sends the response
back to the user.
It sets a cookie in the browser that contains the same session idae a cookie is simply a sharp string
of text stored in the web browser.
Once a session is started you can store data in the session that you can access from one request to
another in P HP.
Data is stored in the session.
Super global which is an array.
Any data you put into this array are stored on the server in the file for that session.
You can access these data from any page where you call the session start function.
Let's look at an example.
Let's create a new file called session exampled up here it's put
in here.
We'll start a resume the session with session starts then with initialise an element in the session
variable called count to 1.
You can call session elements whatever you like in the session array.
Finally let's just print this out like that.
In the browser we get the number one.
If I refresh the page a few times we still get the number one.
This is as expected as we're simply setting this value to one and then printing it out the first time
we open this script.
The session is started and this session variable element doesn't exist.
So we're creating it.
However the second and subsequent times we open this script.
This line resumes the existing session.
So this arrangement already exists.
We can check for that using the set function and if it is set let's just increment its value by one.
If it's not set.
We'll just initialize it as before.
Now when we open the script for the first time we see the value of one.
If I refresh the page now however we get to every time I reload the page.
The value is incremented.
We're using the session to remember that we've already been here before.
There are a couple of things you need to remember when using sessions first the session data is written
to a file by default.
This can be slow and is slower.
The more data you write to it.
So we only store in the session smile's values like a billion are a numeric idea of a record for example.
Secondly the session starts line needs to be kalid before you send any content to the browser.
This is because the session uses cookies and the cookie data is sent in the headers just like the cold
restaff redirection earlier.
If you call session start after you've used Echo are sent to make it to email.
Then you'll get an error.