如何使用函数修复页面重定向

问题描述 投票:0回答:0

iv 试图修复它,但无法修复,所以问题是 我正在尝试将 ?page=mypage 添加到我的侧边栏 首先 iv 将其添加到 .htaccess

    RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

这是我的侧边栏

<div class="col-md-3 col-lg-2 sidebar d-flex flex-column">
    <div class="sidebar-logo">
        <img src="path/to/your/logo.png" alt="Logo">
    </div>
    <div class="sidebar-sticky">
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link active" href="index.php?page=dashboard">
                    <i class="fa fa-home"></i> Dashboard
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="index.php?page=settings">
                    <i class="fa fa-settings"></i> Settings
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="index.php?page=users">
                    <i class="fa fa-shopping-users"></i> Users
                </a>
            </li>
            <!-- Add more sidebar menu items as needed -->
        </ul>
    </div>
    <div class="mt-auto"></div> <!-- Empty div to push the sidebar to the bottom -->
</div>

这是控制 ?page=

的函数
function include_page() {
    $allowedPages = array('error', 'dashboard', 'settings', 'users');

    if (isset($_GET['page'])) {
        $page = $_GET['page'];

        // Validate and sanitize the requested page
        if (!in_array($page, $allowedPages)) {
            // Invalid page, redirect to a default page or show an error message
            header('Location: index.php?page=error');
            exit;
        }

        $filePath = __DIR__ . '/pages/' . $page . '.php';

        if (file_exists($filePath)) {
            include($filePath);
            return;
        }
    }

    // Handle error: page not found
    header('Location: index.php?page=error');
    exit;
}

并且 iv 将其添加到我的 index.php

include_page();

它不允许我去 index.php 或任何页面它总是将我重定向到 index.php?page=error

iv 尝试了一切都没有解决问题

iv 尝试去掉错误页面还是一样

php function redirect dashboard
© www.soinside.com 2019 - 2024. All rights reserved.