简体   繁体   中英

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.

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
      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. How can I accomplish this? Thanks.

You can't file_get_contents a PHP file on the local file system like that. 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.

Why aren't you just doing a regular 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. Take a look at require .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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