Get & Post | PHP tutorial

HacksLand | The computer science playground

Posted by Thilan Dissanayaka on Aug 12, 2019
Today another tutorial on PHP programming. In this one I'm going to explain how we can fetch HTTP requests in PHP. The most common methods we use are Get and Post. You have seen Get requests when you search something on google. Think about following url.
After it's domain there is a special parameter that named "q" . That's used to make a GET request. After web application receive that request it decode parameter and do the processes which supposed to do.

GET Request

In most of times we use get requests . When we use it , all data we send will be displayed in URL. Hear I have an example for this method.
$name = $_GET['name'];
echo "Hello ". $name . " how are you?"
Actually $_GET is a super global variable That means we can use it in any where even  inside functions. If we want to extract name parameter we can use $_GET['name'].   Now we can make a request to that web page with following syntax. post.php?name=Thilan  

POST Request

We use POST method normally  when we submitting a form. That's syntax is same of above GET method. There is a global variable array called $_POST and it'll hold all post variables.
$name = $_post['name'];
echo "Hello ". $name . " how are you?"
Hear I have an example to submit data with post method. When we open a form in HTML we can set the method as GET or POST  Also we want to specify the page which data should be sent to with the action parameter.
<form action="post.php" method="post">
  <input type="text" name="name" value="Thilan"><br>
  <input type="submit" value="Submit">
When the form is submitted by clicking submit button all data will be sent to our PHP file via POST method. Now hear I have show you a best practice when fetching GET/POST requests in PHP. What if you try to fetch $_GET['name'] which is not provided in the URL? You'll get an error saying parameter is not supplied. So it's safe to check if parameter is supplied or not before you get it. For this we can use PHP function called isset(). It'll return true if given argument is present. So we use an if command to check whether GET parameter is supplied or not.
if isset( $_GET['name']){
  $name = $_GET['name'];
  echo "Hello ". $name . " how are you?" 

Finally we have to learn about $_REQUEST[]  global variable. It can fetch data that's sent by any method. (GET or POST).
$name = $_REQUEST['name'];
echo "Hello ". $name . " how are you?"
Now I think you got it clearly. Thank you for reading Don't forget to take a moment to leave a comment. :-)

Hi, I'm Thilan. An engineering student from SriLanka. I love to code with Python, JavaScript PHP and C.

Also read

Oct 16
Protostar Stack3 Tutorial

Hello there in this article we are going to see how we can exploit and win protostar stack 3 level.....

Oct 17
Command line arguments in C

Hi guys, In this document we are going to see how we can use command line argumentsin C programs .....

Aug 12
IP Address Explained

In this tutorial I'm going to cover an important topic in networking. Yes it's internet protocol.....