I have a site located at http://www.mySite.com. My html/PHP pages are located at http://www.mySite.com/index.php I have created a 'tabs' include file located at http://www.mySite.com/includes/tabs.html. And all is grand!
But I have started to need a bit more functionality and have added pre-coded pages/packages, like a blog. These packages are located in their own directories like http://www.mySite.com/blog.
The problem is the from the new package directories the links in the tabs include are no longer pointing to the correct URL because of the directory change. I understand why this is happening but cannot figure out a work around other than maintaining 2 separate tabs files, which seems wrong.
I had a very similar question that dealt with the css links and solved by adding a slash before the stylesheet path in the stylesheet link. Unfortunately it does not work in this situation also because in the different directory the include is '../includes/tabs.html'.
The tabs link is a standard html link:
<li>Home</li>
Again in my root directory files it works fine but if I put a file in a new directory like "http://www.mysite.com/newDir/index.php" the tabs are pointing to "http://www.mysite.com/newDir/tabURL.php" not "http://www.mysite.com/index.php".
Thank you again for your time,
Todd
All you need to do is update your links in the tab.html file to use absolute paths instead of relative paths.
<li>Home</li>
try including the tabs file using the full path on server no in your site directory e.g. www/mysite/includes/tabs.html
Related
I have my website on my local server (localhost - XAMPP). My website is broken up into three parts: 1)Header 2)Body and 3)Footer/Jscripts.
The header.php calls other basic files for the header section of the webpage. It works fine when I used relative paths, for example: include_once('./_css/main.css'); to call my CSS files or include_once('./_inc/metadata.php); to call my metadata for my pages.
The problem arises when I use an absolute path:
include_once('http://localhost/mywebsite.com/_css/main.css'); the same for the other files.
Why is that? This all started because I want to create subfolders because I had a general page, but now that I'm breaking it down to subpages I need a folder to contain the subpages
I was trying to include the css files and all the other resource files to the subpages, but they break using the relative paths method (./_css/main.css) being pulled from the header file located in the root directory. On the subpages, I'm using include_once('../_inc/header.php') to include the initial header.php file.
I want to avoid having to duplicate all the resource files from the root directory and adding them in the subfolder with their respective relative paths. That's why I was trying to use the absolute path method.
Any insight or clue would be appreciate as to:
Fix the 'Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0' error when using the absolute path.
2)Sharing css files in subfolders.
Using an absolute path may be a bad idea, what if you move from localhost to a real server? You'd need to make massive changes. Changing the project structure so that you could avoid all of this might be better, or if you really want to keep things as they are, use a variable.
<?php
$include_path = "whatever/path"
include('../header.php');
?>
and in header.php
<?php
include($include_path.'/my_css.css');
?>
INTRO
I am new to php. I love that it allows me to change one header.php file and it updates all over the site.
IF all - index.php, header.php, style.css, article.php, homework.php files are in the ROOT folder, everything works like magic, I like it. I use:
<?php include_once "header.php"; ?>
at the top of index.php, article.php and homework.php and the header appears.
to load css a regular =
<link href="style.css" rel="stylesheet" type="text/css">
is enough to have in my header.php file, because all the files are in the same directory.
MY PROBLEM
When the amount of articles becomes too large and I decide I want to put those articles in different folders, that is when stuff gets confusing and I would like to know a proper way to solve it.
New website folder structure
C:\xampp\htdocs\articles\homework.php
C:\xampp\htdocs\views\header.php and style.css
C:\xampp\htdocs\index.php
C:\xampp\htdocs\articles.php
Now please help me how to make homework.php file to load the css from the header.php? I manage to load the header itself with
<?php include_once "..\views\header.php";?>
BUT the css file doesn't load for some reason.
I read something about "basenames", "site roots", but don't know how to properly set them up.
The perfect scenario
The perfect scenario would be if I could have a basename variable that I can change, so when I make my server live I can just change the basename to the appropriate new server directory and because all the header.php and other blog files were linked to that basename, everything would change automatically. I have done too many manual directory rewriting to do it once again, please tell me a way to automate it :)
Thank you a lot!
p.s!!!!!! Before I even post this question I realized that the header.php is trying to load views/style.css, which doesn't make sense, because the style.css file is in the same folder as header.php now.. Somehow basenames, site roots are a must here I believe...
You can specify relative paths such as ../css - means up one folder then look in css folder or ../../ - means up 2 levels then look in css folder.
../../main/css/style.css - would mean up 2 levels then look in main/css for the file style.css.
The problem with using / or ../ etc is that if you decide to change the location of the resource you still have to change all your paths. Using a var you can simply change it to reflect the new location.
with PHP you can also getcwd() and dirname() and realpath() to get a string representing a location, you can then set a base variable for your files and 'path' down from it.
This way you can use the same variable to locate a file rather than different relative paths depending on the level of the file calling it.
DIRECTORY_SEPARATOR is also useful for avoiding errors between / and \ with linux and windows OS. However I believe Windows and Linux will both resolve /
Personally I like to set path locations to commonly used files such as /includes in config.php then I can use that setting from anywhere
In summary you are just either discovering a path using PHP or setting a path as a variable
$path = 'c:/htdocs/mysite/includes/';
then using the variable as part of the path name when you access the file
$_SERVER['DOCUMENT_ROOT']
can also be useful to identify your site home folder
I just uploaded my site but am having issues with sub domain and php include.
First I have a directory /main/ and all it content are okay in localhost and also when I uploaded online until when I decided to forward www.main.example to main directory.
Now if I browse it this way www.main.example files that I include will not work, and also my css, but I manage to add full site name to my css and js like www.example.com/css/page.css and is working. So can someone help me with php path or any htaccess code that can help me fix this?
I tried this to but not working
include_once('../_inc/functions.php');
include_once(__DIR__ . '/../_inc/userlocation.php');
I have decided to swap my standard html navigation for a php include, i have a multi deirectory site so have had to include the file using ../ to move up the directories where applicable.
When using the navigation from my root folder it works fine but when i then try and use the same navigation in a subdirectory file my file paths get added onto the end of the current path for example.
http://example.co.uk/catagoryOne/catagoryTwo/index.php
Becomes:
http://example.co.uk/catagoryOne/catagoryTwo/catagoryOne/catagoryThree/index.php
Where i need it to be:
http://example.co.uk/catagoryOne/catagoryThree/index.php
Could anyone tell me why this is happening?
Many thanks in advance
P.S. Apologies if this is not clear please let me know if anything requires further clarification.
Wouldn't this be solved via .htaccess as it is to do with url?
Read more on .htaccess here
To fix this problem i used
<li><a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/catagoryOne/catagoryThree/index.php"></li>
within navigation.php where applicable.
From my previous experience, I've almost always had problems with linking files with my website projects.
For example, linking CSS styles, Javascript files and including files in PHP. The problem is, that on my PC, the directory of my project was /www/project-name/ and when I put the project on a server, the directory would be just /www/. When I uploaded the project to a server, images wouldn't show, styles wouldn't work, database connections wasn't set, functions were not defined etc...
So my question is: What is the best and most efficient way to link/include files?
Something that will work no matter what the directory of the project is, and possibly, if I include project/includes/mysql.class.php in file1.php, and I move that file to a different directory, it would still properly include project/includes/mysql.class.php
You should use relative paths.
Instead of specifying the full path ('/www/project-name/includes/whatever.php'), use a path relative to the current location:
'./includes/whatever.php'
you can define the document root directory of project and then, include all files depending on it
put
define(DOC_ROOT, realpath(direname(__FILE__));
in your front controller, and when you have to include a file
include(DOC_ROOT . "/includes/file.php");
all frameworks uses this method
I'd suggest using a relative path (eg ../style.css or ../../style.css)
The ../ references the parent directory to the current file.
This is what I do, in general.
I use root relative urls inside html (e.g. src="/images/logo.jpg"). This way I can just copy the html from one page and past it in another without having to worry about the link not working becase the other page is inside a folder.
I relative urls in css, because all the resources I use inside the css, like images, I keep in the same folder as the css file (or a sub-directory of it). I mostly do this because it is shorter (url(img/background.jpg); vs. url(/css/img/background.jpg);). Minor added bonus is you could just copy the css folder to create a new theme based on the old one, without having to change all the urls in the css.
In PHP I use include($_SERVER['DOCUMENT_ROOT'] . '/includes/mysql.php');. You can just copy past the code into another file in another folder and it will still work.
The only time I rarely need to hardcode paths is inside htaccess.