PHP Session PHP
- PHP Tutorial
- What is PHP
- Install PHP
- PHP Example
- PHP Echo
- PHP Print
- PHP Variables
- PHP $ and $$ Variables
- PHP Constants
- Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- PHP Boolean
- PHP is_bool() function
- PHP Integer
- PHP is_int() function
- PHP Float
- PHP is_float Function
- PHP Compound Types
- PHP Special Types
- PHP is_null() function
- PHP If Else
- PHP Switch
- PHP For Loop
- PHP While Loop
- PHP do while loop
- PHP Break
- PHP Functions
- PHP Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Argument Values Function
- PHP Variable Length Argument Function
- PHP Recursive Function
- PHP Arrays
- PHP Indexed Array
- PHP Associative Array
- PHP Multidimensional Array
- PHP Array Functions
- PHP String
- PHP String Functions
- PHP Math functions
- PHP Form Handling | GET and POST method
- PHP Include File
- PHP Cookie
- PHP Session
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append to File
- PHP Delete File
- PHP File Upload
- PHP Download File
- PHP Mail
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table Example
- PHP MySQL Insert Record
- PHP Prepared Statements
- PHP MySQL Update Record
- PHP MySQL Delete Record
- PHP MySQL Select
- PHP MySQL Order By
- PHP Limit Data MySql
- PHP JSON
- PHP XML Parsers
- PHP SimpleXML Parser
- PHP SimpleXML - Get Node/Attribute Values
- PHP XML Expat Parser
- PHP XML DOM Parser
- PHP - AJAX intro
- MySQL CREATE VIEW
PHP Session
PHP Session
PHP session is used to store and pass information from one page to another temporarily (until user close the website).
PHP session technique is widely used in shopping websites where we need to store and pass cart information e.g. username, product code, product name, product price etc from one page to another.
PHP session creates unique user id for each browser to recognize the user and avoid conflict between multiple browsers.
PHP session_start() function
PHP session_start() function is used to start the session. It starts a new or resumes existing session. It returns existing session if session is created already. If session is not available, it creates and returns new session.
Syntax
- bool session_start ( void )
Example
- session_start();
PHP $_SESSION
PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.
Example: Store information
- $_SESSION["user"] = "Sachin";
Example: Get information
- echo $_SESSION["user"];
PHP Session Example
File: session1.php
- <?php
- session_start();
- ?>
- <html>
- <body>
- <?php
- $_SESSION["user"] = "Sachin";
- echo "Session information are set successfully.<br/>";
- ?>
- <a href="session2.php">Visit next page</a>
- </body>
- </html>
File: session2.php
- <?php
- session_start();
- ?>
- <html>
- <body>
- <?php
- echo "User is: ".$_SESSION["user"];
- ?>
- </body>
- </html>
PHP Session Counter Example
File: sessioncounter.php
- <?php
- session_start();
- if (!isset($_SESSION['counter'])) {
- $_SESSION['counter'] = 1;
- } else {
- $_SESSION['counter']++;
- }
- echo ("Page Views: ".$_SESSION['counter']);
- ?>
PHP Destroying Session
PHP session_destroy() function is used to destroy all session variables completely.
File: session3.php
- <?php
- session_start();
- session_destroy();
- ?>