What is SESSION in PHP ?

What is SESSION ?

  • A session is a mechanism to persist information across the different web pages to identify users as they navigate a site or app
  • A PHP session stores data on the server rather than user's computer. 
  • In a session based environment, every user is identified through a unique number called session identifier or SID. 
  • The session IDs are randomly generated by the PHP engine

Starting a PHP Session


To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user.

<?php
// Starting session
session_start();
?> 

Firstly, session_start() function checks to see if a session already exists by presence of a session ID. If session id exists, then it sets up the session variables and if doesn't, it starts a new session by creating a new session ID.

You must call the session_start() function at the beginning of the page.

Storing and Accessing Session Data


You can store all your session data as key-value pairs in the $_SESSION[] superglobal array.  

<?php
// Starting session
session_start();
 
// Storing session data
$_SESSION["firstname"] = "Shobhit";
$_SESSION["lastname"] = "Garg";
?>

To access the session data


<?php
// Starting session
session_start();
 
// Accessing session data
echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"];
?>
 

Destroying a session


If you want to remove certain session data:
 
<?php
// Starting session
session_start();
 
// Removing session data
if(isset($_SESSION["lastname"])){
    unset($_SESSION["lastname"]);
}
?>
 
To destroy session completely:

<?php
// Starting session
session_start();
 
// Destroying session
session_destroy();
?>

How to show errors in PHP?

If you are having problem in debugging with your PHP web application and want to display all the errors and warnings, then use error_reporting.

The quickest way to display all php errors and warnings is to add these lines to your PHP code file:

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);
  
For development server 
error_reporting should be set to E_ALL value; 
display_errors should be set to 1


For production server 
error_reporting should be set to E_ALL value; 
display_errors should be set to 0 
log_errors should be set to 1

How to Increase File Upload Size in PHP ?

You need to set the value of upload_max_filesize and post_max_size in your php.ini :

#Maximum allowed size for uploaded files.
upload_max_filesize = 40M

#Must be greater than or equal to upload_max_filesize
post_max_size = 40M
 
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.

Installing PHP5.6 on Ubuntu 16.04

Installing PHP5.6 on Ubuntu 16.04

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

sudo apt-get install php5.6 libapache2-mod-php php5.6-curl php5.6-gd php5.6-mbstring php5.6-mcrypt php5.6-xml php5.6-xmlrpc php5.6-mysql


In case, php5.6-xmlrpc gives not found error use:

sudo apt-get install php5.6 libapache2-mod-php php5.6-curl php5.6-gd php5.6-mbstring php5.6-mcrypt php5.6-xml php5.6-mysql

It will install PHP5.6 on your server

sudo systemctl restart apache2