PHP: includes and folders - php

I have a small PHP website with most of my pages of the form:
<?php include("heading.php"); ?>
<!-- Content of the page -->
<?php include("footing.php"); ?>
Where "footing" contains some stuff to put at the end of each file and "heading" a lot of stuff (including another include for the menu). The problem is: I'm starting to have a lot of php file sand I would like to put some of them in folders. The straightforward way to do it is to change the code for the files in the folder to:
<?php include("../heading.php"); ?>
<!-- Content of the page -->
<?php include("../footing.php"); ?>
But it doesn't work as the include literately copy the code instead of executing it in the original file's folder, so any include and css in heading.php won't be found unless I copy those files in the new folder.

Modify heading.php so that the paths to the files it loads are absolute. For example, if you load a CSS file from css/style.css use /css/style.css or http://example.com/css/style.css instead.

You also can edit the include path, either in php.ini or directly in your code:
//Must be called before every include, if not stated in your php.ini
$PATH=get_include_path();
set_include_path($PATH.":/absolute/path/to/your/include/folder/");
Then you can use your include this way, even if you are in a sub directory:
<?php
include "heading.php"; //no brackets
include "whatever.php";
?>

It sounds like you are just losing track of where you are in your directory structure. Using ../ takes you back one level and using ./ (or just /) takes you back to the root directory and ../../ takes you back two levels.
The trick is to know where you are in the directory structure and where the file is that you want to include. If you are including or linking to a file in your css folder but you are in a file that is located in the inc folder you have to go back to the root and then to css so you would put /css/filename.ext
However, if you are in the fonts/glyphicons folder and want to link to a font in the fonts/font-awesome folder you could just use ../font-awesome. Clear as mud?
What I do is create three (or more) folders. This helps keep the various files organized and with abbreviated lettering, namely:
css (for stylesheets like bootstrap.css, style.css, etc.)
img (for images, graphics, icons, etc.)
font (for fonts, glyphicons, font-awesome, etc.)
inc (for includes/'snippets' like your heading.php and footing.php)
js (for javascripts such as html5shiv.js, boostrap.js, etc.)
Then in my heading.html file (I use html for the header and footer) I include some items like so:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $page_title; ?></title>
<meta name="Description" content="<?php echo $page_description; ?>">
<meta name="Keywords" content="<?php echo $page_keywords; ?>">
<meta name="Author" content="<?php echo $author_name; ?>">
<meta name="Viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="/css/quicksite-redsand.css" rel="stylesheet">
<link href="/img/mm.png" type="image/x-icon" rel="shortcut icon">
<!--[if lt IE 9]><script src="/js/html5shiv.js"></script>
<script src="/js/respond.min.js"></script><![endif]-->
</head>
<body>
Then in my pages such as the index.php (homepage) I set up some parameters for that particular page plus include some snippets such as header, menu(s), other body text items, and footer, like so:
<?php
$page_name = 'Homepage';
$page_author = 'My Name...';
$page_keywords = 'home, home page, homepage';
$page_description = 'This is the home page for the ...';
$active_home = 'class="active"';
$select_home = 'class="selected"';
include ('inc/config.php'); //common to all pages
include ('inc/header.html'); //common to all pages
include ('inc/menus.html'); //common to all pages
include ('inc/banner.html');
include ('inc/review.html');
include ('inc/footer.html'); //common to all pages
?>
Since in your index.php and probably most of your pages you are at root level, in the home directory so to speak, you only need inc without the /. Looks like PHP doesn't like unnecessary /'s as it gave me an error when I tried...
I know this is a long winded answer but thought I would illustrate for better understanding.

Ok not strictly an answer to your question, but try doing it the other way, have a layout file which has the header and footer included and include the main bit of content dynamically.
<html>
<head></head>
<body>
etc...
<?php include_once '/path/to/content.php'; ?>
etc...
</body>
</html>

I am not sure if I understand your problem properly , but I have the following answer
I think you should use relative css paths to your website address
for example if we have a css file it should be like that
<link ...src="/styles/somecss.css">
so where ever you put it in your application this would take the path
http://wesiteaddres/style/somecss.css
and you can set your include path to whatever directory you want
set_include_path() ;
Hope this would help

