首页 > 解决方案 > 改变

文件中的内容取决于 URL 中的字符串

问题描述

所以我有 3 个文件,即“test1.php”、“test2.php”、“test3.php”。在'index.php'我有这个代码:

<div class="nav_wrapper">
    <ul>
        <li><a href="#test1">test1</a></li>
        <li><a href="#test2">test2</a></li>
        <li><a href="#test3">test3</a></li>
    </ul>
</div>

<div class="main_content">
    <?php
        $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

        if (strpos($url, 'test1') !== false) {
            include 'test1.php';
        }
        if (strpos($url, 'test2') !== false) {
            include 'test2.php';
        }
        if (strpos($url, 'test3') !== false) {
            include 'test3.php';
        }
    ?>
</div>

并且在 test#.php 中只有一个简单<h1>Test #</h1>的内容,因此我可以查看内容是否正在更改,但它不起作用。

所以我希望每当我点击导航中的链接时,URL 当然会更改为:localhost/index.php#test1; 但内容没有改变?关于如何正确执行此操作的任何想法?JavaScript 或 PHP,谢谢

标签: javascriptphphtml

解决方案


$_SERVER['REQUEST_URI'];错了

你可以使用 GET 参数

**或使用 $_SERVER['REQUEST_URI'];

你必须使用字符串函数**

格式如下的 URL >https://IndiaOneWay.com/index.php或在本地服务器上http://localhost/folderName/1/index.php

$myURL = $_SERVER['REQUEST_URI']



$myURL  // >> http://localhost/folderName/*test2.php (trim URL part using str_replace function, replace with nothing "")

$lenthofURL = strlen($myURL); 

Get Lenth of Url strlen() [40Charecters] >> find position of "-" stripos("/")[rd position] >> Chop strings left part and store it in variable do the same with remaining


推荐阅读