Reputation: 6625
I would like to include a header in every page using PHP, so that when I edit the header I won't have to change it on every page. However I am pretty sure if I do that, the relative links will do it according to the page that the PHP is included in. Is there any problem using absolute links to do this? Sorry if this is a simple question and just wondering if there are any rules related to this.
<header>
<div class="navbar">
<a href="/"><img style="margin:10px;" src="../images/logo.png" alt="logo"/></a>
<a href="/"><img style="position:relative;bottom:14px;" src="../images/line.png" alt="line"/> </a>
<a href="/"><img class="navbaricon" id="one" src="../images/home.png" alt="home"/></a>
<a href="JavaScript:void(0);"><img class="navbaricon" id="two" src="../images/artist.png" alt="artists"/></a>
<a href="../releases"><img class="navbaricon" id="three" src="../images/releases.png" alt="releases"/></a>
<a href="../join"><img class="navbaricon" id="four" src="../images/join.png" alt="join"/></a>
</div>
<div class="artists">
<div class="artistlinks">
<p>Brady Hartvigsen</p>
<a href="../artists/bradyhartvigsen"><img src="../images/artists/bradymain.jpg" alt="Brady Hartvigsen"/></a>
<p>Catalyst</p>
<a href="../artists/catalyst"><img src="../images/artists/catalystmain.jpg" alt="Catalyst"/></a>
<p>Emmi Moffitt</p>
<a href="../artists/emmimoffitt"><img src="../images/artists/emmimain.jpg" alt="Emmi Moffitt"/></a>
<p>Frederik Jyll</p>
<a href="../artists/frederikjyll"><img src="../images/artists/fredmain.jpg" alt="Frederik Jyll"/></a>
<p>J.R. Hansen</p>
<a href="../artists/jrhansen"><img src="../images/artists/jrmain.jpg" alt="JR Hansen"/></a>
<p>Kate Berry</p>
<a href="../artists/kateberry"><img src="../images/artists/katemain.jpg" alt="Kate Berry"/></a>
<p>Ryan Cluff</p>
<a href="../artists/ryancluff"><img src="../images/artists/ryanmain.jpg" alt="Ryan Cluff"/></a>
<p>Silter</p>
<a href="../artists/silter"><img src="../images/artists/siltermain.jpg" alt="Silter"/></a>
</div>
</div>
<script>
$("a:nth-child(4)").click(function () {
$(".artists").animate({width:'toggle'},500);
});
</script>
</header>
This is what my header currently looks like
Upvotes: 0
Views: 154
Reputation: 5582
I believe you're referring to including a PHP file in another.
Is there a rule? No. But it is always best to stick to relative paths. Pay attention to the following:
If you use absolute paths to folders/sub-folders you'll have trouble when the site is moved to a different location (even within the same domain). So stick to relative references.
Have some sort of 'escape route' so that if someone tries to read an include file directly it should not make the system vulnerable. For example, you may have a variable set to a certain value and check for it in the file. This is not necessarily a security thing but it may prevent the site from 'behaving in an unexpected manner'.
Example of including a php file in another php file...
Steps: 1. Create a separate file (eg: myheader.php). Remember, this must be PHP. Not HTML. 2. Paste your header in it. Save. 3. In the file that needs to call it, include this line or something similar:
<?php include "myheader.php">
if the file is in a folder above the current folder, use something like this:
<?php include "../myheader.php">
To go two levels above the current folder, use this:
<?php include "../../myheader.php">
If the file is in a sub folder within the current folder, use this:
<?php include "SubFolderName/myheader.php">
Upvotes: 1
Reputation: 987
You can use both absolute and relative paths. When using relative paths you have to make sure the path is relative from the file you are including from.
I would recommend to use relative paths, since moving the site to another server or path on the server would be much easier.
Also, you may be talking about relative html links. I solve that by setting a "rel" parameter in the main file.
Both types of relative solved in this sample:
/mysite/webroot/index.php
<?php
define('RELPATH','');
include_once(RELPATH.'../includes/header.inc.php');
...
?>
/mysite/webroot/otherpage/index.php
<?php
define('RELPATH','../');
include_once(RELPATH.'../includes/header.inc.php');
...
?>
/mysite/includes/header.inc.php
<html>
....
<body>
..
<a href="<?php print RELPATH;?>">Front page</a><br />
<a href="<?php print RELPATH;?>otherpage/">Other page</a><br />
Upvotes: 2