PHP Include not working - php

I am trying to use PHP's include function in conjunction with html but it is not working. I'm just messing around with the tag, it seems to be very simple but i cannot figure out why it is not working...
here is the template.php page:
<!DOCTYPE html>
<html>
<head>
<title>Template</title>
<link href="css/layout.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="wrapper">
<?php
include ("include/header.php");
include ("include/leftcolumn.php");
include ("include/rightcolumn.php");
include ("include/footer.php");
?>
</div>
</body>
</html>
and here is the header.php page that I am trying to include:
<header>
<p>Header.</p>
</header>
The only thing i could think of is the wrong directory, I have an include folder which is where the header.php is and referenced that in the include tag...unless I'm wrong, any tips? Thanks

http://php.net/manual/en/function.include.php
you dont need to use parenthesis try :
<?php
include "include/header.php";
include "include/leftcolumn.php";
include "include/rightcolumn.php";
include "include/footer.php";
?>
and make sure the paths correct.
edit:
every exemple on the php manuel are dont have parenthesis.
And a question. where exectly you try to execute your php file. just in a browser or using any wamp server aplication to simulate server side action for your php ?

Remove your Brackets ()
<!DOCTYPE html>
<html>
<head>
<title>Template</title>
<link href="css/layout.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="wrapper">
<?php
// removing brackets... hopefully it will be working....
include "include/header.php";
include "include/leftcolumn.php";
include "include/rightcolumn.php";
include "include/footer.php";
?>
</div>
</body>
</html>

I didn't tested your code, but be sure of your path (is your template.php file to the root, before your include folder?) (it's crazy to say, but pathing are a frequent problem, and it can easily making you mad)
Or, try this syntax too :
include 'include/footer.php';

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.

Php include add title tag

Okay people, its something very common the only thing is that I have no idea how to deal with it. i have a file "login.php" the codes within the file are
<div><textarea>Eneter your description</textarea></div>
I have a second file name "index.php" and the html inside it are
<html>
<head>
<title>SMO</title>
</head>
<body>
<?php include_once("login.php"); ?>
</body>
</html>
The problem i am facing is that, when php include login.php it also add a <title></title> tag hence i am left with two title tags.
Please help me sort it out. I know its a kid things. but just could not figure out how to solve this.
login.php
<html>
<head>
<title>SMO</title>
</head>
<body>
<div><textarea>Eneter your description</textarea></div>
</body>
</html>
index.php
<html>
<head>
<title>SMO</title>
</head>
<body>
<?php include_once("login.php"); ?>
</body>
</html>
Is this your code?
If yes, you don't have to add the <title> in your login.php page.Remove <title> and it'll work fine.
If no, please include your code in your question.
So your login.php page should be like
<div><textarea>Eneter your description</textarea></div>

newbie php issue include function

I have a very basic question I just can't seem to figure out or find the answer too. My php won't work on my local host (wamp) or when i upload it to my website/server.
I simply want to "include" two files on my index.php file to display additional html. For whatever reason, the php won't work and won't display anything outside of what is currently in my index.php file.
Here is my index.php
<php? include 'inc/header.php';?>
<div class="container">
<p>main content.what is going on</p>
</div>
<php? include 'inc/footer.php';?>
here is an example of my header.php file..
<!doctype html>
<html>
<head>
<title>stuff</title>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Is stuff here?</h1>
</header>
I just can't seem to figure it out and any thoughts would be VERY helpful.
You have PHP start tag wrong. It should be
<?php

Including a CSS file in a PHP file that is included in a PHP file

