Remember HTML Forms like the example below:
<html>
<head></head>
<body>
<form action="authenticate.php" method="POST">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
This form gets data from user input. PHP obtains these data using the HTTP POST
and GET
method. You can then display these data as in the example below. This would be the script on the authenticate.php file. These data can be displayed or used in other ways such as storing in a database.
<?php
echo $_POST['username'].PHP_EOL;
echo $_POST['password'].PHP_EOL;
You can also get the $_POST
or $_GET
data using the code below. Notice that the action
part is left blank.
<?php
if (isset($_POST['submit'])) {
echo $_POST["username"]."<br>";
echo $_POST["email"]."<br>";
}
?>
<html>
<head></head>
<body>
<form action="" method="POST">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
$_POST vs $_GET
Both the $_POST
and $_GET
method sends a request to the server, but the data sent by $_POST
method will not be visible in the URL. This difference between the two methods gives the $_POST
method more security since user data is not visible in the URL query string.
With respect to data sent to the server, the $_POST
method can pass much larger data than $_GET
. This data may be in text or binary form.