search

PHP post methods are one of the ways by which the browser client is able to send required information to the webserver. Basically, when we have to log in to a website or interacting with our mails, it means we are dealing with a form. Forms are used to fetch and submit the information taken from the user to the webserver. They are basically HTML tags that have graphical user interface items like the input box, radio buttons, checkboxes, etc,.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

A form is defined using the

tags and GUI items described using input form elements and come in use when dealing with the development of adjustable and dynamic applications that allow user input. Forms can also be used to alter the existing data in the database information.

Syntax:

<?php $_POST['var'];
?>

Where,

  • var is the variable name of the URL
  • $_POST being the PHP array

How Does PHP POST Method work?

This method submits the value via HTTP headers and since the above variable has a global scope, it can be obtained from anywhere in the program. This method is best used when the form post values are not to be shown in the URL.

Some of its properties are:

  • It does not impose any restrictions on the size of the data to be sent.
  • The data transfer occurs through HTTP header and hence the security also depends on the same. Hence by making use of the HTTPS method we can make sure it is secure.
  • By using the POST method we can access both ASCII and binary data.
  • The $_POST is the array which accesses all the information sent by this method.

Examples to Implement PHP POST Method

Below are the examples mentioned:

Example #1

Code:


First Name:
Last Name:
Mail ID:

Output:

PHP POST Method

Explanation: In this example, we shall see a simple HTML form submission where the form consists of 3 attributes: First Name, Last Name, and Mail ID. When the user fills all the information and then clicks on the submit button, the data entered is sent using the HTTP POST method.

Example #2

Code:

<?php if (!empty($_POST))
{
// Defining an array for the list of post values for all the
// different forms on the page
$pNameArray = array('Form1', 'Form2', 'Form3');
// To search all post identifiers that fall within $_POST
$postIdArr = array();
foreach ($pNameArray as $pName)
{
if (array_key_exists($pName, $_POST))
{
$postIdArr[] = $pName;
}
}
// There should be only one post identifier at a time and hence
// only an individual form should be used for submitting at a time
// A warning statement can be used instead of the die statement as used here
if (count($postIdArr) != 1)
{
count($postIdArr) < 1 or
// validation of count
die("\Validating count here: " .
implode(" ", $postIdArr));
// The above condition is not satisfied hence there is only one post identifier
die("\Presence of unknown post");
}
// Using switch statement to execute the codes for respective forms
switch ($postIdArr[0])
{
case 'Form1':
echo "Execute actual code for Form1.";
break;
case 'Modifyform':
echo "Execute actual code for Form2.";
break;
case 'Deleteform':
echo "Execute actual code for Form3.";
break;
}
}
else // $_POST is empty.
{
// displaying that POST is not executed
echo "POST is empty";
}
?>

Output:

PHP POST Method

Explanation: In this example, we are seeing how the POST method works for multiple forms. Hence we are using a switch statements to process different forms and all the forms should be distinct for it to process without any errors. If we need to add more forms, it just needs an entry to be made in the array and the switch statement.

Example #3

Code:

<?php if( POST["fullname"] ||
POST["weight"] || POST["height"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['fullname'] )) {
die ("The name given is invalid and should be alpha");
}
echo "Welcome ". $_POST['fullname']. "<br />";
echo "Welcome ". $_POST['height']. "<br>";
echo "You are ". $_POST['weight']. "weight in kgs";
exit();
}
?>


Full Name: Height: Weight:

Output:

PHP POST Method

Explanation: The query strings in this example are sent to the body of the HTTP message of the POST method request. Here we are specifying 3 attributes namely full name, height, and weight.

Advantages of POST method over GET method in PHP

  • The POST method is more secure as compared to GET because the information entered by the user is not shown in the URL query string or in the logs of the server.
  • The range of the data which can be passed is more in POST than in the GET method since it can send both text data and binary data such as uploading a file using the POST method. It supports many such advanced functionalities.
  • It supports various different data types like numeric, string, binary types.

Conclusion

As shown above, basically PHP Post method is used to collect form data. Hence using the HTTP Post method to send information over web servers. Since this information is never visible in the address bar it is often used to send over sensitive data over the servers. They do also have a few disadvantages in terms of performance, POST method requests are not cached and hence they are not retained in browser history.

The above is the detailed content of PHP POST Method. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools