I know it is duplicate there are many questions as same.
I have included in multiple files, I recently started a new directory and am unable to get some of my includes to work.
I want to include header.php in multiple directory's files but those link in header.php did not work in multiple files. you can see it in my code...
root
|_admin
| |_user
| | |__index.php
| |index.php
|
|_assets
| |_css/js/etc
|
|_includes
| |_header.php'
header.php
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>AdminLTE 2 | Dashboard</title>
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Bootstrap 3.3.7 -->
<link rel="stylesheet" href="../assets/css/bootstrap.min.css">
<!-- Font Awesome -->
<link rel="stylesheet" href="../assets/font-awesome/css/font-awesome.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="../assets/css/AdminLTE.min.css">
<!-- AdminLTE Skins. Choose a skin from the css/skins. -->
<link rel="stylesheet" href="../assets/css/skins/_all-skins.min.css">
</head>
admin/index.php
<?php include_once __DIR__.'/../includes/header.php';?>
its working properly because one directory down.
admin/user/index.php
<?php include_once __DIR__.'/../includes/header.php';?>
its not working properly because two directory down. dont give me "../" or"../../" opinion i have already tried..
You would perhaps be better setting the include path initially and then using that in a more simple manner such as:
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/includes/' );
include 'header.php';
A similar thing with your css / javascript files - if you use the root relative path /assets/file.css etc then it will be available at whatever level of nesting you use.
In header.php change the css links to make them root relative links like so ( assuming directory structure within assets exists! )
<link rel="stylesheet" href="/assets/css/bootstrap.min.css">
<link rel="stylesheet" href="/assets/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="/assets/css/AdminLTE.min.css">
<link rel="stylesheet" href="/assets/css/skins/_all-skins.min.css">
At the beginning of any php enabled age that is to use the header.php file set the include_path
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/includes/' );
And then on the page that needs to use it
include 'header.php';
The css should be available to all pages in all directories if you apply that logic / approach.
Related
it's been some time since I did some coding so I'm basically a newbie in updated PHP (so please bear with me).
I've created a basic draft related to new web I'm working on. I decided to include header.php file (as it's supposed be used on many other pages on that web and I don't want to code header info manually for each page).
index.php file is located in main folder, css, js and images each in a separate folder: web-folders-structure .
All seems to be working fine, but when I simulate either iPad or iPhone display - menu is not resized for those type of screens (even though css styles contain detailed specs for each case).
What it should look like:
expected result
How it looks when header.php is included:
distorted navigation
index1.php start of the code:
<?php include ("./inc/header.php"); ?>
<div id="wrap">
</div>
<!-- navigation -->
content of header.php file (located in "inc" folder):
<?php
//db start
$db = mysqli_connect("localhost", "root", "", "pokus",)
or die('Error connecting to MySQL server.');
//Set DB connection charset to utf-8
$db->set_charset("utf8");
?>
<head>
<title> EGYPTICA - svet starovekeho Egypta 2019 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="sk">
<meta name="description" content="historia starovekeho Egypta">
<meta name="keywords" content="Egypt, staroveky Egypt, stary Egypt, Egyptica, egyptica, egyptologia, knihy, starovek, mumia, faraon, pyramidy">
<meta name="copyright" content="Copyright (c) 2019 Jana Rusnakova">
<meta name="author" content="Jana Rusnakova">
<meta name="robots" content="noindex, nofollow">
<link rel="stylesheet" href="./css/menumaker.css" type="text/css" />
<link rel="stylesheet" href="./css/main-styles.css" type="text/css" />
</head>
<body>
I tried to remove include and insert meta tags directly into index1.php and that works fine on all type of displays. However once I use include (of header) - when I check small screens (tablet, mobile phone) it doesn't work properly.
Any advise as to what could cause this?
Thx
Try adding viewport meta tag.
<meta name="viewport" content="width=device-width, initial-scale=1.0"> .
Hope it helps.
I don't think there is a problem with the inclusion of the header.php file. I would guess that the problem is with the media queries in your CSS file since the hamburger menu is not displayed and the menu items are not hidden...
I am making a website with just PHP. In order to make the header and footer easy to update, I made a master.php file with contents like so:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=1, user-scalable=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../images/a-master-favicon.ico" alt="a-master-favicon" >
<title>Testing site</title>
<link href="assets/css/sticky-footer-navbar.css" rel="stylesheet"/>
<link rel="stylesheet" href="assets/css/head_footer.css" />
</head>
<body>
<?php $signed_request = $_POST['signed_request']; ?>
<?php if(empty($signed_request)): ?>
<?php include('header.php'); ?>
<?php else: ?>
<div id="eatSpace" style="margin-bottom: -175px;"></div>
<?php endif; ?>
<?php include($content); ?>
<?php if(empty($signed_request)): ?>
<?php include('footer.php'); ?>
<?php endif; ?>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/jquery.easing.1.3.js"></script>
<script src="js/jquery.animate-enhanced.min.js"></script>
</body>
</html>
First off the $signed_request part is so I can show certain content when it's loaded via Facebook, that's not the problem.
The problem is that my folder structure is as follows:
root
|
|--assets
| |
| |--css
| |--js
|
|--faqs
| |
| |--faqs-content.php
| |--index.php
|
|
|--footer.php
|--header.php
|--index-content.php
|--index.php
|--master.php
So, when I call say, index.php, it looks like such:
<?php
$content = 'index-content.php';
include('master.php');
This in turn calls master.php and that loads the entire site.
However, if I try to do the same in another directory (i.e. faqs/) and call index.php there, then the code looks slightly different:
<?php
$content = 'faqs-content.php';
include('../master.php');
This loads everything just fine except no css or js loads at all because well the path inside the master file is on a different directory. How can I overcome this?
This is the first time I'm doing anything like this so I'm not sure if what I did is even the best way to go about it.
If your site is simple, you could get away with a combination of relative and absolute links. Your resources would have to use an absolute link like <link href="/assets/css/sticky-footer-navbar.css" rel="stylesheet"/> whereas PHP links can be relative like you described.
However, it doesn't scale well to more complex apps, and can quickly get difficult to manage. Modern PHP frameworks make use of PHP autoloading.
If you don't like re-inventing the wheel, learn a good framework like Laravel or Yii. These take care of autoloading and much more.
I want to use a PHP include for the section. I have an index.php and a head.html in which I have the usual stuff: title, etc plus a link to bootstrap CDN, and a custom CSS file.
Upon testing, the styles from Bootstrap work fine but my custom overrides aren't. I was under the impression custom CSS files automatically override bootstrap? This has worked for me in the past when linking to them traditionally on every page of the website. However, I want to use PHP includes to save time.
I've played around with the order of things (i.e. Bootstrap first, custom CSS first etc), I've tried linking to the custom CSS file separately (outside the include) but can't figure it out.
Any ideas?
My code below:
index.php
<!DOCTYPE html>
<html>
<head>
<?php include("includes/head.html");?>
</head>
<body>
<div class="jumbotron paral paralsec">
<h1 class="hero-heading display-3 text-dark">Some text.</h1>
</div>
</body>
</html>
head.html
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Site Title</title>
<!-- Bootstrap CDN below -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<!-- Custom CSS below -->
<link rel="stylesheet" type="text/css" href="CSS/customCSS.css">
<!-- Font Awesome below -->
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Google Fonts below -->
<link rel="stylesheet" type="text/css" href="#">
customCSS.css (only contains one h1 style, for testing)
h1 {color: white}
Folder structure:
CSS (Folder)
customCSS.css
includes (Folder)
head.html
index.php
Ordering of CSS files plays a less important role than most people think.
When there are multiple rules targeting the same thing, specificity is the most important factor. Higher specificity almost always wins.
When selectors have an equal specificity value, the latest rule (last one called) is the one that usually gets applied.
There's a lot more info on this topic here: https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/
thanks for the suggestions.
It turns out I was experiencing a CSS caching issue. My browser was auto-loading an outdated version of the CSS file. The PHP include and and CSS link were working normally.
If any others come across this thread and are looking for the solution, see here:
https://css-tricks.com/can-we-prevent-css-caching/
Thanks again
Tom
I am learning the php and get some difficulties, I am trying to include the php file in header of another file, but the css is not working?
here is my project directory structure
Project Structure
and here is my php code...
blogpage.php
<html lang="en">
<head>
<?php include 'head.php';?>
<title>Off Canvas Template for Bootstrap</title>
</head>
I have included the path in Struts-2/blogpage.php
here is my head.php
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/offcanvas.css" rel="stylesheet">
<link href="css/blog.css" rel="stylesheet">
Is CSS folder located under the head.php?
Try to add slash to all links:
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="/css/offcanvas.css" rel="stylesheet">
<link href="/css/blog.css" rel="stylesheet">
You may check, adding the links directly, without including th php. Try in that way to see if the links work.
Solution One:
As per your folder structure you have all the style-sheets under the CSS folder and you are trying to include it in the PHP file which is located outside the CSS folder and the procedure it that you have to directly point it out to the CSS folder to pertain the links to all the files that you provide in head.php.
The head.php should look like this so that it will not pertain to any error.
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template -->
<link href="css/offcanvas.css" rel="stylesheet" type="text/css">
<link href="css/blog.css" rel="stylesheet" type="text/css">
Errors:
Your forward slash trailing in the first link will fails since you have added the forward slash over to the bootstrap.css and hence that file will not call out in blogpage.php page. That is the reason you are not getting css over to the blogpage.php.
The type="text/css" is missing to the stylesheet call in the head.php and this may also cause the Error.
And pertaing to that you need to call the head.php in the blogpage.php as follows:
<html lang="en">
<head>
<?php include ('../head.php'); ?>
<title>Off Canvas Template for Bootstrap</title>
</head>
Best Practice
But you can wrap up all the head.php, footer.php, header.php into the includes folder so that it will be a constant under that folder and you can provide up the links without any error.
Since you are calling it inside the Struts-2 folder your path may fail sometimes and the CSS may result in Error.
Tips:One the site is loaded up fully you press CTRL+U so that your entire code will open up in the new window (View Page Source) and you can check to that whether all the links that you are proving are executing correct in the browser.
Solution Two:
You can add up the forward slash over to the CSS folder structure since it will not fails up when you try to add it u over to any of the page that you use. Since all the communication will be done from the root directory on all calls
And your head.php will look like
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="/css/ie10-viewport-bug-workaround.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template -->
<link href="/css/offcanvas.css" rel="stylesheet" type="text/css">
<link href="/css/blog.css" rel="stylesheet" type="text/css">
Possibly a duplicate question, but I have searched all over and haven't found a definitive answer. My CSS files aren't being included in my Bootstrap 3 code.
Here is a sample from a header include file that is at the top of each page. The include is done through PHP.
<title>Page Title</title>
<!-- METAs -->
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, inital-scale=1.0">
<!-- Stylesheets -->
<link href="css/bootstrap.min.css" rel = "stylesheet">
<link href="css/social-buttons.css" rel = "stylesheet">
<link href="css/font-awesome.css" rel = "stylesheet">
<link href="css/styles.css" rel = "stylesheet">
</head>
<body>
<div class = "navbar navbar-default navbar-static-top">
...
All these files are sourced within a CSS file and the file is accessible. I feel like everything is being included properly. Could it be an issue with including the code from a PHP file instead of raw code inline?
The folder structure (for core php project) must be
Main Folder
-- css
--all css files
--index.php / index.html
If your paths are correct, it really shouldn't be a problem.
Let's assume your directory is like so:
/includes
/header.php (calls the below css files)
/css
/style.css
/bootstrap.css
/others.css
/index.php
In your index file, your include would be:
<?php include ('includes/header.php');?>
In your header.php:
<link href="css/style.css" rel="stylesheet"> (etc, your other css files)