0

私はこのチュートリアルを使用して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.htmlなど以外の命名構造を使用したいのですが、 page_2.htmlHTMLは次のようになります。

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」です。最初の3つのリンクが機能するようにPHPを書き直すにはどうすればよいですか?

Javascript

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');
            }
        }

    });

}
4

3 に答える 3

1

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!';
于 2012-05-28T22:43:54.360 に答える
1

それを機能させるには、これ以上のものを変更する必要があります。

  1. ポンド記号以外のURLから何も削除されないようにJSを変更します。

    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
于 2012-05-28T22:44:54.530 に答える
0

PHP を次のように変更します。

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

ハッシュタグ全体を使用しているため、#about は次のようになります: pages/about.html

これを JavaScript 関数の loadPage で変更します

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

になる

url=url.replace('#','');
于 2012-05-28T22:39:57.067 に答える