Links not resolved correctly when using relative paths - php

I've created a Header.php file in the root directory.
Below is the tree structure.
-SiteName
-Header.php
-Books
-Samples
-Math
-MathBook.php
-index.php
-About.php
So MathBook.php includes Header.php like below
include '../../../Header.php';
Header.php is included in About.php like this
include 'Header.php';
Now Header.php contains the code for the navigation menu.
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
So now when I'm in Math.php and I press home it takes me to:
/Books/Samples/Math/index.php
Obviously the URL doesn't exist.
How i can make sure when the user is in Math.php it should go to index.php without including current path.
Header.php is included in all files.

You could define includes and links relative to your site's base url.
The hrefs can be written like so:
<ul>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/index.php';?>"
class="<?php active('index.php');?>">Home</a>
</li>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/About.php';?>"
class="<?php active('AboutUs.php');?>">About Us</a>
</li>
</ul>

You are using a relative path.
by typing "index.php" in the href attribute, you are pointing to a file named "index.php" in the current folder.
If you want to point to a file in the root folder, you need to use a slash /
href="/<sub folder if any>/index.php"
href="/<sub folder if any>/About.php"
This will take the user to the file you would like, also if you are unsure about what to use, just use absolute paths like this:
href="https://www.example.com/<sub folder if any>/index.php"
href="https://www.example.com/<sub folder if any>/About.php"
EDIT
Adding a slash works because by adding / you tell the browser to go to the root folder/website and start the path from there.
Let's say you are in yourwebsite.com/current/path/file.html.
A link to /some/path/to/a-file.php resolves to yourwebsite.com/some/path/to/a-file.php
But a link to some/path/to/a-file.php resolves to yourwebsite.com/current/path/some/path/to/a-file.php

Related

Get path of current file in include file php

This is my header.php stored in include folder
<ul class="dropdown-menu">
<li>My Account</li>
<li class="somefile.php">Admin Panel</li>
<li>Users</li>
</ul>
NOTE: i have to include header.php in both folder
Main Root Folder
RootFolder/MasterFolder
Now when i am calling file from Root directory it works fines
but when i am include this file in Other folder and call index(My account) so it call like this
mysite/master/index.php
but i wants when i am calling include file from master named folder
mysite/index.php
My File Structure is like this
in master also have files. and my header.php is in includes Folder
URLs and paths are different.
When you're including your html header in php you should require it using the document root of the server. This way, no matter which system you're on, you can still access it.
If telecaller is your web root directory, then you should have the code:
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
Paths can then be relative URLs in the HTML inside of that header.
<a href='/index.php'>Telecaller Index File</a>
<a href='/master/index.php'>Master Index File</a>
<script src='/ajax/something.js'></script>
<a href='/login.php'>login page</a>
define('ROOTPATH', __DIR__);
//define root path
// and make link of file from the root path
<ul class="dropdown-menu">
<li>My Account</li>
<li class="<?php echo ROOTPATH.'somefile.php'; ?>">Admin Panel</li>
<li>Users</li>
</ul>
<ul class="dropdown-menu">
<li>My Account</li>
<li class="../somefile.php">Admin Panel</li>
<li>Users</li>
</ul>
You can give a relative path like this, or give absolute path
https://www.w3schools.com/html/html_filepaths.asp

Website Navigation within the Header

I am having trouble properly setting up navigation on my website. I am putting the navigation into my header and I want to get all of the links to work no matter what layer in the file structure that the webpage is on.
This is my file structure:
Website (directory)
index.php
resources (directory)
includes (directory)
html_codes.php
Game (directory)
game_info.php
resources
Characters (directory)
characters_info.php
Players (Directory)
players_info.php
Inside the html_codes.php is a function that builds the header for the website. I use this function to create the header at the top of all of my web pages. However the hyperlinks that navigate around the website do not work in any layer except the top-level directory because their relative position has changed.
Is there a way to get the links to work in
./index.php
./game/game_info.php
./game/Characters/characters_info.php
./game/Characters/Players/players_info.php
from the same create_header() function?
My create_header() function:
function create_Header(){
echo '
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>
';
}
*********************************EDIT***************************************
Okay, I used your suggestions and it solved some problems and created others.
First off, turning the paths from relative to absolute worked but it required that I change the path names to
/Website/index.php
/Website/game/game_info.php
/Website/game/Characters/characters_info.php
/Website/game/Characters/Players/players_info.php
which is fine but I don't understand why. I assume it is because the Website directory is a sub-directory for C:\xampp\htdocs\Website.
The other issue is that my include functions don't work with this absolute path.
The relative path for the include inside of game_info.php before was
include("./../includes/html_codes.php");
which did and still does work.
I have tried both
include("/includes/html_codes.php");
include("/testphp/includes/html_codes.php");
and they did not work.
Two things
1. i recommend not making capitalized folder names in your source and in this header. otherwise some browsers may require the capitalization by your users. which isn't really standard.
2. i recommend you use absolute paths (removed .)
function create_Header(){
echo '
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>
';
}
Put a forward slash / in front of your paths to make them absolute.
Go home
This will ensure that you're always starting from the same place (root), and that your links are independant from the hierarchy.
If your website is live, you can use an absolute URL like this one :
Go home
./ is relative to the current directory. There are two ways to solve it. The first one: keep using relative paths, but let them move up as well. So if you are on ./game/Characters/characters_info.php, the directory is ./game/Characters/, and the path to home is ../../index.php.
So you'll need to know not only the depth of the current page, you'll need to get up to a level that is shared between the current page and the target page. After all, both could be two levels deep, but still are in a different directory.
I think a better (at least much easier) way is to use an absolute path. To do that, you can just start with a / to have the absolute path from the domain name.
Optionally, you could configure an 'installation path' or 'base directory' to start each path with. It could default to just '/', but you could set it to another value if the site was entirely in a subdirectory.
So the header would look like:
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>
Or, with a base path variable, you can move the site to another directory, just change the variable, and everything will still work. Very convenient for testing and development. The normal value for this variable can just be ''.
<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
<ul>
<li>Home</li>
<li>Game</li>
<li>Characters</li>
<li>Players</li>
<ul>
</nav>

