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

Find out the elapsed time of a running process #process #terminal

There are a lot of processes running on your Linux system. Here is a command that will let you know how long the process has been running:

#ps -eo "%p %c %t"|grep "sshd"

In response to the above command, you will get the following output:

2850 sshd 172-01:37:22
29532 sshd 125-09:07:10

In the above command %p is pid, %c is command and %t is elapsed time.

Clearing a log/text file #file

$ >filename

How to check the date and time the system was rebooted #terminal #reboot #security

Here is a simple command to check the system's reboot date and time:

#last reboot

reboot system boot 2.6.18-53.el5 Sat Aug 6 18:02 (8+04:45)
wtmp begins Sat Aug 6 18:02:07 2011

The command below will give you the date and time the system was booted:

#who -b

system boot 2011-08-24 09:43

Securing files #terminal #security #file

Here is a simple tip to password protect your files:

vi -x test

This command will ask for an encryption key. You have to type the key twice. Then save and quit the opened file.
Now, whenever you open this file, it will ask for that password first.

To display network statistics #netstat

netstat -s

To display all open network sockets #netstat

netstat -uta

To display the kernel routing table #netstat

netstat -rn

To display the kernel interface table #netstat

netstat -i

Cut specific logs #file #vi #cut

If you need to cut specific logs from the complete log of any application, here is a tip that will

be of help.

Open the log file in a vi editor and set the editor to display the line number:

vi server.log

:set nu

The above process will provide you the line numbers in the logs. You can then search for

the specific string and note down the line number (e.g., 550). Now, note down the last line

number by using Shift+G (e.g., 780)

sed -n 550,780p server.log > new.log

So the new only contains lines from 550 to 780.

Print a file with line numbers #file #nl

If you want a file with line numbers (say for printing), you can use the 'nl' command in Linux:

$ nl file.c

This prints the file with line numbers to standard output or this can be even redirected to a

file as shown below:

$nl file.c > output.txt

Here, output.txt will have the codes of file.c with each line having a line number.

Measuring the network throughput between two Linux systems #networking #throughput

Iperf is a tool that measures the bandwidth and the quality of a network link. It can be installed very easily on any Linux system. One host must be set as the client and the other one as the server. Make sure that iperf is installed on both systems. If it is not installed, then use your package manager to install it before trying this tip.

Now run iperf on one of the Linux systems as the server, as shown below:

linux-erv3:/home/test/Desktop # iperf -s

------------------------------------------------------------
Server listening on TCP port 5001

TCP window size: 85.3 KByte (default)
------------------------------------------------------------

Go to the second Linux system and run iperf -c as the client:

linux-6bg3:~ # iperf -c 192.168.1.100

------------------------------------------------------------
Client connecting to 192.168.1.100, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------

[ 3] local 192.168.1.109 port 39572 connected with 192.168.1.100 port 5001

^C[ ID] Interval Transfer Bandwidth

[ 3] 0.0- 6.3 sec 6.38 MBytes 8.51 Mbits/sec

By default, the iperf client connects to the iperf server on the TCP port 5001 and the bandwidth displayed by iperf is the bandwidth from the client to the server. In the above example, it is 8.51 Mbits/sec between two Linux test systems connected over a wireless network.

Find your OS and distribution name #terminal #system configuration

Here is a tip that will let you know the name of the OS, along with other details:

[root@vl-pun-blg-qa27]# lsb_release -a

LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description: CentOS release 5.5 (Final)
Release: 5.5
Codename: Final

View the contents of tar and rpm files #tar #rpm

Here are two simple commands to show you the contents of the tar and rpm files.

1. To view the content of a tar file, issue the following command:

#tar -tvf /path/to/file.tar

2. To view the content of an rpm file, use the command given below:

#rpm -qlp /path/to/file.rpm

Find your MySQL configuration file #mysql #grep #file

We often have to administer a system that has been set up by someone else. In such a situation, it's difficult to find the correct configuration files for different applications. Here is a tip to find the correct configuration file for MySQL:

mysql -? | grep ".cnf”

Replacing '\n' with 'space' in each line of a file #replace #awk

You can use the awk statement given below to remove the '\n' from each line and replace it with a blank space:

awk '$1=$1' ORS=' ' /etc/passwd

Comment out hashes in large configuration files

Here is a small tip for system administrators, who need to tackle large configuration files, which include lots of commented lines (marked by #). With this tip you can remove all those hashes and provide only an uncommented configuration view for faster lookup into the file.
If you want to check the configuration file of the Squid proxy server, run the following command:

#cat squid.conf | egrep -v ^#

This will show only lines that do not start with a hash mark, thus giving the configuration parameter that is being used in the current set-up.

Run a linux command after every reboot

This tip allows you to run any Linux command or script just after system reboot. You can use the @reboot cron keyword.
If you have a script in your /home directory and it needs to be run on every boot, open the cron file in editable mode and add the following:

$crontab -e

@reboot /home/xyz/myscript.sh

Do remember to enable crond on boot.

Connecting to a remote Linux Machine

 
If you want to execute any command or script on a remote Linux machine, you can use ssh. Below are a few examples.

The syntax for running a command or script on a remote server is:
ssh [USER]@[IP] [command or script]

Let us look at how this can be done. Suppose you want to display the directory contents of /root of a remote host, you can run the following command:

[user@ubunu]$ ssh root@192.168.0.128 ls -l /root
root@192.168.0.128's password:
total 12

drwxr-xr-x. 2 root root 4096 Apr 12 00:05 a_folder

drwxr-xr-x. 2 root root 4096 Apr 12 01:31 b_folder

drwxr-xr-x. 2 root root 4096 Apr 12 01:32 c_folder