Super Global Variables in PHP

  • What is Super Global Variable?

Super global variable is a variable which can be used in two different files for manipulation.

For example we have a variable “a” in File1.php and we need to print, edit or any manipulation needed to be done in the variable in File2.php then we have to use Super Global Variable as a variable in File1.php.

  • Types of Super Global Variable in PHP:-

– $_GET

– $_POST

– $_REQUEST

– $_COOKIE

– $_SESSION

– $_FILES

  • To see the use of variable $_GET and $_POST follow the link given below:

/http://www.scmgalaxy.com/tutorials/_get-and-_post-variable-in-php/

  • Use of variable $_REQUEST

 $_REQUEST can catch the data which is sent using both POST GET methods whereas $_GET and $_POST can catch the data which is sent by get and post.

  • Use of variable $_SERVER

When we use $_SERVER we can extract extra data from the client when the form is filled. Extra data in a sense we can extract:

– HTTP Connection

– Server Information

– Host Information

– URL Information

We can see it with the example given below:

Now when we insert the data in the form.html we will see the following information:

  • Use of $_COOKIE variable:

Cookies are small file that the server embeds on the user’s computer each time the same computer requests a page with a browser it will send the cookie too. With PHP, we can both create and retrieve cookie values.

To create a cookie we need to run the following command:

setcookie()

Syntax:

setcookie(name, value, expire, path, domain, secure, httponly);

In the syntax only the name parameter is mandatory rest other parameters are optional.

Example for creating a cookie:

  • Use of $_SESSION variable

For session variable refer the link given below:

How Session works in PHP

Tagged : / / / /

How Session works in PHP

Session is a way to store information (in variables) to be used across multiple pages. We can see the architecture of session workflow below:

  • Initially we need to write the command session_start() to start a session.
  • Once we write the command server will check if there is a session available.
  • If there is no server then it will store a cookie in the client machine with the cookie name – PHPSESSID amd it will create a session id of 26 or 32 characters.
  • With that it will also store a file on server (Default Location – C:\xampp\tmp) with session id in the suffix.
  • If there is already a session in the client machine then it will show the variables stored in the session under (C:\xampp\tmp) the server.
  • Once we close the browser all the session ends.

Reference:

Tagged : /