Monday, 26 August 2013

two identical simultaneous request treated as one by (?PHP) + cookies

two identical simultaneous request treated as one by (?PHP) + cookies

I wanted to provide some kind of "transactions" to my PHP website.
Simply, performing two request simultaneously by a logged-in user is
undesirable.
At first, I tested my app without any big changes, also just cookies +
code unaware of concurrency.
I have added some sleep(15) to a.php and b.php.
Requesting two pages: a.php and then b.php causes 30 second of loading,
which is expected behaviour due to cookie usage. Cookie is locked by a.php
for 15 s, so b.php waits for its 15 s.


However, requesting a.php twice lasts just 15 s. It seemed that both
request are somehow joined.
I added some SQL insert with autoincrement id, and echoed $db -> insert_id
, which was the same.


I also echoed some GET parameter: requesting a.php?text=One and
a.php?text=Two also lasts 30 seconds, while requesting a.php?text=One
twice is once again treated as one.
1.What causes such treatment?
2.Is it reliable that the client cannot perform two request because of
this behaviour?
3.If not, how should I restrict user to one instance of a.php at time?

EDIT:
Here is my code:

<?php
session_start();
if(isset($_SESSION["myUser"]) && is_object($_SESSION["myUser"]))
{
$loggedInUser = $_SESSION["myUser"];
}
sleep(15);
$db = new mysqli('localhost', 'root', '', 'test'); // :)
if($db->connect_errno > 0)
{
die('Unable to connect to database!');
}
$statement = $db->prepare("INSERT INTO curl (request) VALUES (?)");
//id is inserted by MySQL autoincrement
$request=1;
$statement->bind_param('i', $request);
$statement->execute();
$id = $db->insert_id;
echo $id;
?>

No comments:

Post a Comment