Last active
January 2, 2016 11:19
-
-
Save tomookaku/8296188 to your computer and use it in GitHub Desktop.
PHP連打(マルチリクエスト)対策
Redis版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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