Thursday, 29 May 2014

$_POST, $_GET and Sessions Concept in PHP



What is Basic difference between $_POST & $_GET Method???

In POST method, the data is hidden and secured. No one can see it.
In GET method, the data is appended with the URL and you can see the data with a proper syntax in the URL
Description
Both methods are used in the HTML forms. Mostly $_POST method is used in HTML forms. Because it’s secure and there is no limitation of the length of the data as well.  In $_GET method there is limit of 255 characters.
As well as performance is concerned, $_GET is faster than $_POST. Time is spent in the encapsulation of the data with HTTP in $_POST.
In $_GET method you can send only text whereas in $_POST method there is no restriction on the data types.
Cashing and Bookmarking can be done with $_GET method but nothing with $_POST method.
$_GET is the default method of the form but for $_POST you have to specify before submission.
$_GET is limited to ASCII characters but $_POST method has encrypt attribute (multipart/form data).

Syntax of $_GET & $_POST

Let suppose you have a input fields in the form. The name of the input field is very important like this:
<input id="firstName" type="text" name="first_name"/>
You can use $_POST method like this:
$first_name = $_POST[' first_name'];
This thing will return the same name which you will type in the text field.
For $_GET Method:
$first_name = $_GET[' first_name'];
NOTE: For $_POST method, you have to mention the “POST” method in the attributes of the form.

What is session in PHP???


This is the most general question in the PHP world and most of people does not know the exact answer of this question.

Definition:

A PHP session is used to store the information of the user for later use like (user_id, username, order_id).  The session is temporarily maintained on server side.
Session creates a unique id for each new visitor on the website. This thing helps the users to prevent the mixing of the data of users.

Syntax:

First of all you have to start the session at the first line of your file like this.
<?php session_start(); ?>
How to store a session variable??
It’s very simple. Like you want to store a UID of a user.
$_SESSION[‘user_id’]= 5;
Now you can take this id to any of the page before ending the session.
For cleaning and destroying session:
 unset($_SESSION[‘user_id’]); //this will clean your session of user_id
 session_destroy();  // This will completely destroy your session.Dnt call that function until you really want to destroy it.

Yellow color is showing the coding part of related topic.

No comments:

Post a Comment