You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过开关按钮修改database.txt文件值的实现方法

通过开关控制页面跳转(编辑database.txt实现)

我刚入门编程,想通过开关按钮编辑database.txt文件,实现用开关的开启/关闭来启用或禁用页面跳转功能。以下是现有文件内容及修改方案:

现有文件内容

database.txt

1|on|on|on|;

setting.php

<?php

<label class="switch">
  <input type="checkbox" name="switcha">
  <div class="slider"></div>
</label>

if (isset($_POST['switcha'])) {

}

<label class="switch">
  <input type="checkbox" name="switchb">
  <div class="slider"></div>
</label>

if (isset($_POST['switchb'])) {

}


<label class="switch">
  <input type="checkbox" name="switchc">
  <div class="slider"></div>
</label>

if (isset($_POST['switchc'])) {

}

?>

redirection.php

<?php

$data = file_get_contents("database.txt");
$rows = explode("|", $data);

$redirectiona = $rows[1];
$redirectionb = $rows[2];
$redirectionc = $rows[3];
    
if($redirectiona == "on"){
        header('location: 1.php');
        exit();
    }
if($redirectionb == "on"){
        header('location: 2.php');
        exit();    
    }
if($redirectionc == "on"){
        header('location: 3.php');
        exit();
    }
?>

修改方案

1. 重构setting.php

原代码存在HTML标签直接嵌套PHP的问题,且缺少表单提交、状态读取逻辑。修改后代码如下:

<?php
// 读取database.txt当前状态
$data = file_get_contents("database.txt");
$rows = explode("|", $data);
// 给开关设置默认选中状态
$statusA = $rows[1] === "on" ? "checked" : "";
$statusB = $rows[2] === "on" ? "checked" : "";
$statusC = $rows[3] === "on" ? "checked" : "";

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 获取每个开关状态,未勾选则为off
    $switchA = isset($_POST['switcha']) ? "on" : "off";
    $switchB = isset($_POST['switchb']) ? "on" : "off";
    $switchC = isset($_POST['switchc']) ? "on" : "off";
    
    // 拼接成database.txt所需格式
    $newData = "1|$switchA|$switchB|$switchC|;";
    // 写入文件
    file_put_contents("database.txt", $newData);
    
    // 提交后刷新页面,避免重复提交
    header("Location: setting.php");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>跳转设置</title>
    <!-- 添加开关样式 -->
    <style>
        .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 34px;
            margin: 10px;
        }

        .switch input { 
            opacity: 0;
            width: 0;
            height: 0;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
            border-radius: 34px;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 26px;
            width: 26px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }

        input:checked + .slider {
            background-color: #2196F3;
        }

        input:focus + .slider {
            box-shadow: 0 0 1px #2196F3;
        }

        input:checked + .slider:before {
            transform: translateX(26px);
        }
    </style>
</head>
<body>
    <!-- 表单提交开关状态 -->
    <form method="POST">
        <label class="switch">
            <input type="checkbox" name="switcha" <?php echo $statusA; ?>>
            <div class="slider"></div>
        </label>
        <span>跳转1.php开关</span>

        <label class="switch">
            <input type="checkbox" name="switchb" <?php echo $statusB; ?>>
            <div class="slider"></div>
        </label>
        <span>跳转2.php开关</span>

        <label class="switch">
            <input type="checkbox" name="switchc" <?php echo $statusC; ?>>
            <div class="slider"></div>
        </label>
        <span>跳转3.php开关</span>

        <br><br>
        <button type="submit">保存设置</button>
    </form>
</body>
</html>

2. 验证redirection.php逻辑

当前redirection.php按顺序判断开关状态,第一个为on的开关会触发对应跳转(exit()会终止脚本,后续判断不会执行)。如果需要调整跳转优先级,修改判断顺序即可,现有逻辑无需改动。

3. 权限注意事项

确保database.txt文件拥有PHP进程的读写权限,否则无法修改内容。可通过服务器文件管理器设置权限为644或755(根据服务器配置调整)。

内容的提问来源于stack exchange,提问作者Jean Guédry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:40:32