PHP:根据主题标签创建变量


PHP: Create variable based on hashtag

我正在使用本教程创建一个ajax站点,并且正在努力使用PHP。这是提供的代码:

.PHP

if(!$_POST['page']) die("0");
$page = (int)$_POST['page'];
if(file_exists('pages/page_'.$page.'.html'))
echo file_get_contents('pages/page_'.$page.'.html');
else echo 'There is no such page!';

我想使用除page_1.htmlpage_2.html等以外的命名结构。我的 HTML 看起来像这样:

.HTML

<ul id="navigation">
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#services">Services</a></li>
    <li><a href="#page4">Page 4</a></li>
</ul>

现在唯一有效的链接是"第 4 页"。我将如何重写 PHP 以使前三个链接正常工作?

爪哇语

var default_content="";
$(document).ready(function(){
    checkURL();
    $('ul li a').click(function (e){
            checkURL(this.hash);
    });
    //filling in the default content
    default_content = $('#pageContent').html();

    setInterval("checkURL()",250);
});
var lasturl="";
function checkURL(hash)
{
    if(!hash) hash=window.location.hash;
    if(hash != lasturl)
    {
        lasturl=hash;
        // FIX - if we've used the history buttons to return to the homepage,
        // fill the pageContent with the default_content
        if(hash=="")
        $('#pageContent').html(default_content);
        else
        loadPage(hash);
    }
}

function loadPage(url)
{
    url=url.replace('#page','');
    $('#loading').css('visibility','visible');
    $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        dataType: "html",
        success: function(msg){
            if(parseInt(msg)!=0)
            {
                $('#pageContent').html(msg);
                $('#loading').css('visibility','hidden');
            }
        }
    });
}

只有page4工作,因为它期望脚本像page_number.html一样命名。 您的home, about, services与该模式不匹配。 要使它们也正常工作,您需要更改file_get_contents()调用以允许page/anything.html

首先要修改的是发布以下内容的函数:

function loadPage(url)
{
    // Instead of stripping off #page, only 
    // strip off the # to use the rest of the URL
    url=url.replace('#','');
    $('#loading').css('visibility','visible');
    $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        dataType: "html",
        success: function(msg){
            if(parseInt(msg)!=0)
            {
                $('#pageContent').html(msg);
                $('#loading').css('visibility','hidden');
            }
        }
    });
}

现在,这在 PHP 中引入了安全风险。 您需要验证 $_POST['page'] 的值是否严格为字母数字,以便没有人可以像 ../../../../somefile 一样注入文件名来读取您的文件系统。 使用下面的表达式将允许您使用任何字母和数字字符命名文件,但会拒绝点和空字节,这是文件路径注入/目录遍历攻击的主要危险。

if(empty($_POST['page'])) die("0");
// Remove the typecast so you can use the whole string
//$page = (int)$_POST['page'];
// Just use the post val.  This is safe because we'll validate it with preg_match() before use...
$page = $_POST['page'];
// And validate it as alphanumeric before reading the filesystem
if (preg_match('/^[a-z0-9]+$/i', $_POST['page']) && file_exists('pages/'.$page.'.html')) {
  // Remove the page_ to use the whole thing
  echo file_get_contents('pages/'.$page.'.html');
}
else echo 'There is no such page!';

您需要更改的不仅仅是此内容才能使其正常工作。

  1. 更改 JS,使其不会从 URL 中删除除井号以外的任何内容:

    url=url.replace('#page','');
    

    需要:

    url=url.replace('#','');
    
  2. 更改 PHP 以处理字符串:

    if(!$_POST['page']) die("0");
    // Only allow alphanumeric characters and an underscore in page names
    $page = preg_replace( "/[^'w]/", '', $_POST['page']);
    if(file_exists('pages/'.$page.'.html'))
        echo file_get_contents('pages/'.$page.'.html');
    else echo 'There is no such page!';
    
  3. 现在在pages/目录中创建页面。

    • <li><a href="#home">Home</a></li>home.html
    • <li><a href="#about">About</a></li>about.html
    • <li><a href="#services">Services</a></li>services.html
    • <li><a href="#page4">Page 4</a></li>page4.html

将 PHP 更改为:

$page = $_POST['page'];
if(file_exists('pages/'.$page.'.html'))
echo file_get_contents('pages/'.$page.'.html');

现在您正在使用整个哈希标记,因此 #about 将导致:pages/about.html

在javascript函数加载页面中更改此内容

url=url.replace('#page','');

成为

url=url.replace('#','');