Using absolute links in PHP included header - php

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">
<img style="margin:10px;" src="../images/logo.png" alt="logo"/>
<img style="position:relative;bottom:14px;" src="../images/line.png" alt="line"/>
<img class="navbaricon" id="one" src="../images/home.png" alt="home"/>
<img class="navbaricon" id="two" src="../images/artist.png" alt="artists"/>
<img class="navbaricon" id="three" src="../images/releases.png" alt="releases"/>
<img class="navbaricon" id="four" src="../images/join.png" alt="join"/>
</div>
<div class="artists">
<div class="artistlinks">
<p>Brady Hartvigsen</p>
<img src="../images/artists/bradymain.jpg" alt="Brady Hartvigsen"/>
<p>Catalyst</p>
<img src="../images/artists/catalystmain.jpg" alt="Catalyst"/>
<p>Emmi Moffitt</p>
<img src="../images/artists/emmimain.jpg" alt="Emmi Moffitt"/>
<p>Frederik Jyll</p>
<img src="../images/artists/fredmain.jpg" alt="Frederik Jyll"/>
<p>J.R. Hansen</p>
<img src="../images/artists/jrmain.jpg" alt="JR Hansen"/>
<p>Kate Berry</p>
<img src="../images/artists/katemain.jpg" alt="Kate Berry"/>
<p>Ryan Cluff</p>
<img src="../images/artists/ryanmain.jpg" alt="Ryan Cluff"/>
<p>Silter</p>
<img src="../images/artists/siltermain.jpg" alt="Silter"/>
</div>
</div>
<script>
$("a:nth-child(4)").click(function () {
$(".artists").animate({width:'toggle'},500);
});
</script>
</header>
This is what my header currently looks like

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>
..
Front page<br />
Other page<br />

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">

Related

Dynamic header filepath

I am writing a website and I decided to separate my header into it's own php file and then use the include statement to insert it back into my webpage. The issue I'm having is on my index.php I'm sourcing my logo as such:
<img class='logo' src='images/logo.png'>
and on my meats/sausage.php page I source it as such:
<img class='logo' src='../images/logo.png'>
Is there a superglobal or something I can use instead to direct both pages to the correct location or do I need to make separate header versions for each of the 2 pages?
Note: I'm using XAMPP and localhost rather then running it on the web just yet.
Use a constant or something that defines your base directory and work with that.
For example:
<?php
$baseDirectory = __DIR__;
function assets_path($append)
{
return "{$baseDirectory}{$append}";
}
And then:
<img class="logo" src="<?= assets_path('images/logo.png') ?>">
Choosing where to place this is on you.
A possible option would be to use .htaccess file and the RewriteRule like so:
RewriteRule ^(.*)SiteAssets/(.*)$ http://YourSite.com/AssetsRoot/$2
And then use like this:
<img class="logo" src="SiteAssets/images/logo.png">
Also, if your DocumentRoot setting is correctly set in httpd.conf, you could simply use this throughout your site:
<img class="logo" src="/images/logo.png">
See https://stackoverflow.com/a/7823598/715105
Base tag
<head>
<base href="https://example.com/">
</head>
This will set your base on every page. No need to use ../images
see https://www.w3schools.com/tags/tag_base.asp
Duplicate of this question:
src absolute path problem
The trick was to reference it as such:
<img src="http:\\localhost\site\img\mypicture.jpg"/>
but with forward slashes rather then backward slashes

Issues with relative paths in PHP includes

