5.
3 Designing and creating your
web database, Accessing MySQL
database from the Web with PHP,
Session Control in PHP
Session Control in PHP
Session Control in PHP
• Session in PHP is a way of temporarily storing and making data
accessible across all the website pages. It will create a temporary file
that stores various session variables and their values.
• This will be destroyed when you close the website. This file is then
available to all the pages of the website to access information about
the user.
• You need to set the path for this file using the session.save_path setting
from the php.ini file. If you don’t set this path, the session might
malfunction.
What Is the Use of Session in PHP?
• A session in PHP allows the webserver to get the information about
when you started a website, what you were doing, when you closed
the website and other related information. It is required because,
unlike the PC or mobile, the webserver does not have any information
about you.
• These sessions have session variables that store all the necessary
information into a temporary file. By default, it will destroy this file
when you close the website.
• Concluding…a session in PHP helps in storing information about users
and makes the data available to all the pages of a website or
application until you close it.
What Happens When You Start a Session in PHP?
• The following things occur when a session is started:
• It creates a random 32 digit hexadecimal value as an identifier for that particular session.
The identifier value will look something like 4af5ac6val45rf2d5vre58sd648ce5f7.
• It sends a cookie named PHPSESSID to the user’s system. As the name gives out, the
PHPSESSID cookie will store the unique session id of the session.
• A temporary file gets created on the server and is stored in the specified directory. It
names the file on the hexadecimal id value prefixed with sess_. Thus, the above id
example will be held in a file called sess_4af5ac6val45rf2d5vre58sd648ce5f7.
• PHP will access the PHPSESSID cookie and get the unique id string to get session variables’
values. It will then look into its directory for the file named with that string.
• When you close the browser or the website, it terminates the session after a certain
period of a predetermined time.
How to Start a PHP Session?
• You can start a session in PHP by using the session_start() function.
• To set session variables, you can use the global array variable called
$_SESSION[]. The server can then access these global variables until it
terminates the session.
• Note: It is always recommended to put the session_start() function as
the first line in your code, even before any HTML tags.
How to Start a Session in PHP?
<?php <html>
session_start(); <head>
if( isset( $_SESSION['counter'] ) ) { <title>Starting a PHP session</title>
$_SESSION['counter'] += 1; </head>
}else { <body>
$_SESSION['counter'] = 1; <?php echo ( $my_Msg ); ?>
} </body>
$my_Msg = "This page is visited ". </html>
$_SESSION['counter'];
$my_Msg .= " time during this session.";
?>
Output:
How to Access Values From a
Session in PHP?
• <?php
• session_start();
• ?>
• <html>
• <body>
• <?php
• $_SESSION["name"] = "Simplilearn";
• echo "Information set in a
variable.<br/>";
• ?>
• </body>
• </html>
How to Destroy a Session in PHP?
• Although the web server will terminate the session by default upon
closing the browser, you can also destroy it manually. Two functions
can help you achieve this.
• session_destroy(): Calling this function will eliminate all the session
variables
• unset(): Calling this function will kill only the specified session variable
• You can also use the session_unset() function to remove all the
variables of a session. Let’s look at how to destroy the counter
variable that you have created in one of the sessions above.
Example
What is MySQL?
• MySQL is a database system used on the web
• MySQL is a database system that runs on a server
• MySQL is ideal for both small and large applications
• MySQL is very fast, reliable, and easy to use
• MySQL uses standard SQL
• MySQL compiles on a number of platforms
• MySQL is free to download and use
• MySQL is developed, distributed, and supported by Oracle Corporation
What are the other database
options?
• CUBRID
• dBase
• IBM-DB2-Cloudspace
• MongoDB
• MySQL
• OCI8
• PostgreSQL
How to create a database?
Resource
• https://www.simplilearn.com/tutorials/php-tutorial/session-in-php