I want to use css, javascript, php-includes from main domain into sub-domain.
Directory structure for main domain.
/root/global/css/css.css
/root/global/javascript/javascript.js
/root/global/include/include.php
Directory structure for sub-domain
/root/sub-domain/index.php
Now css, javascript, php-includes are not working in sub-domain, i.e I get un-stylized page and no javascript, php files are included.
I am doing this way in sub-domain index.php
<link href="../global/css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../global/javascript/jquery.js"></script>
<script type="text/javascript" src="../global/javascript/javascript.js"></script>
this is not working for sub-domain but working for other same level directories, I also tried this way
<link href="http://www.mysite.com/global/css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://www.mysite.com/global/javascript/jquery.js"></script>
<script type="text/javascript" src="http://www.mysite.com/global/javascript/javascript.js"></script>
It is also not working for sub-domain but working for other directories in same level.
Please see and suggest any possible way to do this.
Thanks.
For sub-domains, this is working without issues, otherwise, you would have another issue within your files location :
src="//www.yourdomain.com/whateverpath/yourfile.js"
Altough www.mysite.com/global/css/css.css looks like an absolute link, in fact it isn't.
If you are on www.mydomain.com it will look for http://www.mydomain.com/www.mysite.com/global/css/css.css which is what you do not want.
To absolute link resources include the scheme (which would be http://www.mysite.com/global/...) or set absolute paths with the trailing slash: /global/css/css.css.
Two options, you can set the "include path" in the php.ini file, or you can use set_include_path() function at the beginning of your script: http://php.net/manual/en/function.set-include-path.php
Related
rootFolder
index.php
cssFolder
fontAwesomeFolder
-main.css
-tablet.css
imagesFolder
-image.jpg
includesFolder
-navMain.php
-footer.php
pagesFolder
-contactUs.php
jsFolder
-core.js
I have topology here about my website. Thing is, when I tried to link/include navMain.php in the includesFolder to the contactUs.php in the pageFolder, some other links are messed-up, particularly - css and images files. They don't seems to work.
The issue is the PHP INCLUDE. Alright. It works fine with the index.php. But not with the file in the subdirectories.
How am I going to bring this around. Some said, I'd use config.php. I tried, didn't work. If ever I'm going to use Config.php, what exact codes do I have to place in there and what codes to the other documents.
Thanks for the help!
The problem seems to be that your assets urls are relative to your file location. To avoid this you can always use / to make them relative to your root url and avoid getting 404 errors:
<link rel="stylesheet" type="text/css" href="/cssFolder/main.css" />
<link rel="stylesheet" type="text/css" href="/cssFolder/tablet.css" />
I'm building a PHP based site with this directory structure
index.php
css
style.css
bootstrap.css
includes
header.php
footer.php
bikes
road.php
mountain.php
The Problem
So I'm working on road.php and I obviously need to be able to link to both style.css and bootstrap.css, but when I declare at the start of road.php to include the header.php and footer.php it is like as if it cannot find the stylesheets and the site reverts back to the default 1990s look.
I have also found that any form of link on the page loads a 404. I'm only just starting out with PHP because I need some more power in my sites, but I just can't seem to get my head around the super basic things.
I just don't know what to do and I'm finding myself turning my back on the whole PHP language.
Thanking you in advance,
Stu :)
I can't be certain without seeing the actual content of header.php (in perticular the part where you import the stylesheets), but it sounds like you are using a relative path to your stylesheets. Something like <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />. This works fine for index.php, but since the other pages are inside the subfolder bikes, they will be looking for the CSS files in yoursite.com/bikes/css.
The solution is to provide an absolute path. Something like this:
<link rel="stylesheet" type="text/css" href="http://yoursite.com/css/style.css" media="screen" />
This way, it doesn't matter if the page is inside a subfolder (or a subfolder of a subfolder) - it will allways look for the CSS file in the right location.
If you are using multiple domain names, or for some other reason you cannot hardcode the domain name, you can prepend a slash (/) to the path as well:
<link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
This path is relative to the root of the website, not to the current directory.
I have a site with index.php in the root folder, images in /img , and overview.php in /content. I have a sidebar.php file that is included in both index.php and overview.php. How should I refer to /img/image.gif if I include a link in each file?
The location of image.gif changes relative to the location of the file that references it.
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php.
The only solution that I can see is to either include a separate sidebar.php in each sub-directory, or include an /img directory in every sub-directory.
The best suggestion that I can find is to use the <base> HTML tag as suggested here:
Change relative link paths for included content in PHP
However, in the same link, SamGoody suggests that the <base> tag is no longer properly supported in Internet Explorer, since version 7.
I'd like some insight on the matter before committing to a course of action.
Thanks.
EDIT: I am using the wrong approach below with "../"
Example-
root/index.php:
...
<link rel="stylesheet" type="text/css" href="style.css" />
<title>title</title>
</head>
<body>
<?php include('include/header.php'); ?>
<?php include('include/menu.php'); ?>
...
root/include/header.php:
...
<div id="header">
<span class="fl"><img src="img/dun1.png"/></span><span class="fr"><img src="img/dun2.png"/></span>
...
root/content/overview.php:
...
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
<title>Overview</title>
</head>
<body>
<?php include('../include/header.php'); ?>
<?php include('../include/menu.php'); ?>
...
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php
But it shouldn't. The preceding / makes it an absolute path which will work from any point on the server. If this doesn't work for you, there's a problem somewhere - in that case, post some examples.
Unless you are planning to move the whole site into a sub-directory one day, or move images to a Content Delivery Network (both actions would require re-writing the addresses) you can safely use absolute URLs.
File structure is as follows:
index.php
settings/
|-manage_account.php
templates/viriditio-v2/
|-index.tpl
templates/virditio-v2/css
|-style.css
localhost/~braden/virditio/index.php shows the template like expected showing index.tpl with the style sheet paths correctly showing:
<link rel="stylesheet" href="templates/virditio-v2/css/style.css" type="text/css"/>
However localhost/~braden/virditio/settings/manage_account.php shows the same path, which is the relative path (should be ../ to be complete).
What's an easy way to make it relative to the template? Or absolute to the root? Is there a Smarty function that includes css files and makes them not relative?
Currently I have it set like this:
config:
template_url = "templates/virditio-v2/"
and .tpl:
<link rel="stylesheet" href="{#template_url#}css/reset.css" type="text/css"/>
EDIT
It's not pretty but I was able to accomplish it with
{assign var='config_url' value=#template_url#}
{assign var='template_url' value=http://`$smarty.server.SERVER_NAME`$config_url}
Any better solutions out there?
Why not access the CSS like
/templates/virditio-v2/css/style.css
with an absolute path?
If its about different hosting environments in subdirectorys, consider a config option to set the base directory and append it as a vairable to the path in your smarty template.
If I understood correctly the issue is to have a CSS file linked to a webpage no matter where in the folder tree the page comes from. This might be written as:
<link rel="stylesheet" href="/~braden/virditio/templates/virditio-v2/css/style.css" type="text/css"/>
This would make it a fixed path starting from the root of the server.
<?php
// This is index.php
ob_start();
include 'tem/u.html';
ob_end_flush();
?>
<html>
<!-- This is u.html -->
<link rel="stylesheet" href="style.css" media="screen" />
<body>
<p> abc </p>
</body>
</html>
Now my problem is when i run h.html -> Ok with style.
But when i run index.php -> Ok without style (because now the index.php include style.css, not tem/style.css)
Need a fix
If possible, refer to a domain relative path to the style.css, for example
<link rel="stylesheet" href="/style.css" media="screen" />
If that is not possible, you need to keep track on the page base in some way, which I cannot tell because I do not know enough about your application. But anyway, like
<link rel="stylesheet" href="<?php echo $pageBase; ?>/style.css" media="screen" />
where $pageBase is a variable containing the url to the root of your application.
I'm assuming that the tem directory is supposed to be for some sort of template, and so you don't want it to be directly exposed to the user; rather, you want to be able to include the files so that they're accessible via index.php, possibly with the option of later changing what files are included.
You could create another PHP file called style.php (in the root directory) which would include tem/style.css. You could do this for any other files that your templates used as well — the idea being that each PHP file in the root directory would correspond to a "role" in the template, not a particular template file, so that the template could later be changed without everything needing to be rewritten.
This might get a bit cumbersome if you had a lot of files required by your template, so it might be better to have a single script that could be instructed which file to load (through a $_GET variable). But in that case, you need to be very careful not to allow the user to specify arbitrary files. I'd suggest avoiding this approach until you're more proficient in PHP.
EDIT: On second thought, I'd suggest using a <base> tag in your template HTML file, as suggested in my comment on #gnud's answer.
This has nothing to do with PHP or include. This has to do with your browser, and how URLs are interpreted.
When your browser is pointed at http://xyz.abc/tem/h.html and asked to load "style.css", it tries to load http://xyz.abc/tem/style.css - this is known as a relative url, relative to the current document location.
When your browser is at http://xyz.abc/index.php and is asked to load the stylesheet in the same way, it tries http://xyz.abc/style.css. Maybe you see the problem?
As for a solution, you might use a domain-relative path for the stylesheet ("/tem/style.css").
just always use absolute path to your css file
<link rel="stylesheet" href="/tem/style.css" media="screen" />
that's all