PHP for Beginners – Part 4

0
Want create site? Find Free WordPress Themes and plugins.

Hey Guys! In this part you will be learning how to use super global variables, submit forms and receive its values, storing session values and cookies. But before 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 2 and PHP for Beginners – Part 3.

We are going to look at superglobal variables. There is a difference between local variable, global variable and superglobal variable.

Local variable is a basic variable that access limit is just valid to the block that it is defined. These are used almost every time.

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

Let’s have a closer look at these 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 have 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 to POST in the form field and paste the code at the top of the script.

<form name=”simpleForm” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”get”>

The predefined $_REQUEST variable contains the contents of $_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, administrator and the visitors (buys 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.

This section is very crucial before moving on to the next tutorial. As you will be creating an assignment in the next tutorial but the basics will be based on this tutorial only.

Did you find apk for android? You can find new Free Android Games and apps.
Share.

About Author

Entrepreneur & Dreamer I am a passionate Software Professional, love to learn and share my knowledge with others. Software is the hardware of my life.

Leave A Reply

CommentLuv badge