0

以下のコードは次のように機能します。

http://www.website.com/?page=homeからコンテンツをプルしますhttp://www.website.com/home.php

- また -

http://www.website.com/?page=Aboutからコンテンツをプルしますhttp://www.website.com/About.php

コード:

// Set the default name 
$action = 'home'; 

// Specify some disallowed paths 
$disallowed_paths = array('admin'); 

if (!empty($_GET['page'])) { 
    $tmp_action = basename($_GET['page']); 
    // If it's not a disallowed path, and if the file exists, update $action 
    if (!in_array($tmp_action, $disallowed_paths) && file_exists("{$tmp_action}.php")) 
        $action = $tmp_action; 
} 
// Include $action 
include("$action.php"); 
?>

上記のコードはすべてのページで正常に機能しますが、コンテンツをプルするために追加する必要のあるカスタムリンクが1つあります。http://www.website.com/?page=Searchhttp://www.website.com/search/search.phphttp://www.website.com/Search.php

どうやってやるの?

ありがとう

4

1 に答える 1

3

2 つのオプションがあります。いつでもサブ フォルダーでファイルを探すことができます。または、disallowed_pa​​ths で行うのと同じように、独自のパスを持つページの特別なリストを作成することもできます。

$special_paths = array(
    "search" => "search/search.php",
);

if(in_array($_GET['page'], $special_paths)) {
    $action = $special_paths[$_GET['page']];

これは、将来別の特別なページが別のパスで存在する場合、それを配列に追加するだけでよいことを意味します。

完全なコードは次のようになります。

<?php
    // Set the default name
    $action = 'home.php';

    // Specify some disallowed paths
    $disallowed_paths = array('admin');

    // special paths
    $special_paths = array(
            "search" => "search/search.php",
            "special_b" => "a/different/lengthier/path/file.php",
    );

    if (!empty($_GET['page'])) {
            $tmp_action = basename($_GET['page']);

            // If it's not a disallowed path, and if the file exists, update $action
            if (!in_array($tmp_action, $disallowed_paths) && file_exists("{$tmp_action}.php")) {
                    $action = $tmp_action.".php";
            } elseif(isset($special_paths[$tmp_action])) {
                    $action = $special_paths[$tmp_action];
            }
    }

    // Include $action
    include("$action");
于 2012-11-03T14:57:55.780 に答える