how to include another php file?

前端 未结 2 944
盖世英雄少女心
盖世英雄少女心 2021-01-03 01:18

I have a php file, and I want to include another php file that have css link tags and javascript source tags, but when I try to include them, it doesn\'t get added to the pa

相关标签:
2条回答
  • 2021-01-03 02:10

    PHP's include is server-side, so you need to use the server side path. It is better to use dirname(__FILE__) instead of $_SERVER['SSCRIPT_NAME'], but $_SERVER['SERVER_NAME'] is absolutely wrong.

    Try:

    include dirname(__FILE__)."/common.php";
    

    Or if the file you want to include is not on the same directory, change the path. For example for a parent directory, use dirname(__FILE__)."/../common.php".


    Note that some might suggest using include "./common.php" or similar. This could work, but will most likely fail when the script invoking include is actually being included by another script in another directory. Using dirname(__FILE__)."/common.php" will eliminate this problem.

    0 讨论(0)
  • 2021-01-03 02:10

    Change your code to this:

    <?php  
        $theme = 'includes/php/common.php';
        echo $theme;
        include($theme);
    ?>
    

    If your includes folder is in the same folder as your php page then it should work, if not add you domain name instead. SERVER_NAME is not needed in this instance.

    0 讨论(0)
提交回复
热议问题