PHP include Image file not displaying

The header for my website is the same across all of it so instead of rewrite the code and link the style sheets, i've decided to use the <?php include ;?> to put it at the top of every document.
My issue is that the logo that should come with the header isn't displaying.
File Structure
As you can see, the header file is where it is and the logo named "Picture2.png" is in the image folder.
PHP
<?php include('./_/includes/header.php'); ?>
HTML (In header.php)
<nav id="navigation">
<ul id="navList">
<li id="navLogo"><img src="/image/Picture2.png"/>Computing</li>
<li><a class="navItem" href="gallery.php">Gallery</a></li>
<li><a class="navItem" href="topics.php">Core Topics</a></li>
<li><a class="navItem" href="courseview.php">Courses</a></li>
<li><a class="navItem" href="index.php">Home </a></li>
</ul>
</nav>
Part of header that isnt' displaying correctly
NOTE** everything else in the header is correctly displayed, I'm using a local server, should that make a difference
You are using an absolute path for your image.
You should put and use a relative path :
<img src="_/includes/image/Picture2.png"/>
instead of
<img src="/image/Picture2.png"/>
Yep, hes using an absolute path for image, but the project isn't in server root folder then you need's to inform the name of the folder in path...
Use <img src="/finalprojectneat/image/Picture2.png"> then you have your logo display on every page. But is not the most indicated because when you send to production server, you didn't have the "finalprojectneat" folder then you have to remove all paths using "projectneat".
One solution is to define a constant in your "index.php", not necessary in "index.php" but required in root folder of project
define ('_IMAGES_', realpath(dirname(__FILE__) . '/image'));
if you put this constant in another file inside root folder, use "require" to import these constants to your views...
and in your views, use
<?php echo _IMAGES_ . '/Picture2.png'; ?>

Link to local html file

Im trying to put a link to local html file called Rules.html,
but for some reason I cannot make it work.
Html file is in the same folder as my php file.
Note that Im running my forum on localhost:1337, Should that be somewhere in that line too?
Here is my piece of code:
<li class="copyright">
<a class="rules" href="file:///C:/xampp/htdocs/SMF/smf_2-0-6_install/Themes
/Fresh_v2_RC5/Rules.html">Rules</a>
</li>
I would suggest using relative links. So the link would be
href="theFile.html"
if it's in the same folder as the file you are trying to write the link in.
Use relative path from your root dir. Root dir is a dir where located your html file within this code. For example, if you put this code in html file, located in htdocs dir, so the relative link is:
<li class="copyright">
<a class="rules" href="SMF/smf_2-0-6_install/Themes/Fresh_v2_RC5/Rules.html">Rules</a>
</li>
If it is in the same folder as the file you are calling it from, you can just call it by name, instead of it's complete path.
<li class="copyright">
<a class="rules" href="Rules.html">Rules</a></li>
Also, if you are calling it by it's full path name, you do not want to use file: to open a linked html document. You would call:
edit: (added // in front of localhost.)
<li class="copyright">
<a class="rules" href="//localhost/../Rules.html">Rules</a></li>
And pull it up in your apache server. Using file is like opening the file in your file system, but to view html, use your web server.
As others have said, it's easiest in this case to simply use relative urls.
The way to do this with the code you've provided:
<li class="copyright">
<a class="rules" href="Rules.html">Rules</a>
</li>
Make sure that this Rules.html file is in the same directory (dir) as the file from which you're calling it. If not, you'll need to do some directory traversing to get to it.
Here's a question with some helpful examples.

PHP: Navigating through subdirectories

Consider the following directory tree:
/root
page1.php
menu.php
/subdir
page2.php
Here, menu.php is a navigation menu:
<div class="cssmenu">
<li>page 1</li>
<li>page 2</li>
</div>
Both page1.php and page2.php invoke menu.php through the include function:
<?php include "path to menu.php"?>
However, when code is summoned through the include function, relative links in the navigation menu become ill-defined. How should I define links in the navigation menu as to be properly rendered in a browser? Preferably, I'd like this to be independent of the absolute path of /root, so that I may render my site through my localmachine.
You may start link with / (slash). It means address in current site from root of the site. See example for clarity
Link
Example: Badges (Badges)

Categories