Before going on, I'd just like to say that I've tried pretty much everything, and I read a lot of questions here at Stackoverflow, but none of the solutions worked.
So here is the deal, I have a file ("empresaConf.php") that contains this code:
<?php
include 'navigator2.php';
?>
<div class="contTudo">
<div id="bgCorpo">
<div class="empresaConf">
<?php
include 'sqEsqInc.php'
?>
</div>
</div>
<?php
include '../rodape.php';
?>
</div>
</body>
</html>
As you can see, it is including "rodape.php". The problem is that in "rodape.php", I have an image that is in another folder, so when I include "rodape.php" in "empresaConf.php", the image path should change, since PHP include consider the file path to be the one that you're including other files, making rodape.php's image broken when I access "empresaConf". I want something to fix that so I have the image no matter what file I'm in. Here is the "rodape.php" code:
<div class="empresa">
/* below in the img src tag you can see what I'm talking about */
<div class="redeSocial">
<a href="http://www.facebook.com.br">
<img src="imgs/icons/iconFace42.png" class="iconeFace">
</a>
</div>
</div> <!-- Fim div .empresa -->
The folder structure is: "rodape.php" is in the root directory and "empresaConf.php" is inside a folder named "Empresa" (I would post a img but I can't).
Thanks in advance.
EDIT:
Sorry, I forgot a crucial detail, I'm also including "rodape.php" in other files such as "index.php". So if I change the img src to correspond "empresaConf.php", then in the "index.php" the image will be broken, since it's in the root directory, same as "rodape.php".
If it's an image accessible from the public side of things, the image can probably be accessed by the directory root. So if your index.php is in /index.php where / is the web document root and your image is in /imgs/icons/iconFace42.png you can access the image by
<img src="/imgs/icons/iconFace42.png" class="iconeFace">
Note the leading slash in the src attribute.
You have some options.
If rodape.php will only be accesed from that folder level, simply change the src to ../imgs/icons/iconFace42.png, if you will be changing folder deep then use an absolute path to the image. Another way would be add php code to set the src but that would be wast of resources.
When you include ../rodape.php, think of it like just putting the code from that file directly in the position where it was included. The path string will not change, as it has not been executed until it has been included.
In short, that is to say, things will be relative to the file that's being executed, not the file(s) that's being included.
<img src="../imgs/icons/iconFace42.png" class="iconeFace"
Using that image code should resolve your issues.

Load an image from included php not in root directory of server

I'm stuck in a simple thing as loading an image in php.
All included php files are in a directory called includes.
Index.php is in root folder.
When I call img src from the php file located in the root folder, I can see images.
But when I include a section that is supposed to load an image like that:
<?php include("includes/section.php"); ?>
calling this:
<div class = "right">
<h1> news </h1>
<hr>
echo '<img src="../img/do.png" width="200" alt="Rifrazione news/>';
<p class = "rightp">Blabla </p><br
</div><!--end of right-->
I get nothing
for images I prefare use global variable.
Try do something like this:
<?
$baseUrl = "http://localhost"; // CHANGE IT TO YOUR URL
?>
<div class = "right">
<h1> news </h1>
<hr>
<?
echo '<img src="'.$baseUrl.'/img/do.png" width="200" alt="Rifrazione news" />';
?>
<p class = "rightp">Blabla </p><br />
</div><!--end of right-->
The file you're currently in is your current working directory. So find the location of your image from the directory your index.php is in, that means probably removing the ../. If you have any doubts echo getcwd();.
NEVER do what Adam suggested. You should always use file system paths, never url paths to local files. EDIT Reasoning: Hotlinking is part of it as he may not be aware of why something isn't working as it has worked in other locations but mostly due to routing/.htaccess/url rewriting. If he were to move on later to an MVC or similar framework putting localhost/controller/action will not return his image, but he may not be sure why as this is a bad habit he has already picked up. The other reason was for ease-of-use, if he puts a path like ./image/here.jpg opposed to localhost*/here.jpg he may have to go through and rewrite every link if he were to migrate later to an external server.
Example to help visualize better:
If you have the following directory:
my_site/
js/
css/
images/
image.jpg
index.php
bob/
bob.php
contactBob.php
If you were viewing index.php, your link to images would be /images/image.jpg.
If you were viewing contactBob.php your link to images would be ../images/image.jpg.
In HTML5, you can declare the <base> as a DOM element.
After searching and testing...it was none of that.
The only thing missing was the php tag at the beginning of the bracket.
You don't need variables at all.
This syntax is working as intended.
<?php
echo '<img src="img/do.png" width="120" alt="Rifrazione news"/>';
?>
Thanks for jumping in, anyway.

php file path require syntax for importing a page

I have a directory called images and about and 3 php file on home directory
and 3 files and have different content
footer.php
This is footer <img src="images/logo.png">
header.php
<h1>Welcome to MyWebsite</h1>
index.php
<?php
require('header.php'); ?>
Enter your name : and some forms and javascript code
<?php
require('footer.php'); ?>
Now i have file in directory about as about.php
and it has some contents and below code is
about.php
<?php require('../header.php'); ?>
This is About page<br>
<?php require('../footer.php'); ?>
And when i open the page about.php, the footer is working fine but the image is not showing up, and image directory has image as logo.png
Even i used realpath to work out with relative paths, but could not display.
Even i tried this one too in footer.php
<?php
define('__ROOT__', images(__FILE__));
?>
This is footer <img src="<?php echo require_once(__ROOT__.'/logo.png); ?>">
I tried out the possibilites of relatives paths, do we have any other thing.
If I'm understanding your question correctly, the easiest solution is probably just to use an absolute path for your image tag in footer.php:
So if images is in your docroot, it would look like this:
This is footer <img src="/images/logo.png">
Including images in the php file makes absolutely no sense.
You have to understand the difference between PHP code and HTML code.
PHP code being executed on the server and have result of HTML code sent to the browser.
Where browser reads that HTML and do additional requests to the server to get images.
Thus, server filesystem root has absolutely nothing to do with browser.
As for the problem - Trott's answer is perfect.
Well if you are using include() to get certain section of the Page with PHP, there will definitely be a problem with links.
I believe the best practice is to call images using CSS. Just get a div with a class or id and place the relative link in the CSS.
It worked for me.
I got the answer for this questions
copy the below code in header.php and it will work every where
<?php
define('ROOT_DIR', dirname(__FILE__));
define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
?>
And where ever there is a image you src="" just put the below code only with imagename changed, and whatever you want.
<img src="<?php echo ROOT_URL .'/images/logo.jpg'; ?>">

PHP: Defining relative paths compatible with HTML Tags

Consider the following directory structure:
ROOT
------ images
............... logo.png
------ includes
............... vars.php
------ layout
............... content.php
------ index.php
How do I define a path constant for logo.png in vars.php that is accessible in both index.php and content.php? Should be compatible with HTML Tags as a relative path.
<img src="<?php echo IMAGE_PATH; ?>">
which should be parsed as
<img src="images/logo.png"> <!-- if used in index.php -->
and
<img src="../images/logo.png"> <!-- if used in content.php -->
New Question (EDIT): Does root-relative path work when including php files using include / require methods?
Try setting the <base> tag in the <head> section of your code.
All your images, css, and js files will use this instead of the url in the address bar.
Info on base
Absolute url or root paths will give you the least amount of headaces. Trust me, when the system grows you'll regret that setup.
It is a perfectly legal way to reference things. (as you ask in the comments)
If you're worried about setups between domains, just create a config variable with the absolute path to the domain / directory / etc
You can use "root-relative" paths. Simply link to everything with a forward slash at the beginning, i.e.
<img src="/images/logo.png">
This will resolve to http://yoursite.com/images/logo.png from every page on yoursite.com.
simply specify all paths as relative to the root
<img src="/images/logo.png"> <!-- will work anywhere -->
I'd suggest, primarily, that you use root-relative paths. This is only to reduce the complications of moving your site to another host, and also it allows for consistent paths (rather than using an if() condition to test from where the script's being run).
But otherwise, your suggestion would be fine.
I would use something like an application base URL:
define('APP_URL', 'http://example.com/path/to/app');
echo '<img src="'.APP_URL.IMAGE_PATH.'">';
Or to have it more convenient, write a function that resolves your relative URL to an absolute URL.

Categories