Clearing _POST array fully
Yes, that is fine. $_POST
is just another variable, except it has (super)global scope.
$_POST = array();
...will be quite enough. The loop is useless. It's probably best to keep it as an array rather than unset it, in case other files are attempting to read it and assuming it is an array. How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP?
The request header contains some POST data. No matter what you do, when you reload the page, the rquest would be sent again.
The simple solution is to redirect to a new (if not the same) page. This pattern is very common in web applications, and is called Post/Redirect/Get. It's typical for all forms to do a POST, then if successful, you should do a redirect.
Try as much as possible to always separate (in different files) your view script (html mostly) from your controller script (business logic and stuff). In this way, you would always post data to a seperate controller script and then redirect back to a view script which when rendered, will contain no POST data in the request header.
cleaning $_POST variables
What you're doing isn't enough. See here.
Clear the form field after successful submission of php form
They remain in the fields because you are explicitly telling PHP to fill the form with the submitted data.
<input name="firstname" type="text" placeholder="First Name" required="required"
value="<?php echo $_POST['firstname'];?>">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ HERE
Just remove this, or if you want a condition to not do so make a if
statement to that echo
or just cleanup the $_POST
fields.$_POST = array(); // lets pretend nothing was posted
Or, if successful, redirect the user to another page:header("Location: success.html");
exit; // Location header is set, pointless to send HTML, stop the script
Which by the way is the prefered method. If you keep the user in a page that was reached through a POST
method, if he refreshes the page the form will be submitted again. Is it possible to trigger an error when $_POST or another superglobal is accessed?
You can use ArrayAccess
Example 1 :
$_POST = new SUPER($_POST);
$_POST['hello'] = "Hello World"; // This would trigger error ;
Example 2 : a.php?var=1&var2=2
$_GET = new SUPER($_GET);
echo $_GET['var'] ; // returns 1
echo $_GET['var2'] ; // returns 2
$_GET['var3'] = 2 ; //return error
Class Used class SUPER implements \ArrayAccess {
private $request = array();
public function __construct(array $array) {
$this->request = $array;
}
public function setRequest(array $array) {
$this->request = $array;
}
public function offsetSet($offset, $value) {
trigger_error("Error: SUPER GLOBAL data cannot be modified");
}
public function offsetExists($offset) {
return isset($this->request[$offset]);
}
public function offsetUnset($offset) {
unset($this->request[$offset]);
}
public function offsetGet($offset) {
return isset($this->request[$offset]) ? $this->request[$offset] : null;
}
}
Related Topics
Combining And/Or Eloquent Query in Laravel
Looping Through a Simplexml Object, or Turning the Whole Thing into an Array
How to Generate a Round Robin Tournament in PHP and MySQL
How to Send HTML Mails Using Pear Mail
Rename Multiple Order Statuses in Woocommerce
Random Float Number Between 0 and 1.0 PHP
Php: How to Read a File Live That Is Constantly Being Written To
What Are My Options to Check for Viruses on a PHP Upload
How to Get the Session Id in Laravel
How to Put Double Quotes Inside a String Within an Ajax JSON Response from PHP
How Does PHP Max_Execution_Time Work
Different Timezone_Types on Datetime Object
PHP - Simplexml - Addchild with Another Simplexmlelement
I Want to Find Current Location of User in PHP