PHP for Beginners – Part 4



php-for-beginners
Hey Folks ! In this part we will be learning how to use super global variables,submit forms and receive its values, storing session values and cookies. Before we get going I suggest you get a basic knowledge of HTML and forms from W3schools, as I will be concentrating on the PHP part rather than HTML.

Before we start building forms and play with data, let’s just have a sneak peek at the variables we are gonna use. I guess you guys already know about variables, data types, and creating functions which is discussed in the “PHP for Beginners – Part 3”. Now we are going to look at superglobal variables. Now there is a difference between normal variable or local variable, global variable and superglobal variable. Lets us first see global variable.

A global variable is like any variable that is declared at the top or top-level of the script and used within a function using global keyword.

Superglobal Variables are arrays that are built in with PHP. So you can access then anywhere within the scope of the script.

Have a look at the list of superglobal variable provided by the PHP.

Array Desciption
$_SERVER Variables made available by the server
$GLOBALS Contains all global variables associated with the current script
$_COOKIE Contains keys and values set as browser cookies
$_GET Contains keys and values submitted to the script using the HTTP get method
$_POST Contains keys and values submitted to the script using the HTTP post method
$_REQUEST A combined array containing values from the $_GET, $_POST, and $_COOKIES superglobal arrays
$_FILES Contains information about uploaded files

Now lets have a closer look at the superglobal variable.

$_SERVER

$_SERVER is an array that contains information related to servers such as paths, script location, headers etc.

$_COOKIE

A cookie is a small piece of information created at server side stored on the user’s browser, Each time the user visit a site/webpage it will send cookie too. Cookie can store a maximum of 4kb of information. Cookies are used where you want to identify the user visiting a website or remembering the password when submitting a form. It is less secure as you can check the values from the browser.

$_GET, $_POST, $_REQUEST

How do we handle data when we submit form?

$_GET is used where the level of security on the variable is low as because information send via this method is visible to everyone. It’s mainly used in search forms or where information send to a page has less security priority. $_GET can only 2000 characters.



Here’s the PHP code to see your data when you submit the form

$_POST basically do the same as $_GET, but it is more secured as because variable passed under this method is hidden. We use this method submitting highly sensitive data such as password, confidential files and data of similar concern. Just change the method tag to post in the form field and paste the code at the top of the script.


The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.

Demonstration of all form field .

The PHP script that will be executed when the form is submitted

$_SESSION

Suppose you have a web store where there are access for two types of users, one the administrator and the visitors and buys your products. So how do you verify the identity of the current user. To solve this issue we use session. Its a super global variable as we have read just before but the main ability of $_SESSION is that stores a state of the user. Every time a visitor logs in it will maintain a separate state for the user. Just treat session as another superglobal variable, the main thing you have to remember is to start the session every time you want to use session. Have a look at the snippet below. Add value to the session variable just like adding value to a normal variable. Look at a sample snippet below.

See your value after your submit form, At the same type run the script with different browser, and login as different user.

To destroy or end the session just unset the session variable

and Thats all.. Hope you guys enjoyed the post. 🙂



Was this article/content helpful?
Thanks! Your feedback helps us to improve PHPHive.info

Leave a Reply

Your email address will not be published. Required fields are marked *