2017-05-08 4 views
2

私はウェブサイトを作ろうとしており、インデックス(index.php)としてphpファイルを使用しています。ウェブサイト全体文字列に入っているPHPコードにstr_replaceがコメントするのを防ぎます。

を使用してすべてのリクエストを受け取りますので、それはすべて正常に動作します(Webテンプレートがうまく動作するようになっています)。問題は私がPHPのコードをテンプレートの一部であるファイルは、index.phpにのみあります。

私の質問は、str_replaceがPHPコードをコメントに変えるのを防ぐ方法がありますか?

のindex.php:

<?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> 

は注:それは学習の目的のためでない限り、このメソッドを使用しないでください、その悪い、できる変更(この1つは動作します)後

<?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; 

新しいディスパッチャ維持するのが非常に困難であることが判明し、私はクライアントサイドでできる多くのサーバー側の方法があるので、おそらく遅くなるでしょう。

+0

???いくつかのコードや何かを投稿するとあなたが何を求めているのか理解できません – Augwa

+0

私のコードはおそらくたくさんの意味を作っていませんが、そこにはそれがあります。 – MrSanchez

+0

私は、OPがテンプレートを文字列としてエコーしようとしていると思って、結果のページにphpタグ(と指示)が表示されています。 – Aioros

答えて

1

あなたはページの内容を読み、それをエコーし​​ます!それをしないでください。代わりに、include( 'file.html')を使用してください。説明のために、(あなたがする必要があれば)このようなsthを行う:

$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を使って作業する方法を完全に理解するまでテンプレートエンジンを使わないので、このプロジェクトは私は新しいバージョンのディスパッチャを含むように質問を更新し、それでもまだ悪いプログラミングをたくさん使っています。私はそれを認識していますが、もう一度、その目的のために学習しています。 – MrSanchez

関連する問題