include navigation.php creating file path error - 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.

Related

Linking CSS file with PHP throgh all folders

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

xampp include doesn't work

Good morning at all! I have an issue that I don't understand.I've XAMPP
localhost files where include function doesn't work in one file, while
in other files works without problems.I'm running Chrome browser. I've
already tried to clear browser cache, but issue still exists. Why?
Include function works in this pages:
"index.php",
"blog.php",
"history.php"
Include function doesn't work only on this page:
contacts.php
The code is here:
<?php include('include/menu.php'); ?>
You need your absolute path to the include file correct by prefixing with $_SERVER['DOCUMENT_ROOT'];
In all your includes() do :
include($_SERVER['DOCUMENT_ROOT'].'/include/menu.php');
it would help to know two things: 1) where are the files located in? 2) what is the error message?
maybe the solution is simple since there is only the lack of the correct path:
include(__DIR__."/blog.php");
etc.
__DIR__ contains the path of the current script and is very useful to go up/down the directories based on it.
I've solved issue. I copied another existent page such as index.php with menu and footer correctly included, and I changed the body content and now it works.

Issues with php include in sub domain

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');

PHP require folder above

I have a file structure that looks like this (unimportant files left out):
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
Now when I try to call require_once('../PHP/functions.php'); inside the "index.php" it doesn't work and I don't understand why. Anyone an idea what the problem could be?
This works fine:
require_once('../file.php');
first thanks for all the answers, I found the bug now:
inside the functions.php I require another file and the path to that file wasn't correct when I called it from index.php
So I had to change the path inside functions.php not inside index.php.
Still thanks to everyone for the other suggestions :)
Do you get an PHP error, and if what does it say?
Check for lowercase/uppercase problems
The following should work correctly:
require_once('../PHP/functions.php');
Check your privileges on the folder and try:
require_once('../PHP/functions.php')
You have a Folder Structure Like so:
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
And then, you are inside of index.php trying to Access the File functions.php, which lives inside of the Directory PHP. Then you need to go up one Directory from the current directory of the index.php File (ie. The Administration Directory) [meaning that you get up to Testserver Folder from theAdministration Folder] and then drill down to the PHP Directory. In code this would mean:
<?php
require_once __DIR__ . "/../PHP/functions.php";
Hope this does it for you.
Cheers & Good Luck....

Resolve links in include file

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

Categories