connecting header.php up a file level - php

I'm trying to connect my PHP header from a file that is in a folder.
I'm attempting to do this by using:
<?php include("../header.php"); ?>
It works to bring the content in that file on the page, but its not applying the style.css file to the doc. Why?
Here is the contents of header.php:
<!DOCTYPE html>
<html>
<head>
<title>%TITLE%</title>
<link href='http://fonts.googleapis.com/css?family=Lilita+One' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="includes/normalize.css">
</head>
<body>
<header>
<div class="page-wrap">
<h2 id="logo">Some Company</h2>
<nav>
Home
Contact
Rentals
For Sale
</nav>
</div>
</header>
<section id="mainContent">

This is probably because its trying to reference the css at 'style.css' but try putting '../style.css'. It should allow the styles but it won't fix all your pages. In that case its better to use something like this:
<?php
// Set a constant with your url
define('BASE_URL', 'http://localhost:64411/TestingPHPStuff/');
echo 'Now use it in your html like this:';
?>
<link rel="stylesheet" type="text/css" href="<?=BASE_URL;?>style.css"/>
That way file location does not matter.
Basically though, you are trying to use a stylesheet that doesn't exist at that current level. Probably.
Edited
I changed it. If you include the 'http://' and the port ':64411' (for example you would use your port) then it should work. I just tested it and it only worked for me with both 'http://' and ':64411'.

Related

php , include, inject code of de header in the base file

The body of the base file has a include ' slider.php '.
In de slider.php there are css and javascript files that need to be load.
Because of the include it will but all of that in the body where the include is posted.
My question is: Is it possible if you have a include the file. You can say in the include put css and javascript in the <header> tag and not where the include is made.
[index.php]
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="slider">
<?php require_once( INCLUDES . 'slider.php');?>
</div>
</body>
</html>
[slider.php]
<?php
<!-- slider -->
<link rel="stylesheet" type="text/css" href="/normalize.css" />
<link rel="stylesheet" type="text/css" href="/demo.css" />
<link rel="stylesheet" type="text/css" href="/component.css" />
<script src="/slider.min.js"></script>
<script src="scattered_slider/classie.js"></script>
<div class="row header">
etc...
No, it's not possible to do that using only a single include/require.
The fastest solution would be to use two different files "slider_header.php" containing the javascript/css tags and "slider_body.php" containing the actual slider html.
Then you could include them like this:
<!DOCTYPE html>
<html>
<head>
<?php require( INCLUDES . 'slider_header.php');?>
</head>
<body>
<div id="slider">
<?php require( INCLUDES . 'slider_body.php');?>
</div>
</body>
</html>
Maybe you should consider using a proper template engine like Twig, but this requires a little bit more effort to set it up in the first place.

DOCUMENT_ROOT thing Don't Seems to work

As advised, I pasted the following code in one of my pages:
include $_SERVER["DOCUMENT_ROOT"] . "/includes/navMain.php";
And, well it shows off the following errors:
Warning: include(D:/xampp/htdocs../includes/navMain.php): failed to open stream: No such file or directory in D:\xampp\htdocs\adamsProject\pages\contactUs.php on line 4
Warning: include(): Failed opening 'D:/xampp/htdocs../includes/navMain.php' for inclusion (include_path='.;D:\xampp\php\PEAR') in D:\xampp\htdocs\adamsProject\pages\contactUs.php on line 4
It says no such file in blah blah blah directory. Thing is, it has! In fact, the error is pointing to the right direction.
There's an extra .. At the end that is making the path invalid. When only printing $_SERVER['DOCUMENT_ROOT'] what is the path?
You may be able to use dirname( __FILE__ ) to get the path of the current file, then use ../ to go back each directory until you reach includes.
Notice how the head.php opens the <html> and <body> tags and footer.php closes them.
The point of using the includes is so there is no need to duplicate the HTML code on every page. You can simply just include it on any one you need, so things that would stay the same across the site I would assume would be the header and footer, whereas each page's content will differ and you can declare that in individual files (main,contact,services,about,etc).
//MAIN.PHP
<?php
include('head.php');
include('header.php');
?>
//PAGE SPECIFIC CONTENT
<header></header>
<section></section>
<?php
include('footer.php');
?>
//HEAD.PHP
<!DOCTYPE html>
<html>
<head>
<title>HOME | ADAMS Project</title>
<link rel="icon" type="image/png" href="images/favicon.png">
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" type="text/css" href="css/tablet.css">
<link rel="stylesheet" type="text/css" href="css/fontAwesome/css/fontAwesome.min.css">
<!-- <link href='http://fonts.googleapis.com/css?family=Oswald:400,500,700,300' rel='stylesheet' type='text/css'> -->
<script type="text/javascript" src="js/jqueryMin.js" ></script>
<script type="text/javascript" src="js/roundAbout.js"></script>
<script type="text/javascript" src="js/galleryInit.js"></script>
<script type="text/javascript" src="js/roundAboutShapes.js"></script>
</head>
<body>
//HEADER.PHP
<header>
Home
About
Services
About Us
Contact Us
Portfolio
</header>
//FOOTER.PHP
<footer></footer>
</body>
</html>

php page automaticly adding doublequotes to my page

