How To Pass Php Parameters

Table of contents:

How To Pass Php Parameters
How To Pass Php Parameters

Video: How To Pass Php Parameters

Video: How To Pass Php Parameters
Video: PHP Tutorial: How To Pass Variables In PHP Using Sessions And Get Method 2024, December
Anonim

Very often it becomes necessary to transfer data from a client browser to a server file with a script for processing this data. Let's look at exactly how to organize the transfer of php parameters to the script.

How to pass php parameters
How to pass php parameters

It is necessary

Basic knowledge of PHP and HTML languages

Instructions

Step 1

To transport data from web forms in the HTTP (HyperText Transfer Protocol) two methods are provided - GET and POST. They differ in the way they are transmitted from the client application (browser) to the server application (executable php script). The GET method uses the address bar for this. That is, the names and values of the variables passed to it are appended directly to the script address (or URL - Uniform Resource Locator) through a question mark (?). For example, the URL might look like this:

Here, the search.php script is passed a variable named num with a value of 30, a variable newwindow with a value of 1, and a variable safe with a value of off. The server, having received such a request, by the "?" separates the file address, and divides everything else into pairs of variable names and values. The resulting pairs are filled in the $ _GET array, from which the php script specified in the address will be able to extract them. In its simplest form, the form html code for sending this data from the browser to the server using the GET method might look like this:

And the simplest php script for receiving this data is like this:

<? php

$ num = $ _GET ['num'];

$ newwindow = $ _GET ['newwindow'];

$ safe = $ _GET ['safe'];

?>

The most significant disadvantages of passing variables using the GET method:

- limited amount of data, since the length of the URL cannot exceed 255 characters;

- not all html-code characters can be transferred by this method;

- the transmitted data is visible to the user, which is not always acceptable from a security point of view;

Step 2

These inconveniences and limitations can be avoided by using another method - POST. It uses special areas of network packets to transfer data - headers. In all other respects, the differences between these methods are minimal - in the above form of sending data, only the name of the method will change:

And in the php script, only the name of the data array:

<? php

$ num = $ _POST ['num'];

$ newwindow = $ _POST ['newwindow'];

$ safe = $ _POST ['safe'];

?>

Recommended: