且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PHP查询字符串动态加载内容?

更新时间:2023-11-15 12:14:10

最简单的方法就像用户评论的那样你的问题。使服务器包含基于查询字符串值的特定文件。这些包含文件将包含您的内容,每次单击链接时,整个页面都将使用动态内容进行刷新。例如:

The simplest way is something like what one user commented on your question. Make the server include particular files based on the query string value. Those include files will contain your content and every time a link is clicked the whole page will be refreshed with the dynamic content. Eg:

<?php
    $page = $_REQUEST['p'];
    $valid_pages = array('first-page-name', 'second-page-name', 'third-page-name', '...etc.');
    switch ($page) {
        case 'first-page-name':
            $title = "First Page Name";
            break;
        case 'second-page-name':
            $title = "Second Page Name";
            break;
        case 'third-page-name':
            $title = "Third Page Name";
            break;
        default:
            $title = "Some Default Title For Your Site";
    }
?><!DOCTYPE html>
<html>
    <head>
        <title><?php echo $title; ?></title>
    </head>
    <body>
        <div id="menuWork">
        <h1>Web:</h1>
            <ul>

        <!--links to dynamic content -->

                <li><a href="?p=first-page-name">&#62;REN</a></li>
                <li><a href="?p=second-page-name">&#62;Vasco Gaspar</a></li>
                <li><a href="?p=third-page-name">&#62;GF Guitars</a></li>
            </ul>

         <h1>App's:</h1>
            <ul>
                <li><a href="">&#62;DesignerTools</a></li>
                <li><a href="">&#62;Artec21</a></li>
            </ul>

         <h1>Print:</h1>
            <ul>
                <li><a href="">&#62;Self-Promoting Poster</a></li>
                <li><a href="">&#62;Another Day Will Come</a></li>
                <li><a href="">&#62;Qiasmo</a></li>
                <li><a href="">&#62;Logo</a></li>

            </ul>

         <h1>Other:</h1>
            <ul>
                <li><a href="">&#62;Chapéu é o nome (video)</a></li>
                <li><a href="">&#62;Papá Wrestling <br />(video-Special FX)</a></li>                    
        </div><!-- #menuWork -->

        <div id="content">
<?php   if (in_array($page, $valid_pages)) {
            include('pages/' . $page . '.php');
        } else { ?>
            <h2>Page not found</h2>
            <p>The page you requested was not found on this server.</p>
<?php   } ?>
        </div>
    </body>
</html>

然后,您可以将每个页面的内容放在网站的/ pages /文件夹中的文件中匹配通过 p =< your_filename_here> 在URL中传递的字符串。 (例如:'first-page-name'将加载 /pages/first-page-name.php )I包含一系列允许的文件名,以防止攻击者包含任意文件。这是一个非常基本的例子。希望你能从中学习并扩展它。

Then you would place your content for each page in a file in the /pages/ folder of your site matching the string that is passed in the URL via p=<your_filename_here>. (eg: 'first-page-name' would load /pages/first-page-name.php) I have included an array of permitted filenames so as to prevent attackers from including arbitrary files. This is a very basic example. Hopefully you can learn from it and expand upon it.