I'm having a problem where my page automaticly puts two doublequotes outside php tags ("")
This happens as soon as the page contains <?php and ?> and I can't find out why.. Nor should it be my php settings because files in other directories work... except for this files somehow, here are the codes:
index.php
<html>
<?php include 'include/head.php' ?>
some data to test
</body>
</html>
head.php
<head>
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
</head>
<body>
<div id="test">test data</div>
and both of the CSS files are empty, I work with .css.php because I have been told that this could greatly improve loading speeds.. This all together gives me the following code in chrome, and Internet explorer:
<html>
<head></head>
<body>
"
"
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
<div id="test">test data</div>
"
somedatatotest"
</body>
</html>
EDIT: Real browser source:
<html>
<head>
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
</head>
<body>
<div id="test">test</div>
some data to test
Change your index.php file to this:
<html>
<head>
<?php include 'include/head.php' ?>
</head>
<body>
<div id="test">test data</div>
</body>
</html>
And your head.php file to this:
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
No questions, just do this. Promise it will solve your problem.
EDIT: Also, why is it common.css.php and not just common.css?
EDIT 2: Okay, do this then. You've got to atleast try some of the suggestions, otherwise no one will want to help you.
//index.php
<html>
<?php include 'include/head.php' ?>
<div id="test">test data</div>
</body>
</html>
//head.php
<head>
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
</head>
<body>
<div id="test">some head test data</div>
You're not passing a well formed HTML document to your browser, and the rendering engine is trying its best to show what it has got.
Since your HTML isn't well formed, you can't expect a predictable output. Fix your source, and the problem will be solved
Okay I found the problem, It seems that my Notepad++ settings were wrong, it would create files with the UTF-8 coding and ANSI enabled, but since I set my settings to UTF-8 (WITHOUT BOM) and ANSI coding ignored everything seems to be workign fine again.

MAMP LocalHost server not working

LocalHost shows blank page. I changed port to 80 for Apache and when I press open page, it goes to localhost/mamp just fine. When I delete /mamp, all I get is a blank page. I've tried multiple things like shutting down mamp, starting it up, restarting my machine. I've checked my folder set-up and it's fine.
I tested it by creating a new file page index.html and it opens just fine.
I have no clue why it's not opening my index.php file. Here's my block of php code from header.php that I have.
<html>
<head>
<title><?php echo $pageTitle; ?></title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
<link rel="shortcut icon" href="favicon.ico">
</head>
And here's my index.php
<?php
$pageTitle = "A store of unique t-shirts";
include('inc/header.php');
?>
Any help would be appreciated.
Anything between <head> and </head> shouldn't show up on the page, so maybe try adding some content that would be printed on the page. For example, you could change header.php to this just to see if anything shows up:
<html>
<head>
<title><?php echo $pageTitle; ?></title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
<link rel="shortcut icon" href="favicon.ico">
</head>
<body>
<p>Hello, this is content.</p>
</body>
</html>

Alternatives to using absolute paths in included php files

I have a file called "header.php" that I am including on every page on my site and this file contains links to other files on my sever like css files, jquery plugins, etc.. and right now I am using absolute paths for those links so they will work with files that are not in the same directory as the header.php file and this works, but as you can see in the example below, things start to get really hard to manage if you your header.php file contains lot's of links (which mine does) so I would like to know if there are any other alternatives to using absolute paths in the header.php file like I have done here.
header.php
<?
$base_url = "http://example.com";
?>
<html>
<head>
<title> <? echo($title); ?> </title>
<link rel="stylesheet" type="text/css" href="<? echo($base_url); ?>/styles/some_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="<? echo($base_url); ?>styles/another_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="<? echo($base_url); ?>styles/another_css_file.css" media="all"/>
...
...
...
<script type="text/javascript" src="<? echo($base_url); ?>/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<? echo($base_url); ?>/scripts/some_jquery_plugin.js"></script>
<script type="text/javascript" src="<? echo($base_url); ?>/scripts/another_jquery_plugin.js"></script>
...
...
...
</head>
Some file which includes header.php
<?
$title = "some page title";
include("header.php");
?>
<body>
PAGE CONTENTS
</body>
</html>
Use the HTML base tag to define the base of your site.
<base href="<?php echo $base_url; ?>" />
<link rel="style.css" />
Absolute path is the way to go here. But, if you have a lot of links to print, it can be simplified by storing the paths in an array and looping through the array.
$base = "http://www.example.com/"
$links = array( "styles/file1.css", "styles/file2.css", ... );
foreach ( $links as $link ) {
echo '<link rel="stylesheet" href="' . $base . $link . '" type="text/css" media="all" />';
}
As a side note, what you have isn't great practice. It would be better and more efficient for you to try and combine some of those files as this will reduce the amount of HTTP requests the browser has to make to your server. http://developer.yahoo.com/performance/rules.html#num_http
I had the same doubt in one of my projects and i've done as the code bellow. As the paths start with "/", the file will be found based in the root directory and in this case isn't necessary to specify the domain, it will turn the things easier for maintenace and prevent problems if you will do rewrite of URLs using (mod_rewrite). Hope it can help you too!
<html>
<head>
<title> <? echo($title); ?> </title>
<link rel="stylesheet" type="text/css" href="/styles/some_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="/styles/another_css_file.css" media="all"/>
<link rel="stylesheet" type="text/css" href="/styles/another_css_file.css" media="all"/>
<script type="text/javascript" src="/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/scripts/some_jquery_plugin.js"></script>
<script type="text/javascript" src="/scripts/another_jquery_plugin.js"></script>
</head>
</html>
This is not a direct answer to your question, but it may help you.
If you are including that many css and JS files you should look into using Minify. Minify will take a bunch of css/js files and compress/combine them into one file, which will greatly speed up the loading of your site.
Using this could also help with your path concern, as you can specify directories and groups very easily.
You can find information about it here: Minify

Categories