PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_MULTI_STATEMENTS => false ); $pdo = new PDO('mysql:charset=utf8mb4;dbname='.DB_NAME.';host='.DB_HOST , DB_USER, DB_PASS, $option); } catch(PDOException $e) { // 接続エラーのときエラー内容を取得する $error_message[] = $e->getMessage(); } // 管理者としてログインしているか確認 if( empty($_SESSION['admin_login']) || $_SESSION['admin_login'] !== true ) { // ログインページへリダイレクト header("Location: ./login.php"); exit; } require_once 'authcode/GoogleAuthenticator.php'; if(empty($_SESSION['secretcode'])){ $authcode = new PHPGangsta_GoogleAuthenticator(); $secret = $authcode->createSecret(); $_SESSION['secretcode'] = $secret; }else{ $authcode = new PHPGangsta_GoogleAuthenticator(); $secret = $_SESSION['secretcode']; } if( !empty($_POST['btn_submit']) ) { $chkauthcode = new PHPGangsta_GoogleAuthenticator(); //二段階認証の確認 $userauthcode = $_POST['usercode']; $discrepancy = 2; $checkResult = $chkauthcode->verifyCode($secret, $userauthcode, $discrepancy); if ($checkResult) { if( empty($error_message) ) { $secret = $_SESSION['secretcode']; // トランザクション開始 $pdo->beginTransaction(); try { // SQL作成 $stmt = $pdo->prepare("UPDATE account SET authcode = :authcode WHERE userid = :userid"); $stmt->bindValue(':authcode', $secret, PDO::PARAM_STR); // ユーザーIDのバインド(WHERE句に必要) $stmt->bindValue(':userid', $userid, PDO::PARAM_STR); // SQLクエリの実行 $res = $stmt->execute(); // コミット $res = $pdo->commit(); } catch (Exception $e) { // エラーが発生した時はロールバック $pdo->rollBack(); } if ($res) { if (isset($_SERVER['HTTP_COOKIE'])) { $cookies = explode(';', $_SERVER['HTTP_COOKIE']); foreach($cookies as $cookie) { $parts = explode('=', $cookie); $name = trim($parts[0]); setcookie($name, '', time()-1000); setcookie($name, '', time()-1000, '/'); } } $userid = ""; // リダイレクト先のURLへ転送する $url = 'success.php'; header('Location: ' . $url, true, 303); exit; } else { $error_message[] = '更新に失敗しました。'; } // プリペアドステートメントを削除 $stmt = null; } } else { $error_message[] = "二段階認証が出来ませんでした。再度お試しください。"; } } // データベースの接続を閉じる $pdo = null; ?>
以下の二次元コードより二段階認証をセットアップしてください。
セットアップが完了したら入力ボックスにコードを入力して「次へ」ボタンを押してください!
注意:まだ二段階認証の設定は終わっていません。次へを押すと設定が完了します。
・