Skip to content

Instantly share code, notes, and snippets.

@tomookaku
Last active January 2, 2016 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomookaku/8296188 to your computer and use it in GitHub Desktop.
Save tomookaku/8296188 to your computer and use it in GitHub Desktop.
PHP連打(マルチリクエスト)対策 Redis版
<?php
$redis = null;
session_start();
$user_id = $_SESSION['user_id'];
if ($user_id) {
// my_shutdown_handlerの登録
register_shutdown_function('my_shutdown_handler');
// Redis
$redis = new Redis();
// Redis接続
$redis->pconnect("127.0.0.1", 6379);
$key = "CONTINUOUS_DETECTION-" . $user_id;
// インクリメント
$i = $redis->incrBy($key, 1);
// 値保持時間の設定(5秒)
$redis->expire($key, 5);
if ($i && $i > 1) {
exit; // 連打検出
}
// OK
}
/**
* my_shutdown_handler function.
*
* @access public
* @return void
*/
function my_shutdown_handler() {
global $user_id, $redis;
// デクリメント
$redis->decrBy("CONTINUOUS_DETECTION-" . $user_id, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment