在某帝國面試的時候問題了這個題: 怎么處理post提交重復的問題, 后來跟@暖陽交流,他說記錄時間,我沒有明白,我想的是用session在表單頁面記錄下,然后提交頁面判斷,如果相等則視為成功,并清空session,但有個問題是如果表單頁面是html的呢,乍辦?要不調個php驗證的頁面?類似驗證碼的功能. 還有的說用 header頭設置過期時間...但沒試.以下是我php寫的,經測試可用.
代碼如下:
<?php
//開啟session
session_start();
//如果有提交標識
if(isset($_get['action']) && $_get['action'] === 'save'){
//如果有session且跟傳過來的值一樣才算提交
if(isset($_session['__open_auth']) && isset($_post['auth']) && $_session['__open_auth'] == $_post['auth']){
print_r($_post);
$_session['__open_auth'] = null;//清空
} else {
//走起
header(location: post.php);
}
exit();
}
//授權
$auth = $_session['__open_auth'] = time();
?>
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>post</title>
</head>
<body>
<form action=post.php?action=save method=post>
<ul>
<li>
<input type=hidden name=auth value=<?php echo $auth;?>>
<input type=text name=username>
</li>
<li>
<input type=password name=userpass>
</li>
<li>
<input type=submit value=走起>
</li>
<li>
<?php echo time(); ?>
</li>
</ul>
</form>
</body>
</html>
更多信息請查看IT技術專欄