Here is what I have. I have an index php page sitting on my /root/ folder with the following code:
index.php
require_once("access/$template/head.php");
My functions page has the following configuration for the $template code:
funcs.php
function getTemplateFiles() {
$directory = "models/site-templates/";
$languages = glob($directory . "*");
return $languages;
}
My head php page has the following code:
head.php
<link rel='stylesheet' href='$template/css/style.css'>
My question is what code can I add to my index page that will communicate with my head page, a code before my $template that will include the file location of my php page.
Example 1
$include_dir/$template/css/style.css'>
My question being is how can I include the path of my index page so that my head page will read as html like so?
default, does not work $template/css/style.css
need it like this only for the index page access/$template/css/style.css
Replace your code
<link rel='stylesheet' href='$template/css/style.css'>
Use this
<link rel='stylesheet' href='<?php echo $template; ?>/css/style.css'>
I'm not quite sure whether I understand your problem correctly, but these two methods might help:
$currentDir = dirname(__FILE__);
$currentDir will be the path on your server to the file you're currently in. So if you have a structure like this:
/access/functions/myFunction.php
and you're calling this method from myFunction.php, $currentDir will return '/access/functions'. If the file to be included is in the same directory-path, you can include it like this:
$includePath = $currentDir."/myFile.php";
Another way of determining a target directory is using:
$docRoot = realpath($_SERVER["DOCUMENT_ROOT"]);
$docRoot will be the top-directory of your website (usually something like /usr/www/username/public_html/)
Using this, you always know what your top-directory is and then you can include files like this:
$includePath = $docRoot."/models/site-templates/myTemplate.css";
Related
I am just wondering what will be the best way to import multiple css files into php header. Currently four css files are linked to different pages which are being converted into 4 different php header and then being called like require_once DIR . '/../templates/header1.inc.php'; is there anyway i can merge them in to one file with function as i am only at beginner level i am definetly getting something wrong here is what i have tried.
function aboutAction()
{
$pageTitle = 'About Us';
}
if ($pageTitle== About Us)
{
require_once __DIR__ . '/../public/css/style4.css';
}
Thanks
I ususualy do something like this:
<?php if(condition) { ?>
<link rel="stylesheet" href="..
/* or any other html code */
<?php } ?>
The CSS will be included only if the condtition is true.
I have 2 types of header on my website, so instead of creating 2 include files for each type of header, I want to create a single include file with an if/else statement that will display the right type of header to each page.
Here is the code for the "testpage.php":
<?php
$testpage="testpage.php";
$currentpage = $_SERVER['SCRIPT_FILENAME'];
?>
<!DOCTYPE html>
<html>
<head>
<title>testpage</title>
</head>
<body>
<?php include_once('include/header.php'); ?>
</body>
</html>
And here is the code for the include file "header.php":
<?php
if($currentpage==$testpage) {?>
<p>Woaw!!! You are a genius. you should work for NASA</p>
<?php }else{ ?>
<p>this doesn't seem to work. Try something else.</p>
<?php }
?>
So far, I've tried this:
$currentpage = $_SERVER['SCRIPT_FILENAME'];
$currentpage = $_SERVER['PHP_SELF'];
$currentpage = $_SERVER['REQUEST_URI'];
$currentpage = __FILE__;
I've tried on my XAMPP local server, and as well on my "bluehost" live server, and I just cant make it work.
Also, I created a small test on github, to test that, so if you want, you can take a look, here is the link: https://github.com/nobody7/test001
So, please let me know if you have any suggestions, any help will be highly appreciated.
Thanks
From http://www.php.net/manual/en/reserved.variables.server.php
$_SERVER['REQUEST_URI'] is the web address that the user inputted into the address bar minus the hostname and protocol (e.g. a request to http://example.com/my/web/site/123, will return /my/web/site/123), and is usually what people use to differentiate between different pages when using a Front Controller since front controllers mean all requests go through a single php script first.
$_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_FILENAME'] will show the filename of the initial script that you executed, they are pretty much the same thing. SO if you did a request to http://example.com/abc.php those 2 variables will return the filepath to abc.php (e.g. /var/www/abc.php) even if you executed this in an included file, it will show the filepath foe abc.php.
__FILE__ will show the current file you are in, this is similar to the above, but it will show the file path of the file you are currently in even for included files. So if you have a header.php that you have included inside abc.php, and you use __FILE__ inside of header.php, it will return the filepath to header.php. This is probably not what you need.
Your attempts are almost there, but because $_SERVER['SCRIPT_FILENAME'] returns the full path of your file, you need to somehow only get the filename portion of it, luckily there is a built in function for that already basename(). So you only need to change a little bit of your code:
$currentpage = basename($_SERVER['SCRIPT_FILENAME']);
I have a variable on my site called $basePath which is set as:
$basePath = '/Systems/dgw/';
I am using it on all my css, js and images tags as so (shortened for better visibility):
<link href="<?php echo $basePath; ?>include/assets/css/bootstrap.min.css">
I have no problem with those includes and they work fine in wherever file and in whatever folder I am.
I have a certain included page which has the following line:
<img src="<?php echo $basePath; ?>images/new_logo.png" alt="logo"/>
And the image shows just fine. The line after it states:
<?php include($basePath.'include/assets/common/topMessages.php');?>
But the include doesn't happens. When I try it like this:
<?php include('../../include/assets/common/topMessages.php');?>
It works.
Anybody has any idea what could be wrong?
You can't include php files relatively to your webroot that way, cause if you use the slash as first character, the reference will go much deeper than just your document root. So, instead of using your basepath, you could do something like this :
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/yourfile.php";
include_once($path);
?>
If your server doesn't populate the "document_root", you may need this
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/path/to/file.php");
I use this line of code. It goes back to the "top" of the site tree, then goes to the file desired.
For example, let's say i have this file tree:
domain.com/aaa/index.php
domain.com/bbb/ccc/ddd/index.php
domain.com/_resources/functions.php
I can include the functions.php file from wherever i am, just by copy pasting
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
If you need to use this code many times, you may create a function that returns the "str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))" part. Then just insert this function in the first file you include. I have an "initialize.php" file that i include at the very top of each php page and which contains this function. The next time i have to include files, i in fact just use the function (named "path_back"):
require(path_back()."/_resources/another_php_file.php");
You can add an include_path = ".:/home/myuser/mysite.com/" to your php.ini or you can add something like this into your script before the include or require:
set_include_path(get_include_path() . ":/home/myuser/mysite.com/");
The first one will work for all the scripts running in your website.
The second option will only work for the script which has the setincludepath on the code, for the rest of the application it will not work unless you have an object you call in every script that add the setincludepath.
I am currently using an index.php to include another file , it structure like:
root / index.php
/ new/ index.php
/ img/ test.jpg
And I write following in root/index.php :
<?php
require_once("new/index.php");
?>
the problem is , all paths in it are wrong. As all paths are based on new/index.php.
For example, In new/index.php my test.jpg is like
<img src="img/test.jpg" />
It shows http://www.test.com/new/img/test.jpg when I directly access new/index.php
But it shows http://www.test.com/img/test.jpg when I include the php in index.php.
How to fix it? I tried chdir() but it does not work expect Thanks
Make sure you always include with an absolute path, like:
require_once(dirname(__FILE__) . "/otherfile.php");
require_once(dirname(__FILE__) . "/../uponefolder.php");
require_once(dirname(__FILE__) . "/sub/folder/file.php");
Or use autoloading.
Did you just ask the same question twice? Use the required / included file as base directory in PHP
See my answer there.
The folder path you have in the require_once() function is relative to the directory your page is currently in. Have a look at the set_include_path function. Specifically, you could add that function to the top of your scripts to set the root include folder. Something like this:
set_include_path("/var/www/html/root");
require_once("new/index.php");
You could simply change the included HTML document base by adding a base tag.
https://developer.mozilla.org/fr/docs/Web/HTML/Element/base
<head>
<base href="your_new_url_base" target="_blank">
</head>
The HTML included file could check "Am I included from a parent?" if so "I have to change my base tag"...
// new/index.php
echo '<head>';
if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) {
// called directly, no need base tag;
} else {
// included/required
echo '<base href="your_new_url_base" target="_blank">';
}
echo '</head>';
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.