Pagproseso ng Form ng PHP
- Previous Page PHP Super Global
- Next Page PHP Form Validation
Ang mga global variable na $_GET at $_POST ng PHP ay ginagamit para sa pagkolekta ng data ng form (form-data).
PHP - Isang simple na HTML form
Ang halimbawa na ito ay nagpapakita ng isang simple na HTML form, na may dalawang input field at isang sumite button:
Example
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
Kapag napupunta ng user ang form na ito at pindutin ang button na sumusunod, ang data ng form ay ipapadala sa PHP file na may pangalan na "welcome.php" para sa pagproseso. Ang data ng form ay ipapadala sa pamamagitan ng HTTP POST method.
Upang ipakita ang inilabas na data, madaling mag-output (echo) ng lahat ng mga variable. Ang "welcome.php" na file ay tulad nito:
<html> <body> Maligayang pagdating, <?php echo $_POST["name"]; ?><br> Ang iyong email address ay: <?php echo $_POST["email"]; ?> </body> </html>
Output:
Maligayang pagdating, Bill Ang iyong email address ay Bill.Gates@example.com
Makakakuha din ng katulad na resulta sa pamamagitan ng paggamit ng HTTP GET method:
Example
<html> <body> <form action="welcome_get.php" method="get"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
"welcome_get.php" ay tulad nito:
<html> <body> Maligayang pagdating, <?php echo $_GET["name"]; ?><br> Ang iyong email address ay: <?php echo $_GET["email"]; ?> </body> </html>
Ang code na ito ay napakasimple. Gayunman, ang pinakamahalaga na nilalagay ay nawala. Kailangan mong walang kailangan na itong walang pag��syasyon sa data ng form, upang maiwasan ang mga bug sa script.
Babala:Pansin ang seguridad sa paggamit ng PHP form!
Wala dito ang kahit anong programang pag��syasyon ng form sa pahina, ito ay nagpapakita lamang kung paano ipagpadala at ipagkakaloob ang data ng form.
Gayunpaman, ang susunod na kabanata ay magtuturo sa iyo kung paano mapabuti ang seguridad ng PHP form! Ang paggawa ng wastong seguridad na pag��syasyon sa form ay napakahalaga sa paghahawak ng mga attack ng hacker at spam emails!
GET vs. POST
GET ato POST ay gumagawa ng array (halimbawa, array( key => value, key2 => value2, key3 => value3, ...))。Ang array na ito ay naglalaman ng key/value na pares, kung saan ang key ay ang pangalan ng form na kontrol at ang halaga ay ang input data mula sa user.
GET and POST are considered as $_GET and $_POST. They are superglobal variables, which means that access to them does not require consideration of scope - no special code is needed, and you can access them from any function, class, or file.
$_GET is an array of variables passed to the current script via URL parameters.
$_POST is an array of variables passed to the current script via HTTP POST.
When to use GET?
Information sent from the form via GET methodVisible to everyone(All variable names and values are displayed in the URL). GET also has a limit on the amount of information sent. The limit is about 2000 characters. However, since the variables are displayed in the URL, it is also more convenient to add the page to the bookmarks.
GET can be used to send non-sensitive data.
Note:Never use GET to send passwords or other sensitive information!
When to use POST?
Information sent from the form via POST methodInvisible to others(All names/values will be embedded in the body of the HTTP request), and also limits the amount of information sentUnlimited.
In addition, POST supports advanced features, such as multi-part binary input when uploading files to the server.
However, since the variable is not displayed in the URL, it is also impossible to add the page to the bookmarks.
Tip:Developers prefer POST to send form data.
Let's see how to handle PHP forms safely!
- Previous Page PHP Super Global
- Next Page PHP Form Validation