Php 更改第页的标题

Php 更改第页的标题,php,url,get,Php,Url,Get,我有以下更改页面标题的代码: class.php <?php class Title_And_Page { public $pagekey; public $title; public $page; private $pages = array( 'start_page' => array("Startsida", "start_page.php"),

我有以下更改页面标题的代码:

class.php

<?php
    class Title_And_Page
    {
        public $pagekey;
        public $title;
        public $page;
        private $pages = array(
                    'start_page'    => array("Startsida", "start_page.php"),
                    'products'      => array("Produkter", "products.php"),
                    'max_ot'        => array("Max-OT", "max_ot.php"),
                    'blog'          => array("Blogg", "blog.php"),
                    'tools'         => array("Verktyg", "tools.php"),
                    'about_us'      => array("Om oss", "about_us.php"));

        public function __construct($pagekey)
        {
            $this->pagekey      = $pagekey;
        }

        public function setTitle()
        {
            if(array_key_exists($this->pagekey, $this->pages))
            {
                $this->title = $this->pages[$this->pagekey][0]; //Returns the value in title, that it gets when the constructs is run
                return $this->title;
            }
        }

        public function includePage()
        {
            if(array_key_exists($this->pagekey, $this->pages))
            {
                $this->page = $this->pages[$this->pagekey][1]; //Returns the value in page, that will be included
                return $this->page;
            }
        }
    }
?>

下面是我的一些代码,来自我的index.php

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

    $page_title = new Title_And_Page($page);
<title><?= $page_title->setTitle(); ?></title>
<li id="info"><a href="?page=products" class='clickme'>Produkter</a></li>
        <li id="info"><a href="?page=max_ot">MAX-OT</a></li>
        <li id="info"><a href="?page=blog">Blogg</a></li>
        <li id="info"><a href="?page=tools">Verktyg</a></li>
        <li id="info"><a href="?page=about_us">Om oss</a></li>
if(isset($\u GET['page']))
{
$page=$_GET['page'];
}
$page\u title=新标题和页面($page);
  • 这很有效。然而,我在博客页面中有文章包含“阅读更多”链接。当我点击一个“阅读更多”链接时,URL变为:index.php?page=blog&readmore_from_firstpage=1&article_header=Vilken kolhythatär bäst attäta efter träningen


    如何将页面标题更改为上面所示的$GET['article\u header']中的值?

    只需扩展GET检查:

    if(isset($_GET['article_header']))
    {
        $page = $_GET['article_header'];
    }
    elseif(isset($_GET['page']))
    {
        $page = $_GET['page'];
    }
    
    但是,由于您要在类中检查该页面是否为白名单,您需要添加另一个变量来强制避免此类检查,或者如果存在
    文章标题
    ,则只需打印标题即可

    下面是后者的一个例子:

    $avoidClass = false;
    if(isset($_GET['article_header']))
    {
        $page = $_GET['article_header'];
        $avoidClass = true;
    }
    elseif(isset($_GET['page']))
    {
        $page = $_GET['page'];
    }
    
    然后在HTML中:

    <title><?= $avoidClass ? $page : $page_title->setTitle(); ?></title>
    
    HTML

    
    
    if(isset($_GET['article_header']))
    {
        $page_title = $_GET['article_header'];
    }
    elseif(isset($_GET['page']))
    {
        $page_title = new Title_And_Page($_GET['page'])->setTitle();
    }
    else
    {
        $page_title = 'Default title here';
    }
    
    <title><?= $page_title ?></title>