How can i put the css for all specific php pages - php

My folders are here:
In my header.php there is this code:
<link rel="stylesheet" href="style.css">
In the index.php i can include like this:
include('header.php')
I can reach style.css there is no problem.
But, in the product/product.php
include('../header.php')
I can not reach style.css
I can reach the header.php but i can not reach style.css. Because in the header.php, this include code seems that style.css is in the product/style.css, but my style.css is in home folder. How can i reach? Can u help me?

The solution is simple the browsers try to request the file relative to the current directory, so just need to add / in the begging of the HREF to make sure you are referring to a file that is in the root.
Solution:
<link rel="stylesheet" href="/style.css">

Related

Can a CSS code work in all php files if I set the style.css in the header.php but I include header.php in all other php files?

So just to be clear the code from the header.php is here:
<link rel="stylesheet" href="style.css">
And if I put this next part in every other php file:
include 'header.php';
Will the rest of them have access to style.css?
Ok I figured it out. The CSS was not updating so I just had to press ctrl+k for a hard reset.

Loading same assets from different folders using one header and footer

So I have a webpage where I use the same header/footer for every page:
<?php include 'header.php?>
In header link to css looks like this:
<link rel="stylesheet" type="text/css" href="styles.css">
they both are in the same folder.
Problem:
When I try to load page in another folder (products/page.php) - the assets wont load properly. I put a link to header like this:
<?php include '../header.php' ?>
header and footer loads properly but the assets defined in them do not.
How can I fix the paths so I would not need to copy same files to every folder.
Sorry for noob question :)
A pssible solution is to use absolute paths:
<link rel="stylesheet" type="text/css" href="/styles.css">
This way your assets are indepent to your phps structure.

PHP Website - Loading Stylesheets in Sub Directories

I'm building a PHP based site with this directory structure
index.php
css
style.css
bootstrap.css
includes
header.php
footer.php
bikes
road.php
mountain.php
The Problem
So I'm working on road.php and I obviously need to be able to link to both style.css and bootstrap.css, but when I declare at the start of road.php to include the header.php and footer.php it is like as if it cannot find the stylesheets and the site reverts back to the default 1990s look.
I have also found that any form of link on the page loads a 404. I'm only just starting out with PHP because I need some more power in my sites, but I just can't seem to get my head around the super basic things.
I just don't know what to do and I'm finding myself turning my back on the whole PHP language.
Thanking you in advance,
Stu :)
I can't be certain without seeing the actual content of header.php (in perticular the part where you import the stylesheets), but it sounds like you are using a relative path to your stylesheets. Something like <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />. This works fine for index.php, but since the other pages are inside the subfolder bikes, they will be looking for the CSS files in yoursite.com/bikes/css.
The solution is to provide an absolute path. Something like this:
<link rel="stylesheet" type="text/css" href="http://yoursite.com/css/style.css" media="screen" />
This way, it doesn't matter if the page is inside a subfolder (or a subfolder of a subfolder) - it will allways look for the CSS file in the right location.
If you are using multiple domain names, or for some other reason you cannot hardcode the domain name, you can prepend a slash (/) to the path as well:
<link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
This path is relative to the root of the website, not to the current directory.

trouble with php include and file paths

I have a question related to php include.
I have a folder called login and another folder files. I have a footer.php in files and its stylesheet lies also in same folder as style.css.
so , I want to include footer.php in login.php as <?php include('../files/footer.php');?> but its css is missing. how can I solve this problem?
You make your login.php extend some layout.php that has all the required CSS files attached.
When you write your html code, use the complete url (start with http://) to your css and js file in the <head>. This way you will not have problems about relative paths
In the head, you should use an absolute path for your css link:
<link rel="stylesheet" href="http://localhost/your-main-folder/files/footer.css" />
or you can use this:
<base href="http://localhost/your-main-folder/" />
<link rel="stylesheet" href="files/footer.css" />

Web development - relative URLs without duplicating files

I have a site with index.php in the root folder, images in /img , and overview.php in /content. I have a sidebar.php file that is included in both index.php and overview.php. How should I refer to /img/image.gif if I include a link in each file?
The location of image.gif changes relative to the location of the file that references it.
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php.
The only solution that I can see is to either include a separate sidebar.php in each sub-directory, or include an /img directory in every sub-directory.
The best suggestion that I can find is to use the <base> HTML tag as suggested here:
Change relative link paths for included content in PHP
However, in the same link, SamGoody suggests that the <base> tag is no longer properly supported in Internet Explorer, since version 7.
I'd like some insight on the matter before committing to a course of action.
Thanks.
EDIT: I am using the wrong approach below with "../"
Example-
root/index.php:
...
<link rel="stylesheet" type="text/css" href="style.css" />
<title>title</title>
</head>
<body>
<?php include('include/header.php'); ?>
<?php include('include/menu.php'); ?>
...
root/include/header.php:
...
<div id="header">
<span class="fl"><img src="img/dun1.png"/></span><span class="fr"><img src="img/dun2.png"/></span>
...
root/content/overview.php:
...
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
<title>Overview</title>
</head>
<body>
<?php include('../include/header.php'); ?>
<?php include('../include/menu.php'); ?>
...
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php
But it shouldn't. The preceding / makes it an absolute path which will work from any point on the server. If this doesn't work for you, there's a problem somewhere - in that case, post some examples.
Unless you are planning to move the whole site into a sub-directory one day, or move images to a Content Delivery Network (both actions would require re-writing the addresses) you can safely use absolute URLs.

Categories