0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Basic認証でツボった話

0
Last updated at Posted at 2026-10-12

Apache環境でBasic認証をしようとしたらディレクティブ設定ができなかった

仕方がないので、1段下にフォルダを作って、index.phpで指定キーワードでダウンロードするプログラムGeminiに書いてもらった。

index.php
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ファイルダウンロード</title>
    <style>
        body { font-family: sans-serif; margin: 50px; }
        .form-container { max-width: 400px; padding: 20px; border: 1px solid #ccc; border-radius: 5px; }
        .input-group { margin-bottom: 15px; }
        .input-group label { display: block; margin-bottom: 5px; }
        .input-group input { width: 100%; padding: 8px; box-sizing: border-box; }
        .btn { padding: 10px 15px; background: #007bff; color: white; border: none; border-radius: 3px; cursor: pointer; }
        .btn:hover { background: #0056b3; }
        .error { color: red; margin-bottom: 15px; }
    </style>
</head>
<body>

<div class="form-container">
    <h2>ファイルダウンロード</h2>
    
    <!-- エラーメッセージの表示 -->
    <?php if (isset($_GET['error']) && $_GET['error'] == 'invalid'): ?>
        <p class="error">キーワードが正しくありません。</p>
    <?php endif; ?>

    <form action="download.php" method="POST">
        <div class="input-group">
            <label for="keyword">ダウンロード用キーワード:</label>
            <input type="text" id="keyword" name="keyword" required autocomplete="off">
        </div>
        <button type="submit" class="btn">ダウンロードする</button>
    </form>
</div>

</body>
</html>
download.php
<?php
// 1. 正解のキーワードと、それに対応するファイルの設定
$correct_keyword = "secret2026"; // 任意のキーワードに変更してください
$filepath = "./files/sample.pdf";  // 実際のファイルのパスに変更してください
$filename = "download_file.pdf";   // 保存時のファイル名(日本語も可)

// 2. POSTリクエスト以外、またはキーワードが未送信の場合はトップへ戻す
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['keyword'])) {
    header("Location: index.php");
    exit;
}

// 3. キーワードの判定
if ($_POST['keyword'] !== $correct_keyword) {
    // 間違っていたらエラーをつけて入力画面に戻す
    header("Location: index.php?error=invalid");
    exit;
}

// 4. ファイルの存在確認
if (!file_exists($filepath)) {
    die("エラー:ダウンロード対象のファイルが存在しません。");
}

// 5. ダウンロード用のHTTPヘッダーを出力してファイルを送信
// ブラウザのキャッシュを無効化
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));

// ファイルを出力(ダウンロード開始)
readfile($filepath);
exit;
?>

このとき置くフォルダ名を予測不能な長い名前にしておく。

$correct_keyword = "secret2026"; // 任意のキーワードに変更してください
$filepath = "./files/sample.pdf";  // 実際のファイルのパスに変更してください
$filename = "download_file.pdf";   // 保存時のファイル名(日本語も可)

この部分を配列化してindex.php側に配列引数を指定すれば複数対応可能になる。
ちょっと工夫はいるけどね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?