• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
logo

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Write For US
  • News
  • About Us
  • Snippets
You are here: Home / PHP / PHP GET and POST Method

PHP GET and POST Method

Sep 22, 2017 by Editorial Staff Leave a Comment

When we are using html form, we can send data to another page using two different methods

  • POST method
  • GET method

Both serves same purpose, but the internal mechanism is bit different. When using GET method key value pairs are passing through browser navigation bar. But for POST method a visitor cannot see the data through browse, basically it sending via HTTP headers.

GET Method

Spaces are removed and replaced with the + character or  any other non alphanumeric characters are replaced with a hexadecimal values. Values are URL encoded for better handling.

A sample GET method request will look like this

http://domian.com/index.php?categroy=outdoor&product=barbiedoll

The encoded string should pass after a question mark, otherwise we cannot parse the GET data.

  • GET method produces a long string that we can see on the browser navigation bar.
  • We can send upto 1024 characters using GET method.

See the below examples to get an understanding how GET method sending data. If you don’t specify a method in the form, default is GET.

<form action="formget.php">
<input type="text" name="fistname" >
<input type="text" name="lastname" >
<input type="text" name="email" >
<input type="text" name="mobile" >
<button name="button" value="OK" type ="button">Submit</button>
</form>

This will create a encoded URL like

http://domain.com/formget.php?fistname=John&lastname=Doe&email=john@examle.com&mobile=909909

So your form will be sending all data to formget.php, which is a server side script. Next, we will see how to retrieve these GET values in a server side script, like PHP.

formget.php

<?php
echo "Firstname: ". $_GET['firstname']. "<br />";
echo "Lastname: ". $_GET["lastname"]. "<br />";
echo "Email: ". $_GET["email"]. "<br />";
echo "Mobile: ". $_GET["mobile"];

When using GET method all the form values are exposed to public, so never use the GET method for sensitive data like passwords, or credit card information etc.

POST method

Using POST method is more safer than GET method and more data can transfer using POST method, there is no restriction for sending data. POST method transfers the data via HTTP headers.

See below an example of POST method form, you have to explicitly add the form method in order to do POST form

<form action="formpost.php"  method="post">
<input type="text" name="fistname" >
<input type="text" name="lastname" >
<input type="text" name="email" >
<input type="text" name="mobile" >
<button name="button" value="OK" type ="button">Submit</button>
</form>

So in formpost.php, you can get the detials as follows

echo "Firstname: ". $_POST['firstname']. "<br />";
echo "Lastname: ". $_POST["lastname"]. "<br />";
echo "Email: ". $_POST["email"]. "<br />";
echo "Mobile: ". $_POST["mobile"];

Conclusion

Both methods are using for transferring data from form to server side scripts. Do not use GET method while you dealing with sensitive data like passwords, credit card information.

You may be interested in this simple login script script, which uses these methods in a application

 

 

 

 


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Filed Under: PHP

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar




Quick Links

  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Base64 Decode Online
  • Base64 Encode Online

Subscribe

* indicates required

Share

   

Hot topics

  • A Simple YouTube Video Downloader Script in PHP 270 views
  • Replace “\n” with new line characters, using Notepad++ 242 views
  • Add FTP/SFTP in Visual Studio Code 177 views
  • Open a URL in a new tab using JavaScript 141 views
  • Using Third-Party Libraries in Codeigniter 139 views
  • Upload Multiple Images and Store in Database using PHP and MySQL. 130 views
  • Laravel Image Intervention Tutorial With Example 125 views
  • Hierarchical Tree view Category Example in Laravel 112 views
  • Spout, an awesome library for reading and writing in Excel. 102 views
  • Manually Install APK Files In Android Studio Emulator 98 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2021 · TutsPlanet Gene Theme on Genesis Framework · Powered By BunnyCDN