dsgoj7457 2017-06-19 17:47
浏览 68

包括CSS文件取决于php包含变量

I am trying to include a specific css file depending on the URL provided by the browser.

My website has the following format:

Header information

php include for content

Footer information

I am doing this so when I change my header menu links, I don't have to change every page on the website.

The php include code is as follows:

    <?php
        $page = $_GET['page'];
        $pages = array('main', 'contact');
        if (!empty($page)) {
            if(in_array($page,$pages)) {
                $page .= '.php';
                include($page);
            }
            else {
            include('404.php');
            }
        }
        else {
            include('main.php');
        }
    ?>

The URL is made up as follows

index.php?page= for example index.php?page=contact

As part of the php include code, when the page=???? element isn't within the array it loads page 404.php which is an error page.

As can be seen, the array currently contains two pages, main (the home page content) and contact (the contact form). If a user tries to load a page called test (index.php?page=test) my webpage will display the 404 page (however doesn't load index.php?page=404 - the URL in browser stays as index.php?page=test.

What I'm trying to achieve is to load 404.css file when page=test(or any other page name not included in the array) is loaded.

Does anyone know how i'd go about achieving this? I've tried writing an if statement in the header to load 404.css however I was using strstr that fetches the browser URL (so test, not 404), therefore the correct css file doesn't get loaded.

Any help is much appreciated (or a better way to achieve what i'm trying to do).

Thanks in advance

Iain

  • 写回答

1条回答 默认 最新

  • doudoulb1234 2017-06-19 18:10
    关注

    An easy way of doing this is to make use of the ob_* php functions like bellow:

    PHP get all the information from within the ob_start and the ob_get_clean functions and concatenate them all together as a single variable $_page_content, that variable can be used anywhere you need.

    <?php
    $page   = $_GET['page'];
    $pages  = array('main', 'contact');
    
    if (!empty($page)) 
    {
        if(in_array($page, $pages)) 
        {
            ob_start('ob_gzhandler');
            ?>
            <link rel="stylesheet" href="css/<?=$page?>.css" type="text/css">
            <?
    
            include($page . '.php');
            $_page_content = ob_get_clean();
    
            echo $_page_content;
        }
        else 
        {
            include('404.php');
        }
    }
    else 
    {
        include('main.php');
    }
    

    ?>

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)