Repository
https://github.com/php/php-src
What Will I Learn?
I will learn the session, the concept of a session where we can find it and why we use it , and how to create a session or how to save the information about a user , how to modify these session and delete it.
- What's the session.
- How to create a session.
- How to modify a session.
- How to delete a session.
Requirements
System Requirements:
- Server support PHP , Xampp or Wamp for example
- An IDE like Sublime text.
- Browser (Chrome for example)
OS Support for PHP
- Windows
- macOS
- Linux
Required Knowledge
- HTML language
- CSS language
Difficulty
- Basic
Description
In this tutorial we will learn the sessions, how the websites remembers the name or other information about the user who connects in the site.
When you create a website, you need to quickly store and display information about your users to distinguish them or assign them rights of use. The main problem with languages like PHP is that variables have only a limited lifetime to the script that calls them.
To preserve data from page to page, you must pass them through the GET or POST method .This can be binding if we have a lot of data to keep or we do not want to show them to the customer.
Fortunately, we can use a mechanism to store these variables and retrieve them later, there are two ways of storing information, they are both in the form of a file recorded on the disc :
- The cookie that will be on the client side.
- The session that will be server side.
What's a session ?
A session is a technical mechanism for temporarily saving information about a user to the server, this system has been invented to overcome the fact that the HTTP protocol operates in offline mode.
Each time a new session is opened the user is assigned a session identifier, This identifier can be transmitted either in GET, POST or Cookie depending on the configuration of the server.
The information will be transferred from page to page by the server and not by the client, In this way the security and integrity of the data are improved and their availability throughout the session.
A session can contain any type of data (number, string and even an array ), unlike a database or file system the session keeps the information for a few minutes, this time depends on the server configuration but is usually set to 24 minutes by default. The server creates files stored in a particular directory.
Create a session
PHP introduces a unique natively function to start or continue a session, this is session_start()
, this function doesn't take a parameter and always returns true, it checks the status of the current session.
If it doesn't exist the server creates it, else it pursues it in the case of use of sessions with cookies, the function session_start()
must be called before sending to the browser.
When a session is created it's empty by default it has no interest, it must therefore be assigned values to save temporarily, for that the PHP language sets up the $_SESSION
superglobal array.
The array $_SESSION
can be indexed numerically but also associatively, in general the second is preferred in order to be able to give meaningful and meaningful names of session variables.
The example that I want to do is a login page after that I will allow the user to publish something with his username, we will not ask in each task the username of the user, for example in facebook we connect and after we can publish, comment, discuss .. etc with our username, facebook doesn't need to ask each time to enter the username just the first time and will save it using the session.
I have created a form with action="SessionLogin.php"
and the method="post"
I will send the values to the SessionLogin page
I will get the username and the password in the next page to create the session
I will get the $username
and $password
then I will create the session ' $_SESSION['username'] = $username
' in the array $_SESSION
I will add the key username and the value that the user entered, the header("Location: publish.php")
will redirect the user to the publish.php
page
In the publish.php page I have two forms a form for logout and a form to publish an article with a title and content , the idea that I will get the title and content and also the username using the SESSION , this is the result
Modify a session
To modify a session you need to replace the value of the key saved in the $_SESSION
array with another value that the user entred using the input for example.
I have entered the username " Alexendre " I will just change it because am the same user I will change it to any other username and the result in the publish page will be " Maxim " for example.
Destroy a session
As mentioned above the server itself destroys the session after a certain time if the session hasn't been renewed.
On the other hand it's possible to force its destruction by means of the function session_destroy()
, this function allows for example webmasters to propose a logout page to members logged in their personal space, for that you need to use this function to delete or to destroy the session created .
I will use logout to delete the session of the user and to do that I need to go to another page and use the unset()
and destroy()
methods
I will firstly start the session then the methods unset and destroy and I will print the array and " You have disconnected "
And if we open the publish page without session ( we have deleted the session ) , this is the result
Video Tutorial
Curriculum
- PHP Tutorial #11 String Methods ( Addslaches, Chr, Chop and Chunk_splite )
- PHP Tutorial #12 String Methods ( Bin2Hex, Count_Chars, Explode and Hex2Bin )
- PHP Tutorial #13 String Methods (Implode, Join, Lcfirst, Ltrim and Fprintf )
- PHP Tutorial #14 String Methods (Md5_File, Md5, Nl2br and Addcslaches )
- PHP Tutorial #15 String Methods (Number_Format, Ord, Parse_Str, Print and Printf )
- PHP Tutorial #16 String Methods (QuoteMeta, Rtrim , Sha1_file and Sha1 )
- PHP Tutorial #17 Mathematical Methods (Abs, Acos , Acosh and Asin )
- PHP Tutorial #18 ( GET and POST ) Methods and $_SERVER Variables
- PHP Tutorial #19 ( Include and Require ) , Globals Variable, Request and $_SERVER Variables
- PHP Tutorial #20 The Cookie How To Create , Modify And Delete A Cookie
Proof of Work Done
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/login.html
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/SessionLogin.php
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/publish.php
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/logout.php
Hi @alexendre-maxim
I can see an overall improvement in your tutorial in terms of outline, the structure and also the presentation.
You are putting emphasis in diction and therefore making your presentation clear and easy to understand. This is very important especially when English is your second language.
So far, this is the best video tutorial you have submitted.
Rosa
P.S. I also sent a DM to your discord. Can you check?
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Chat with us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you @rosatravels , I am very happy to see that .
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for your review, @rosatravels! Keep up the good work!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hi @alexendre-maxim!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you @steem-ua
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hi @alexendre-maxim!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hello alexendre-maxim, welcome to Partiko, an amazing community for crypto lovers! Here, you will find cool people to connect with, and interesting articles to read!
You can also earn Partiko Points by engaging with people and bringing new people in. And you can convert them into crypto! How cool is that!
Hopefully you will have a lot of fun using Partiko! And never hesitate to reach out to me when you have questions!
Cheers,
crypto.talk
Creator of Partiko
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you @crypto.talk
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey, @alexendre-maxim!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit