2017-05-08 77 views
2

所以我試圖做一個網站,並且我使用一個PHP文件作爲索引(index.php),並且非常像控制頁面整個網站。防止str_replace評論字符串中的php代碼

由於它收到所有的請求並返回使用str_replace的頁面,它的所有工作都應該如此(因爲它使Web模板按照它應該的工作),但問題是我無法在PHP代碼中使用文件是模板的一部分,只在index.php中。

所以我的問題是,有沒有辦法阻止str_replace將php代碼轉換爲註釋?

的index.php:

<?php 

//dirs 
$pagesDir = "pages/"; 
$templatesDir = "templates/"; 
$errorsDir = "errors/"; 

if (isset($_REQUEST['page'])) { 
    if ($_REQUEST['page'] != "") 
     if (file_exists($pagesDir . $_REQUEST['page'] . ".html")) 
      $page_content = file_get_contents($pagesDir . $_REQUEST['page'] . ".html"); 
    else 
     if (file_exists($_REQUEST['page'] . ".html")) 
      $page_content = file_get_contents($_REQUEST['pages'] . ".html"); 
      else 
       echo "<h1>Page:" . $_REQUEST['page'] . " does not exist!  Please check the url and try again!</h1>"; 
} else { 
    $page_content = file_get_contents($pagesDir . "home.html"); 
} 

//PLACEHOLDER REPLACEMENT 
$page_content = str_replace("!!HEAD!!", file_get_contents($templatesDir . "head.html"), $page_content); 
$page_content = str_replace("!!BODY!!", file_get_contents($templatesDir . "body.html"), $page_content); 
$page_content = str_replace("!!FOOT!!", file_get_contents($templatesDir . "eofScripts.html"), $page_content); 

//RETURN THE CONTENT OF THE PAGE 
echo $page_content; 

新調度後的變化(這一個工程):

<?php 
$templatesDir = "templates/"; 
$pagesDir = "pages/"; 
$loggedPagesDir = "templates/logged"; 
$pageExists = false; 
$pageContent = null; 
require_once('scripts/php/db_conn.php'); 

if (isset($_REQUEST['page'])) { 
    $page = $_REQUEST['page'] . ".php"; 
} 

if (isset($_SESSION['redirect_reason'])) { 
    $dialogs->alertDialog("warningDialog", $_SESSION['redirect_reason']); 
    unset($_SESSION['redirect_reason']); 
} 

if (isset($_SESSION['user_action'])) { 
    $dialogs->alertDialog("infoDialog", $_SESSION['user_action']); 
    unset($_SESSION['user_action']); 
} 

if ($user->is_logged()) { //Only runs beyond this point if user is logged, if not, it will run the other one. 
    if (isset($_POST['logout_btn'])) { 
     $user->logout(); 
     $user->redirect("pageDispatcher.php"); 
    } 

    if (isset($page)) { 
     if ($page != "") { 
      if (file_exists($pagesDir . $page)) { 
       $pageExists = true; 
       $pageContent = ($pagesDir . $page); 
      } else { 
       echo "<h1>Page: " . $page . "does not exist! Please check the url and try again</h1>"; 
      } 
     } else { 
      $pageExists = true; 
      $pageContent = ($pagesDir . "loggedhome.php"); 
     } 
    } else { 
     $pageExists = true; 
     $pageContent = ($pagesDir . "loggedhome.php"); 
    } 
} else { //Only runs beyond this point if user isn't logged. 

    if (isset($_POST['login_btn'])) { 
     if ($user->login($_POST['email'], $_POST['password']) == false) { 
      $dialogs->loginFailed(); 
     } else { 
      $_SESSION['user_action'] = "Welcome back " . $_SESSION['user_name']; 
      $user->redirect("pageDispatcher.php"); 
     } 
    } 

    if (isset($page)) { 
     if ($page != "") { 
      if (file_exists($pagesDir . $page)) { 
       $pageExists = true; 
       $pageContent = ($pagesDir . $page); 
      } else { 
       echo "<h1>Page: " . $page . " does not exist! Please check the url and try again!</h1>"; 
      } 
     } else { 
      $pageExists = true; 
      $pageContent = ($pagesDir . "home.php"); 
     } 
    } else { 
     $pageExists = true; 
     $pageContent = ($pagesDir . "home.php"); 
    } 
} 
?> 

<html> 

<?php include($templatesDir . "head.html"); ?> 


<body> 
<?php 
if ($user->is_logged()) { 
    include($templatesDir . "loggedBody.html"); 
} else { 
    include($templatesDir . "body.html"); 
} 
include($pageContent); 
?> 
</body> 
</html> 

注意:除非是出於學習的目的,它的壞,不要使用此方法就可以了,結果很難維護,並且可能最終會變得很慢,因爲我有很多服務器端方法可以做客戶端。

+0

???發佈一些代碼或其他東西沒有意義你在問什麼 – Augwa

+0

我的代碼可能不會讓很多sence,但你有它。 – MrSanchez

+0

我認爲OP正試圖將他的模板作爲字符串進行回顯,並且他在結果頁面中看到了php標籤(和我的猜測)。 – Aioros

回答

1

您閱讀頁面的內容並回應它!不要這樣做。改用include('file.html')。只是爲了解釋起見,(如果有)折騰這樣的:

$pages=['head.html','body.html','eofScripts.html']; 
$page=$_REQUEST['page']; 
if(in_array($page,$pages)) include($page); 
else echo "<h1>Page: $page does not exist!</h1>"; 

但通常這是不好的編程習慣。正如前面的評論中所建議的那樣,使用模板引擎。

+0

它工作,但我不得不重新調整我發佈頁面的方式,除了我沒有模板引擎,因爲我不會使用模板引擎,直到我完全理解如何使用php,這個項目會最終被釋放,但我這樣做是爲了學習的目的,在發佈版本中我將使用Laravel,感謝您的幫助,我會更新我的問題以包含我的新調度程序,並且它仍然使用大量不良編程實踐,我意識到這一點,但同樣,它是爲了學習的目的。 – MrSanchez