DOCUMENT_ROOT thing Don't Seems to work - php

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>

Related

PHP File that contains Scripts and Resources from Head Tag (Problem with PHP include)

I am trying to create a PHP file that will contain scripts, css links and resources just like I have header.php and footer.php. However, when I create that file and include it in my <head> it does not apply to the page in question.
I have tried including it in <head> tags and <body> tags above the PHP include header.php but still no results. I tried different formats as well.
<?php include 'includes/scripts.php'; ?>
<?php include ('includes/scripts.php'); ?>
I want to inlcude files such as:
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
header location for the code
index code location
scripts file
In your header.php file, and script.php file remove the <HTML>, <HEAD>, <BODY> tags, and also remove the <meta charset="utf-8">. In both of the files, keep only what you want to include into your index.php file.
You do not need to include the script.php file in the header.php file, instead include it only in the index.php file.
So you can keep only these things in your script.php file:
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
Also in your header.php file you can keep only the contents which is wrapped with the <nav> element, and finally include the both files in your index.php file as below:
<!DOCTYPE html>
<html>
<head>
<?php require('includes/script.php');?>
</head>
<body>
<?php require('includes/header.php');?>
// your rest of your elements here
</body>
</html>

connecting header.php up a file level

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'.

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.

Why is site_url hiding my html file

Im trying to hide my url for the css and only display the relative path. here is my html
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="<?php echo site_url('css/bootstrap.min.css'); ?>" rel="stylesheet" media="screen">
</head>
<body>
<h1>Hello, world!</h1>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Then when i view in browser my source code shows this.
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="
How do i fix this. The file im using is a php file.
Did you load the url helper?
$this->load->helper('url');
You can also Auto Load the helpers if you want to only load them once
To autoload resources, open the application/config/autoload.php file and add the item you want loaded to the autoload array. You'll find instructions in that file corresponding to each type of item.
CodeIgniter AutoLoad
I could not find the function site_url in the default php library, is PEAR enabled on your server?
if you want the relative path you could use:
<?php echo $SERVER['HTTP_HOST'].'css/bootstrap.min.css'; ?>
or just
../css/bootstrap.min.css
or directly from the root
/css/bootstrap.min.css

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