Unable to link css file using constant - php

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

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

I have an odd issue with CSS dir in localhost WAMP (Apache)

I've been pulling my hair out over this one.
<link rel="stylesheet" href="/icons/style.css">
<link rel="stylesheet" href="/css/custom.css">
the custom.css ref works fine, the one in the icons dir just returns a "The requested URL was not found on this server.", it will work however if:
<link rel="stylesheet" href="/css/icons/style.css">
... I drop the icons dir into the CSS dir! So, my local WAMP server install is set to only read CSS files from a dir named CSS?
It's one of those things that seems so random and is sucking so much time and energy from me.
Cheers.
this is mean the dir is incorrect so you need to type the correct one
if the file path points to a file in the icons folder located in the folder one level up from the current folder, then your code should be like this
<link rel="stylesheet" href="../icons/style.css">
if this solution not working for you try one of those
https://www.w3schools.com/html/html_filepaths.asp
Answer is, Apache reserves or configs any folder named 'icons' in some special way... https://electrictoolbox.com/apache-icons-directory/

Multiple folders linking to css files PHP

I have a PHP file called header.php which contains as follows:
<title><?php echo $title; ?></title>
<link href="core/css/styles.css" rel="stylesheet" />
<link href="core/css/bootstrap.css" rel="stylesheet">
When on the root folder of my website it is easy to access these files. Simply using: include 'header.php'. However when I am in a directory other than root, for example order/ the files are not able to be accessed. I can access the PHP file by going back a directory using: include '../header.php' however this does not work for the css files included in that file and they cannot be accessed by the file that is not in the root directory.
Can anyone help me with a way to come over this? I like only having one header.php file as I can then add/remove files from that document and the change will appear across the whole site.
Many thanks,
Harry
When using a universal header it's important to use absolute paths to your css files, from the web root. So if core were a folder in the root this would be the revised header.php:
<title><?php echo $title; ?></title>
<link href="/core/css/styles.css" rel="stylesheet" />
<link href="/core/css/bootstrap.css" rel="stylesheet">
Notice the / at the beginning of the path denoting from the web root.

Include HTML files with a PHP script in a different directory while keeping file paths in HTML intact?

From a PHP script, I want to include a "header.php" and "footer.php" file, which are in a different directory. They have HTML in them, and the HTML has references to file paths in them such as:
<link rel="stylesheet" type="text/css" href="css/custom.css">
and
About Us
When I include this html from the php file (which is in another directory) the html looks for the file paths in the directory it's included into, instead of where it's included from.
Is there an easy fix to that? I realize I could go through the html and prepend something like <?php echo dirname(__FILE__); ?> into every file path. But is that the correct way to do that?
Should I restructure my files completely?
Currently the reason the php script is in a different directory is because currently most of my web pages are in the root directory of the server -- so my css js and template files are in folders named "css" "js" and "inc"; but I want to have some web pages be in a url of the format "[root]/Account/settings.php". So I'm trying to put some php pages into a folder in the root directory named Account. But when I try to include my templating files, the file paths in them get messed up.
Use the following path -
<link rel="stylesheet" type="text/css" href="/css/custom.css">
and
About Us
This is assuming that the CSS directory and About Us are both in your document root. Really a simple HTML and path issue rather than PHP, if I am understanding what you are trying to do.

Problems with links not working on local server

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.

Categories