Aug 12, 2019

Get & Post | PHP tutorial

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. :-)

Mar 10
SEO friendly url with PHP

When it comes to search engine optimization, URL structure plays an important role. If the URL....

Mar 12
Basic cryptography terms for beginners

Hi guys, I thought to start a tutorial serious on cryptography. Because it's an essential subject....

Jun 22
Protostar Stack0 walkthrough

Hello there, In this tutorial we are going to learn Linux exploit development. We use protostar....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.