Related

Change webpage content without re-using navigation bar on all the pages

I'm working on a website and I want to change the page contents when a user selects which page they want to navigate to. What I'm trying to accomplish would be like ASP.NET where you have only 1 navigation component that is used across all pages and the content of the page changes when a user selects a different page. How would I be able to accomplish this if I'm building a website with HTML/CSS and PHP. Any information I'm getting is how to change page content dynamically from PHP. I want to change the page content from other files in my directory
easy, you'll create normal pages without navigations, with their normal links, then you'll create navigation to add it as a component.
nav.php
<nav>
<ul>
...
</ul>
</nav>
index.php
<?php include_once "nav.php"; ?>
<p>index</p>
contacts.php
<?php include_once "contacts.php"; ?>
<p>contacts</p>
EDIT
If you have too many included and you want a short include to them, you can do this by including all files you want to include then include thi file wherever you want, it's preferred to add them in a separated folder from the pages like components or includes, like
includes / css_files.php
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
includes / metas.php
<meta bla="" bla="" bla=""\>
<meta bla="" bla="" bla=""\>
<meta bla="" bla="" bla=""\>
<meta bla="" bla="" bla=""\>
include / seo.php
<meta keywords="StackOverflow, HTML">
<meta keywords="StackOverflow, HTML">
<meta keywords="StackOverflow, HTML">
for includes.php files you have to choices:
is to put the include file with the pages in the same directory, and it will be like
includes.php
<?php
include "includes/seo.php";
include "includes/metas.php";
include "includes/css_files.php";
you can but it with other includes in the same directory, but dont remove the includes/ before includes.
includes.php
include "includes/seo.php";
include "includes/metas.php";
include "includes/css_files.php";
then the pages will be like
<head><?php include "includes.php"; ?></head>
<p>...</p>
because including in PHP includes the code COPY&PASTE, so you'll treat it as you write it in pages. For example, index.php will be like if you didn't write includes/ before the filename:
<head>
<?php
include "seo.php";
include "metas.php";
include "css_files.php";
?>
</head>
index lorem ipsum
and it will not include it, so nor the include only takes the file and place it in the file as it is without changing anything
You can use PHP include function to control elements from only one file. Write this in your index.php in the place where you want to place header
<?php include('path\header.php'); ?>
and in header.php write the code like
<header>
...
</header>
and you could add css, javascript, jquery resource files into index.php
And advantage of include code is when visitor look at your page source or developer tools' source it will appear as your header.php file not php include line.

PHP include file.php having CSS issues

