Problems with links not working on local server - php

I'm trying to set up my site in Dreamweaver CS5 to work with my local server, and I'm having issues with document relative links.
I've got a structure on my HD like this
website
_Common
header.php
_css
twoColFixRtHdr.css
index.php
and the same structure mirrored on my local WAMP server, except on the local server the site is in a subfolder, so it's something like www/website/
The problem is this line inside header.php
<link href="../_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
That looks correct to me pathwise, but on the local server it cannot find that css file from the header.php
If I change it to
<link href="/website/_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
or
<link href="_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
It works fine, but I want to use document relative links if I can, any ideas?

This sounds clear to me, since I'm sure that you include the header file in the index.php file. So the path for the css files is set relatively to the index.php.

you may achieve it by several ways.
one of them: you can setup a VirtualHost (in httpd.conf) and point your subfolder as root folder for host.

If header.php is being included in index.php, the relative link ../_css/ in header.php won't work because index.php is at the same level as _css.
For all intents and purposes, once it's been included in index.php, to the browser, the content in header.php is now simply part of index.php, so all paths need to be relative to index.php.
Ie:
index.php
/my_include_folder
- header.php
/_css
- style.css
Once I add <?php include('my_include_folder/header.php); ?> to index.php, the links to css files, js, and hyperlinks in header.php should be relative to index.php.
Hopefully that makes sense.

Related

What is the best practice to include CSS and Scripts in php? [duplicate]

i have started to add include files like header, nav, footer.php to my index.php in my root directory, i got a admin folder with another index.php, with the same include files except it is using "../" in its target path to go back one before accessing the includes file, what happens is it works except for the css files... i lose my styling, but on my root index.php if i go back to that, the styling is working.
Any idea why this is happening?
my css code is:
<link rel="stylesheet" href="css/main.css">
Use an absolute path in your link tag
<link rel="stylesheet" href="/css/main.css" />
As kindly pointed out by John, this is not an absolute path and in all honesty I have no idea what to call it (root relative?). What I do know is that it is relative to the root of the site and not the current folder on the server.
The syntax you're using is relative path.
Use either this to fix the relative path:
<link rel="stylesheet" href="../css/main.css">
Or make it absolute if your css directory is in the web docroot:
<link rel="stylesheet" href="/css/main.css">
if css folder is outside of your admin folder it should be ../css/main.css, right?
For a better management, I'll use an abolute path with full url
<link rel="stylesheet" href="{$html_css}/main.css" />

PHP File Path With Include

This is how my project is currently set up:
-index.php
-css
--index.css
-includes
--head.php
head.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/index.css">
<title> My Website </title>
</head>
<body>
index.php
<?php
include(__DIR__.'/includes/head.php');
?>
The problem is that index.css does not work. I don't see what I'm doing wrong at this point. Any help would be greatly appreciated. Thanks.
The problem is in the way PHP include() works; it literally includes the code from head.php into index.php, without updating any relative links. As such, you index.php is looking for the CSS file in ../css/index.css. And while your head.php does indeed need to go up one directory before looking in the CSS folder, your index.php file does not.
To resolve this, you have a number of options:
You can update the relative path to css/index.css to work from your index.php file.
You can use the root-relative path /css/index.css to reference the CSS file from any folder.
You can use the absolute path https://yourwebsite.com/css/index.css to reference the CSS file from not only your website, but any other website. This further eliminates confusion, but will cause issues if you change the domain.
Personally I would recommend the root-relative path, as it makes things less confusing when using things like includes and partials.

Moving website to localhost: Document Root changed

I have download a website that was written from scratch using PHP, MySql, html, css, jQuery.
After fixing some configuration with MySql and htaccess, I get to a point where all my CSS and JS files are not loaded because of a relative path error.
This is how it looks:
The error in the console GET http://localhost/fonts/fonts.css sends me to index.php which loads all those files with a relative path.
Now, if I remove the / at the beginning of each line, the files will be loaded, however, I get more errors on different pages with bad URLs and such, that means that I need to fix this / problem instead of just renaming the hrefs.
So my question is, how to fix it?
I have tried to set a base href tag:
<base href="http://localhost/israelrescue/">
or
<base href="http://localhost/israelrescue">
or in PHP:
$_SERVER['DOCUMENT_ROOT'] = 'http://localhost/israelrescue';
or
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/israelrescue/';
P.S:
israelrescue is the name of the folder I am using in my localhost installation for this website.
Nothing seems to work, any ideas?
Thanks!
Answers:
It's because you've got the site in a subfolder in localhost. Is it possible for you to move the site into the root? I think #MukeshPanchal is telling you to load the assets from live, which is missing the point somewhat. – thebluefox
Looks like this site was not written to be run from within a
sub-folder. Easiest way to work around that would be to set up a
VirtualHost for this project in your local server, so that you can
load it directly via a local domain name, without any additional path.
(And btw., setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.)
– CBroe
You can leave the site in its subfolder as long as you drop the "/" in the beggining of every file path :
<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/common.js"></script>
becomes
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/common.js"></script>
Should work like this

header.php not displaying properly from root folder homepage

I have the index.php in my public_ root destination which has the header taken using php include from the folder include/header.php.
But when the index page loads the menu and images within the header.php doesnt load because its directory is from include folder.
Ex in header.php the menu will be in directory
<link rel="stylesheet" href="../scripts/menu.css" type="text/css" media="all" />
but the index.php directory to menu stylesheet would be scripts/menu.css. So i guess the menu styles are not loading.
Is there a way to get the index page right without having to have header in the same folder as index.php or is it because i am testing this on windows using xampp?
Let me clear this one up for you.
When you write HTML and CSS from PHP like you are doing here, the HTML and CSS don't know where the PHP is being run from or anything about the PHP. This is because PHP is interpreted and writes out the page into HTML which is then processed in your browser (i.e. the PHP never reaches your browser, all it sees is HTML).
This means that you must add the correct paths to the CSS files relative to your web server root not the PHP script location (in your case that is probably "/scripts/menu.css" for the example you've provided - assuming that the scripts is in the root web folder).
So for example, on your website / means the root of the web server (i.e. mywebsite.com/) and anything after that is it's path relative to the root of the web server.
By using /mycssfolder/mycssfile.css like I suggested, you can ensure that the location is correct regardless of the page which the PHP script is called on.
Hope this helps.
Ben
<link rel="stylesheet" href="/scripts/menu.css" type="text/css" media="all" />
You might try simple removing the .., like shown above. Remember, any external source file that you include becomes part of the destination. Thus, think of the location of your CSS file relative to index.php.

Absolute Address for CSS

I have the following file structure on my PHP localhost:
Root
css/
main.css
admin/
admin.php
index.php
header.php
Both admin.php and index.php are requiring header.php, in which main.css is included like the following:
<link rel="stylesheet" type="text/css"
href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/css/main.css" />
However, the css file isn't included properly. When I look at the Source Code in Chrome on Mac, The browser has turned that into
http://localhost/Users/ljhljh235/Documents/web/hetd/css/main.css
in which http://localhost is not intended to be here. Could anybody help me on how to build the correct absolute path for the css file?
P.S. My way of including header.php in admin.php and index.php is
require_once ($_SERVER['DOCUMENT_ROOT'].'/header.php');
And I'm using MAMP 2.1.1.
Thanks for any help.
The variable $_SERVER['DOCUMENT_ROOT'] is for server side path, use /css/main.css only, it will be relative to the domain ie : http://domain.com/css/main.css

Categories