PHP session is a variable to be more specific that stores the information about user or settings he makes on a particular page. This information is carried forward to other pages he visits using. In HTML you cannot keep track of this. An example could be a shopping cart on a online shopping portal. You only keep things into it as long as user is logged on, later the information is deleted. Same thing happens in sessions in PHP as well. This PHP session tutorial will guide you through. A PHP session is started using method
Starting a session in PHP
Using the above syntax you can start sessions in PHP. Make sure that session variables store the information temporary; they are stored in PHP cookies.If you want the information to be stored permanently you can very well do that by storing them in MySQL tables. Always make sure you start the session before the HTML text or body.
Session starts by assigning a unique session ID (UID) to every new user who visits or logs in to your website. You can display the session UID(Unique Identification) using the PHP session function
Here is an example where you can see session ID being displayed and number of views of user as well. Note that the session ID doesn’t change unless you close the page and open it again. This is because a session is created for every user who logs in to the page. You can end the session any time using the PHP session function.
Storing information / Retrieving information in a session
Information about the session can be stored in the predefined PHP variable $_SESSION .
Destroying/deleting session information
Sessions in PHP are automatically destroyed once the user leaves the website or closes the browser. You can manually erase the session variable by using the command.
To completely destroy the session use
Here is an example code I have written to explain you the session’s in PHP. Here you can see session ID being displayed and number of views of user as well. Note that the session ID doesn’t change unless you close the page and open it again. This is because a session is created for every user who logs in to the page. Perhaps if u refresh the page using F5 key the number of views increases but session ID remains same. On other side if you close the browser and run the script again you will have different session ID.
<?php session_start(); ?> <html> <body> <?php if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; echo "<br>"; $id=session_id (); echo "Session ID =".$id; ?> </body> </html>
The output is given as:
Sessions are one of the important concepts in PHP where it helps you to design login interfaces to the users. Perhaps it is very easy to handle session in PHP than any other programming language I went through.
There are some functions in PHP sessions that can help you to handle them better. Here they are :
session_cache_expire — Return current cache expire
session_cache_limiter — Get and/or set the current cache limiter
session_commit — Alias of session_write_close
session_decode — Decodes session data from a string
session_destroy — Destroys all data registered to a session
session_encode — Encodes the current session data as a string
session_get_cookie_params — Get the session cookie parameters
session_id — Get and/or set the current session id
session_is_registered — Find out whether a global variable is registered in a session
session_module_name — Get and/or set the current session module
session_name — Get and/or set the current session name
session_regenerate_id — Update the current session id with a newly generated one
session_register — Register one or more global variables with the current session
session_save_path — Get and/or set the current session save path
session_set_cookie_params — Set the session cookie parameters
session_set_save_handler — Sets user-level session storage functions
session_start — Initialize session data
session_unregister — Unregister a global variable from the current session
session_unset — Free all session variables
session_write_close — Write session data and end session
- 10 Best Online Photo Sharing Websites
- Best Video Players for Android devices in 2019
- Best Loan apps for Android
- Best Photo Editing Apps for Android
- Best Video Editor Apps for iPhone
- Best Video Editing Apps for Android
- Best Android Launchers
- Best Android Apps To Draw On Pictures
- Best Android Keyboards with Clipboard Features
- Best Poster Maker Apps for Android