I have a php file that doesn't (for now) use any php code, that holds code for the header and main menu that will be used across all pages. The CSS file has no effect, even though I've created a style class for h1. The text "TEST" shows up, but the style is not applied. How do I properly include the CSS file?
mainMenu.php
<!--This code is included within the <head> of each page, and the stylesheet for the header and main menu are included here-->
<link href="styles/headerMenu.css" ref="stylesheet" type="text/css">
<!--Header and menu code-->
<div>
<h1>TEST</h1>
</div>
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>untitled</title>
<?php include ('./includes/mainMenu.php') ?>
</head>
<body>
</body>
</html>
The CSS file is not found. Double check the link and correct it:
<link href="menu.css" rel="stylesheet" type="text/css">
^- REL not REF
Also to prevent additional problems, remove the start and end tags of <head> and <body> from the code. The way you output the HTML elements, you would create wrong HTML if you keep those tags. Remove them and your page will be valid HTML again.
index.php:
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<title>untitled</title>
<?php include ('menu.php') ?>
</html>
Valid HTML has the benefit that you can run it through a validator to spot errors early.
I think it may be because you have got your menu appearing inside your <head> tag.
The CSS needs to go inbetween the <head> and </head> but the rest needs to be inside the <body> tag
<link href="styles/headerMenu.css" rel="stylesheet" type="text/css">
This must be at <HEAD></HEAD>
<div>
<h1>TEST</h1>
</div>
This must be at <BODY></BODY>
You have to separate this file into 2 files and include them in Head and in Body..
Don't include your HTML code in HEAD part. Only include CSS and JavaScript files in HEAD section. and you need to prefix the css or images path in some php file.
E.g.
create new php file with name "conn_path.php"
<?php
define('SITE_PATH',$_SERVER['DOCUMENT_ROOT'].'siteName/');
define('SITE_HTTP_PATH','http://localhost/'siteName/');
define('CSS_PATH','http://localhost/siteName/styles/');
define('IMAGE_PATH','http://localhost/siteName/images/');
?>
And then you path will be like below:-
mainMenu.php
<?php include "conn_path.php" ?>
<link href="<?php echo CSS_PATH ;?>headerMenu.css" rel="stylesheet" type="text/css">
It will help you in whole project…
Create a template file, with your essential (and re-used) html. Also with <html>, <head> and <body> tags and anything you must have in all pages – As your stylesheets and menu.
Then add a content section with a single variable.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>untitled</title>
<link href="styles/headerMenu.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--Header and menu code-->
<div>
<h1>TEST</h1>
</div>
<?php echo $page_content; ?>
</body>
</html>
This way, any page content shoud be assigned to $page_content instead of echoed.

How to import/include a CSS file using PHP code and not HTML code?

I have googled a lot but it seems that I am doing something wrong.
I want to do this:
<?php
include 'header.php';
include'CSS/main.css';
...
?>
However, my page prints the CSS code.
Note: I want to use PHP to include the CSS file, and not use
I also do you want to rename my CSS file to a PHP file as some website mentioned.
Any clues?
Many thanks.
You have to surround the CSS with a <style> tag:
<?php include 'header.php'; ?>
<style>
<?php include 'CSS/main.css'; ?>
</style>
...
PHP include works fine with .css ending too. In this way you can even use PHP in your CSS file. That can be really helpful to organize e.g. colors as variables.
You are including the CSS code as text in your PHP page. Why not just link it in the traditional fashion?
<link rel="stylesheet" href="CSS/main.css" type="text/css">
you can use:
<?php
$css = file_get_contents('CSS/main.css');
echo $css;
?>
and assuming that css file doesn't have it already, wrap the above in:
<style type="text/css">
...
</style>
To use "include" to include CSS, you have to tell PHP you're using CSS code. Add this to your header of your CSS file and make it main.php (or styles.css, or whatever):
header("Content-type: text/css; charset: UTF-8");
This might help with some user's connections, but it theoretically (read: I haven't tested it) adds processor overhead to your server and according to Steve Souder, because your computer can download multiple files at once, using include could be slower. If you have your CSS split into a dozen files, maybe it would be faster?
Steve's blog post: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/
Source: http://css-tricks.com/css-variables-with-php/
<?php
define('CSSPATH', 'template/css/'); //define css path
$cssItem = 'style.css'; //css item to display
?>
<html>
<head>
<title>Including css</title>
<link rel="stylesheet" href="<?php echo (CSSPATH . "$cssItem"); ?>" type="text/css">
</head>
<body>
...
...
</body>
</html>
YOUR CSS ITEM IS INCLUDED
This is an older post, however as the info is still relevant today an additional option may help others.
Define a constant for the file path per Stefan's answer. The
definition can be placed at the top of the PHP page itself, or within
an included/required external file such as config.php.
(http://php.net/manual/en/function.include.php)
Echo the constant in PHP tags, then add the filename directly after.
That's it!
Works for other linked files such as JavaScript as well.
<?php
define('CSS_PATH', 'template/css/'); //define CSS path
define('JS_PATH', 'template/js/'); //define JavaScript path
?>
<!-- Doctype should be declared, even in PHP file -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH; ?>main.css">
<script type="text/javascript" src="<?php echo JS_PATH; ?>main.js"></script>
</head>
<body>
</body>
</html>
If you want to import a CSS file like that, just give the file itself a .php extension and import it anyway. It will work just fine :)
You can also do the following:
Create a php file in includes folder, name it bootstrap_css.php for example
paste the css code files to file created above
<?php
$minCss=' <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$business = '<link href="bootstrap/css/modern-business.css" rel="stylesheet">';
echo $minCss;
echo $business;
?>
in the html header, include the css files as follows
<?php include_once 'includes/bootstrap_css.php'; ?>
You could do this
<?php include("Includes/styles.inc"); ?>
And then in this include file, have a link to the your css file(s).
I don't know why you would need this but to do this, you could edit your css file:-
<style type="text/css">
body{
...;
...;
}
</style>
You have just added here and saved it as main.php. You can continue with main.css but it is better as .php since it does not remain a css file after you do that edit
Then edit your HTML file like this. NOTE: Make the include statement inside the tag
<html>
<head>
<title>Sample</title>
<?php inculde('css/main.css');>
</head>
<body>
...
...
</body>
</html>
I solved a similar problem by enveloping all css instructions in a php echo and then saving it as a php file (ofcourse starting and ending the file with the php tags), and then included the php file.
This was a necessity as a redirect followed (header ("somefilename.php")) and no html code is allowed before a redirect.
Just put
echo "<link rel='stylesheet' type='text/css' href='CSS/main.css'>";
inside the php code, then your style is incuded. Worked for me, I tried.
This is the format of what I have which works:
<head>
<title>Site Title</title>
<?php include 'header.php'; ?>
</head>
Inside my header.php I have:
<!doctype html>
<html class="no-js" lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/png" href="assets/images/icon/favicon.ico">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
The file name must be something other than a .CSS index. Write the following:
<link rel="stylesheet" href="style.css" type="text/css" />
The best way to do it is:
Step 1:
Rename your main.css to main.php
Step 2: in your main.php add
<style> ... </style>
Step 3: include it as usual
<?php include 'main.php'; ?>
That is how i did it, and it works smoothly..
_trace its directory, I guess
echo css('lib/datatables_rqs/jquery.dataTables.css');

Categories