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 page.
my php page:
<?php
$root = $_SERVER['SERVER_NAME'] . '/mysite';
$theme = $root . '/includes/php/common.php';
echo $theme;
include($theme);
?>
common.php:
<link rel='stylesheet' type='text/css' href='../css/main.css'/>";
Anyone know whats wrong? Thanks
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With