English 中文(简体)
disable repeated call using getmethod() in php when page is refreshed
原标题:

I m new to PHP and in order to learn the language and the concepts I m working on a e-commerce website with a shopping cart, etc. In this site I have items, when an item is clicked, the id of the item is sent via the GET method to the shopping cart page. Using this id, I add the item to the shopping cart(table in db) and it works fine.

<a href="do_shoppingcart.php?id=<?php echo "$itm_id"; ?>">

The issue is; if the user clicks the refresh button, the item is added again to the shopping cart. Do you think that disabling the refresh button or F5 button is a good option? what must i do to prevent the user from adding the item to the shopping cart when the page is refreshed? In forms I ve noticed that "(isset($_POST[ Submit ])){}" is helpful, but for the GET method this doesn t work accordingly.

Your help is appreciated.

问题回答

The safest way (also helpful to prevent CSRF attacks) is to add a token as hidden field to your form. Then, in the processing script, only add the item to the database if that token does not exist yet...

The token could be created by something like this:

$token = sha1(uniqid());

Appended to your link:

echo  <a href="process.php?id= .$id. &token= .$token;

Then, when processing, you query your database for a line with that token.

SELECT 1 FROM table WHERE token= abc.... 

If this query returns a result, don t process anything else...

you should do destructive actions with POST, reserve GET for idempotent operations.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签