I have a index PHP page where I include all PHP files like index.php?page=example. All pages are in another folder, here is the structure:
public_html/index.php
public_html/css/style.php
public_html/pages/
Index calls the CSS file from css/style.php.
Pages are called from index.php like (include pages/example.php) using GET function.
If I run index.php I get no problems with CSS, if I run only the included page like example.php I get CSS problems because the CSS is in index.php and obviously will not show the CSS correct.
But when I run the index.php and include the index.php?page=example then the index CSS show correct but the classes from the included pages does not work...
I suppose the include will only import the code but it seems like something is wrong with the server or I am doing something wrong?
Here is a example code of what I am using. This is index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php
include('pages/example.php');
?>
</body>
</html>
Index.php all css classes works fine but the style class from the included pages does not work they are just not styled
You shouldn't write your css code in a php file. Better create a css file and put your style directives in there. You can include css styles best by following conventions, create a basic html template like the following and link to your css file and include the php in there.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="part/to/file.css"> <!-- link your stylesheet here -->
</head>
<body>
<?php
include('path/to/file.php'); // include your php code here
?>
</body>
</html>
Make sure you have header("Content-type: text/css"); as your first line in php file so it renders correctly as css. Then do not include the file. Instead refrence to it like a normal css file only change the .css to .php. <link rel="stylesheet" href="part/to/file.php">. That should get you working. I am assuming your pulling data from a database to fill in your css, so make sure it is format correctly. Do not use something like .headertext{
color:<?=$row['headercolor'];?>; . Instead declare it in php tags. $color= $row['headercolor']; . Then in css part of php file call that variable. .headertext{
color:<?=$headercolor?>;. Hope that helps

Is it good practice to add a php include of the head section in my pages?

I am creating my portfolio site and I am wanting to include the head section as a php include on my page. Reason being is because the site will have a fair few pages and I will want to make changes later on to things later on like tidying up the css files.
For example;
<head>
<?php include('head.php'); ?>
</head>
as opposed to all this below being shown on each and every page:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/1140.css">
<link rel="stylesheet" href="css/ie.css">
<script src="js/vendor/modernizr-2.6.1.min.js"></script>
</head>
I just didn't know if this was good practice to do, as with this being my portfolio site, I need the code to be correct from the start also as they will probably look into the standard of it also.
What are your opinions and advice people? Thanks.
Yep, it's quite standard. But instead of writing:
<head>
<?php include('head.php'); ?>
</head>
you should put the tags inside head.php. I say it's better because what's inside head.php has no sense without the head tags, so they are kinda linked together. It's good practice to join things so linked into a single file without having to repeat open and close head tags for each page.
Actually, it's even good practice (and commonly used) to have header.php, body.php and footer.php files that has respectively:
header.php
<html>
<head>
...
</head>
<body>
body.php
...
footer.php
</body>
</html>
I'm doing that in my application but I've found that it's not a good idea, because you have many of your stylesheets, javascripts, etc in a php file including the head section and you'll have problems with including it in php files in nested folders. this problem is because of relative paths.
If you can use absolute paths then it's ok otherwise it's not a good idea ...
PHP Includes are used like this all the time. Any time that you have content that will be the exact same on every page, it is very helpful to use an include
This is an old topic but I use
<?php include_once("phpinclude/head.txt"); ?>
phpinclude is it's own folder and I keep the footer, header, and common place info in that folder. .js, and .css has it's own as well.
Edit: I use require now. I would rather have a code fail and die rather than give some random string. They are the same except one dies and the other will print out an error or random code. This is for people learning PHP, not old heads.

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.

Include content with php

I want to create a Wordpress like function using php to load variable content in a single page. To better understand what I want to achieve, here is an example: I have an index.php file. It includes three parts (header, content and footer) via php "include" command. Now the header and footer is same for all pages, but I need to be able to include different content in the same page (based on which navigation anchor I click). The problem is that unless I have specific base url (I don't have one because base url would be same index.php) I don't know how to pull specific content. Kindly let me know if this is possible? If yes, how? Thanks in advance.
I agree with the comments made, however, I also believe it would be a shame if you didn't try and learn something new yourself today! Tip: there are many ways to do what you want to do, more or less sophisticated, so don't hold back and dive into some tutorials; I hope this will motivate you (and keep you from running to WP)!
So, to help you to get started, set up the following directory structure:
We have a content folder with all the different pages you would want to 'navigate to'
We have a header.php and footer.php that are pulled in on every request to index.php.
We have index.php, the page where all the magic happens.
Let's have a look at the code(I'm assuming you're running this on localhost):
header.php
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Test</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
</head>
<body>
page0
page1
In header.php we define two <a href =...> 's. These link to the same destination, index.php, but they each set a different query string variable: ?page=page0 and ?page=page1.
Query string variable page will be picked up by index.php through $_GET['page'] to pull-in the content for either page0.php or page1.php respectively.
index.php
<?php
require "header.php";
if(isset($_GET['page'])) {
$target = "content/" . $_GET['page'] . ".php";
require $target;
}
require "footer.php";
As you can see, index.php waits for $_GET['page'] to be set, i.e. a link to be clicked, to then pick up the information set in $_GET['page'] variable and use it to require the correct content (i.e. page0.php or page1.php).
Closing off your HTML with a proper footer...
footer.php
</body>
</html>
And finally, the content pages:
page0.php
<?php
echo "Page 0 content";
page1.php
<?php
echo "Page 1 content";

Categories