how to include another php file?

前端 未结 2 945
盖世英雄少女心
盖世英雄少女心 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.

提交回复
热议问题