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
Related
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" />
I am new to php and facing initial problems :).
My directory structure is as follows :
site
index
homepagesections.php
assets
css
style.css
config
db.php
include
header.php
index.php
In the index.php , which is in the root folder I need to include the header.php file which includes config/db.php and also assets/css/style.css.
I have tried all the options ( including. the DIR option ) but failed. What is the best way to include these files and also the path for the style.css ?
In index.php path to header is include/header.php:
include('include/header.php');
In header your path to db.php is ../config/db.php so you will have:
include('../config/db.php');
Note '../' - this is how you navigate 1 level up, from the directory your file is. In header adding css will be something like:
<link rel="stylesheet" href="../assets/css/style.css">
In index.php:
include (__DIR__.'/include/header.php');
In header.php:
include (__DIR__.'/../config/db.php');
include (__DIR__.'/../assets/css/style.css');
I am trying to include css file with php so that i have one same path through my sub folders and files as well. This is my code
<link rel="stylesheet" href="<?=ROOT;?>CustomStyles/MyStyles.css">
I have constant root set up before in this way:
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
However this does not work because the result of the link is that after ROOT constant there is the web address of my server.
Can anyone help me please?
Thank you very much.
Place your css file in public directory. Example assets/. And link
<link rel="stylesheet" href="//your.site/assets/MyStyles.css">
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.
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.