如何使用PHP查询字符串动态加载内容? [英] how to load content dynamically using PHP Query Strings?

查看:92
本文介绍了如何使用PHP查询字符串动态加载内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力避免iFrames开发网站。我尝试了很多示例代码,但似乎没有任何工作。
如果你能告诉我确切的位置以及如何放置代码我会很感激:)

i'm trying to avoid iFrames on the developing of a website. I've tried many sample codes but nothing seems to work. if you could tell me exactly where and how to put the code i would appreciate :)

提前致谢。

PS:我正在使用的服务器一切正常。

PS: everything is ok with the server i'm working it.

<div id="menuWork">
            <h1>Web:</h1>
                <ul>

            <!--links to dynamic content -->

                    <li><a href=">&#62;REN</a></li>
                    <li><a href="" id="" >&#62;Vasco Gaspar</a></li>
                    <li><a href="">&#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">

        <!-- Content Area where dynamic content would appear -->



        </div>


推荐答案

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

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.

这篇关于如何使用PHP查询字符串动态加载内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