0

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.

4

2 に答える 2

0

最も安全な方法 (CSRF 攻撃の防止にも役立ちます) は、トークンを非表示フィールドとしてフォームに追加することです。次に、処理スクリプトで、そのトークンがまだ存在しない場合にのみ、アイテムをデータベースに追加します...

トークンは、次のような方法で作成できます。

$token = sha1(uniqid());

リンクに追加:

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

次に、処理時に、そのトークンを使用してデータベースにクエリを実行します。

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

このクエリが結果を返す場合は、他に何も処理しないでください...

于 2009-12-01T14:51:32.557 に答える
-1

POST で破壊的なアクションを実行し、べき等操作のために GET を予約する必要があります。

于 2009-12-01T14:49:30.467 に答える