Frequently Asked Questions about Form Submission

Frequently Asked Questions about Form Submission

1. Let PHP run regularly

Edit the following code and save it as test.php:

$fp = @fopen("test.txt", "a+"); fwrite($fp, date("Ymd H:i:s"). "Let PHP run regularly!/n"); fclose($fp ); ?>

2. How to retain the information filled in after the PHP submission fails. The most commonly used method is to use the cache method. This method may cause problems if the network speed is slow. The best way is to use ajax.

Use the header header to set the cache control header Cache-control.

header('Cache-control: private, must-revalidate');//Support page jump back

Use the session_cache_limiter method.

session_cache_limiter('private, must-revalidate');//To be written before the session_start method

The following describes the session_cache_limiter parameter:

The meanings of several parameters in session_cache_limiter are: nocache: Of course it is not cached (for example: form information is cleared), but public variables can be cached private: privately cached (for example: form information is retained but valid during the lifetime) private_no_cache ´╝ÜPrivate method but not expired (form information is retained) publice: public method, (form information is also retained)

Set cache expiration time: session_cache_expire function setting, the default is 180 minutes.

Frequently encountered problems:

1. session_cache_limiter("private"); The form information is retained, but if I modify the information that has been submitted, the information presented on the form page is still the information in the cache, and it cannot be automatically refreshed in time. If you do not use session_cache_limiter("private"); Keep form information


Add in front of session_start

session_cache_limiter( "private, must-revalidate" );

Prevent duplicate submission of forms

session_start(); if (empty($_SESSION['ip'])) {//The first write operation, determine whether the IP address is recorded, so as to know whether to write to the database $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];//Writing for the first time, pave the way for subsequent refresh or back judgment//...........//Write to database operation} else {//There has been an operation after the first write, and it will no longer be written to the database echo'Please do not refresh and go back again';//Write some hints or other things that have been written}

Another way we can use ajax to instantiate

Reference: form submission FAQ-Cloud + Community-Tencent Cloud