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.
https://www.google.com/search?q=hacksland
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.
<?php
$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.
<?php
$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">
  name:<br>
  <input type="text" name="name" value="Thilan"><br>
 
  <input type="submit" value="Submit">
</form>
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.
<?php
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).
<?php
$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. :-)

Apr 30
XSS overide functions

One of my friend gave me a JavaScript code and asked to trigger an alert() by changing one....

Jun 20
SEO friendly url with PHP

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

Aug 20
Assembly basic tutorial - add two numbers

Welcome guys, today another basic tutorial on assembly coding. Hear I'll explain you how we can add....

Replying to 's comment Cancel reply