Let me explain my need with simple example.
I have folder called "Website" Inside that some sub folders and files.
Website(Main-Folder)
pages(sub-folder)
(files)
page1.php
page2.php
target_page(sub-folder)
(file)
target.php
menu(sub-folder)
(file)
menu.php
index.php (this file is in main folder)
This is my sample folder view, I just included menu.php(navigation menu with links)file in my both pages and index.php file.
In menu.php i have a nav link to target.php page
So, to
access target.php file in
index page mean i have to use Target
Target
access target.php file inside
the pages folder mean i have to use
Target
Is there any short way to access that target page, because I don't like to create multiple menu.php file and set the different location for same file.
Sorry for bad English, Thanks.
[UPDATED]
The best alternative is to define your root path once and for all !
We use another file that we could call whenever we need the PATH. Like for example in settings.php that we save near index.php:
define('My_ROOT' , 'http://'.$_SERVER['HTTP_HOST'].'/');
Then whenever needed, we require that file and so we could write links like follwing :
Target
P.S: Please notice that in my first Answer I used the file path and that could be used for relative inclusion not Frontend links !
Just put your href starting with "/" :
link
Related
I just started creating a website at my home.
Absolutely, I must have these two pages to finish my website rapidly:- footer.php, header.php.
So I created those pages & put some contents. Also created an index page as index.php inside the htdocs folder.
Then I did include the header & footer pages inside the index.php page by using these following codes.
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
Undoubtedly, they worked fine without any trouble.
Then I created a directory as account inside the htdocs.
Now I've a login.php page inside the account directory (/account/login.php).
Repeatedly I used those same codes to include the header & footer in the login page. But they didn't work! I saw nothing is happening. If I create the login.php page inside of the htdocs folder (not in htdocs/account/), so it works.
So, how can I include them while the login page is in account directory?
When creating sub directories and including files it is always simpler to use absolute file paths.
The path with reference to root directory is called absolute (https://www.website.com/modules/header.php), you can even remove the domain and just have /modules/header.php. The path with reference to current directory is called relative (../images/phone.png). The ../ indicates that the URL points to the directory above the current folder.
Please see answers relating to a similar question here: difference-between-relative-path-and-absolute-path-in-javascript
I think it's a file path problem,you can use this code:
<?php include '../header.php' ?>
<?php include '../footer.php' ?>
Load the file of the first level directory。
I just started creating a website at my home.
Absolutely, I must have these two pages to finish my website rapidly:- footer.php, header.php.
So I created those pages & put some contents. Also created an index page as index.php inside the htdocs folder.
Then I did include the header & footer pages inside the index.php page by using these following codes.
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
Undoubtedly, they worked fine without any trouble.
Then I created a directory as account inside the htdocs.
Now I've a login.php page inside the account directory (/account/login.php).
Repeatedly I used those same codes to include the header & footer in the login page. But they didn't work! I saw nothing is happening. If I create the login.php page inside of the htdocs folder (not in htdocs/account/), so it works.
So, how can I include them while the login page is in account directory?
When creating sub directories and including files it is always simpler to use absolute file paths.
The path with reference to root directory is called absolute (https://www.website.com/modules/header.php), you can even remove the domain and just have /modules/header.php. The path with reference to current directory is called relative (../images/phone.png). The ../ indicates that the URL points to the directory above the current folder.
Please see answers relating to a similar question here: difference-between-relative-path-and-absolute-path-in-javascript
I think it's a file path problem,you can use this code:
<?php include '../header.php' ?>
<?php include '../footer.php' ?>
Load the file of the first level directory。
I have three pages on my localhost 1 is my index page,2 is my universal header page which is under includes folder of and 3 is my html file which is under html folder.The header file is included in both the index file and html file like that...
for index.php-include("includes/header.php");
for html.php-include("../includes/header.php");
and my header has the link of index.php page that is (./index.php)
Now my questions is that when i open my index page and click on link of index page from my header it takes me to same index.php page but when in open html.php page and then click index.php page link from header it does not go to index.php page but it goes to this page-
(localhost/educational%20website/html/index.php) how to solve that.
And i also want to know that write now i am on localhost but when i make my site live is there any need to change the paths because i am making around 150 pages with your technique plaese so please answer me that kind of technique that is used for both localhost and on live
Your are including paths relatively, use a (absolute) base path in your index.php to fix this:
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
One way is to define a variable or a constant for the site's url in the header.php file. Then in all your other pages, you could just use this variable/constant when you need to mention the other urls.
Eg(put this as first line in your header.php file):
define('SITE_URL', 'http://localhost/educationalwebsite');
Here, we have defined a constant named SITE_URL. Then in other pages, you are already including this header file. Isn't it? So, this constant will be available in your index.php, html.php and other pages.
And suppose for a link in your html.php file(to point to the index.php), you could use it like this:
Home
If you want to include link to the html.php file residing inside html folder, it would be like:
HTML
By using this way, if you are uploading the whole site to a live server, you only need to change one line, ie. the first line in header.php, where we have defined the SITE_URL constant. Just change it's value to the new URL of the home directory of your website.
Hope this helps
please,I am trying to create link to a php file in sub folder using tag but when I am testing the link it gives "NOT FOUND" error this is the code About
Make sure that name of file or folder is correct with caps lock text. something like this in below:
About
If about.php is in pages folder and you are gonna link to another file in this folder, you must remove ../ from the code, something like this:
About
Please provide link of About.php that we'll check it out, it's easy way to find the problem.
Browse to the file in the browser manually, then work backwards.
Draw a map of your directories, for visual aid
For example;
.public_html/
..pages/
page1.html
..files/
...config/
..folder1
..folder2/
index.html
The link from index.html to pages/page1.html would be
Page1
The link from pages/page1.html to index.html would be
Index
I have a php page(index.php) that includes another html file(footer.html). Am using dreamweaver, and my problem is: The included file(footer.html) shows in the main file (index.php) in dreamweaver design view, but does not show in the browser preview. I have both the php files in the root directory, so they're at the same level. I have tried using require rather than include with the same result. My include code is:
<?php include("footer.html");?>
does anyone see something wrong off the bat?
Do realy exist file "footer.html"?
If no create them.
Is file "footer.html" in the same directory as "index.php"?
If no add it to the same directory.
Do you have any content in your "footer.html" file?
If no add any and check if it appears on page.