简体   繁体   English

使用file_get_contents发出解析PHP

[英]Issue Parsing Php With file_get_contents

I'm loading my nav-bar from one php file, so I can just edit one file to update the whole site. 我正在从一个php文件加载导航栏,因此我只需编辑一个文件即可更新整个网站。

Code: 码:

function getRsc($url){
    return file_get_contents($url.'.php');
}

That was working file until I tried to add actual php to the file. 那是工作文件,直到我尝试将实际的php添加到文件中为止。

  <?php
      if(isLoggedIn()){
        echo("<a class='btn btn-success' href='/account.php'> My Account </a>");
      }else {
        echo("<a class='btn btn-success' href='/login.php'> Login/Signup </a>");
      } 
  ?>

It just displays the PHP as text, not parsing it. 它只是将PHP显示为文本,而不进行解析。 How can I accomplish this? 我该怎么做? Thanks. 谢谢。

You can't file_get_contents a PHP file on the local file system like that. 您不能像这样在本地文件系统上file_get_contents一个PHP文件。 It won't get executed, you'll just get back the plain-text, unprocessed code. 它不会执行,您将只取回未经处理的纯文本代码。

You need include or require instead for a local PHP file. 您需要includerequire使用本地PHP文件。

Why aren't you just doing a regular require ? 您为什么不只做定期的require eg, 例如,

<?php

require "path/to/nav-bar.php";

You're not supposed to use file_get_contents to load and run a PHP file. 您不应该使用file_get_contents来加载和运行PHP文件。 Take a look at require . 看看require

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM