PHP runs two requests per request when using rewrite rule
I noticed that PHP has been inserting the same data twice, even with a simple query, below is my index file:
<?php
require("constants.php"); //contains database settings
$database = new PDO("$type:host=$host;dbname=$name", $user, $pass);
$query = $database->prepare('INSERT into test (test) VALUES (?)');
$query->execute(array(rand()));
echo $database->lastInsertId();
The test
table has an auto increment column id
and a开发者_Python百科 varchar column test
. I'm using WAMP to run PHP and mysql.
Each request to the page inserts two entries with different values (from the rand()
call). Only the first insert id is echoed. This behaviour is the same for Chrome, Firefox and IE.
It's caused by a rewrite rule:
RewriteEngine on
RewriteRule .* index.php
I'm assuming there's a file like robots.txt or favicon.ico which is requested transparently.
Do you have any kind of rewrite/errordocument rules that could redirect to index.php? Could it be the page/browser trying to load a different resource (favicon, etc...) being redirect to index?
You should be using a form token/nonce to prevent this and reply-attacks.
http://www.phpro.org/tutorials/Preventing-Multiple-Submits.html
精彩评论