Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > All Around Tutorials > Serverside Scripting Tutorials
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 01-15-2007, 03:58 PM
  #1
johnz
charlie dont surf!
 
johnz's Avatar
 
Join Date: May 2006
Location: Long Island, New York
Posts: 1,239
iTrader: (0)
johnz will become famous soon enough
php includes

To use PHP Includes, the first thing you will have to do is rename all your .html files to .php.

After you've done that, the concept is pretty simple. Lets say you wanted to include navigation to all your pages. You would code your pages just like you would normally. When you reach the part in your page where your navigation would go, you use the include.

PHP Code:
<?php include("yournavfile.html"); ?>
so lets say you had a page like this:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>
</head>
<body>

<div id="wrap">
	
	<div id="top"></div>
	
	<div id="main">
		
		<ul id="nav">
			<li><a href="page1.php">Link1</a></li>
			<li><a href="page2.php">Link2</a></li>
			<li><a href="page3.php">Link3</a></li>
			<li><a href="page4.php">Link4</a></li>
			<li><a href="page5.php">Link5</a></li>

		</ul>
		
	<div id="footer"></div>

</div>
</body>
</html>
Instead of inserting your navigation into every one of your pages, you can just use the include from above.

So now your page should look like this:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title></title>
</head>
<body>

<div id="wrap">
	
	<div id="top"></div>
	
	<div id="main">
		
		<ul id="nav">
			<?php include("navigation.html"); ?>
		</ul>
		
	<div id="footer"></div>

</div>
</body>
</html
The file that you are including (navigation.html) will contain the code that you want included:
HTML Code:
<li><a href="page1.php">Link1</a></li>
<li><a href="page2.php">Link2</a></li>
<li><a href="page3.php">Link3</a></li>
<li><a href="page4.php">Link4</a></li>
<li><a href="page5.php">Link5</a></li>
__________________
work. collegehumor.com
blog. johnzanussi.com


If someone has helped you be sure to add to their reputation
johnz is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-17-2007, 12:59 AM
  #2
Pegasus
Mama Hen
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 22,386
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
Okay, I'm a bit thick on this. The pages that have includes in them are to be given the .php extension and the includes themselves are to be .html pages? Right?

Now, what happens if I have the include inside a <div> and that <div> has special CSS formatting? Will the .php page read the CSS properly? If not, how do I get the linked CSS information to the appropriate include?
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is online now   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-17-2007, 10:10 AM
  #3
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
Peg, well actually the file you include can be whatever extension you please.

Hmm. Let's assume this is our div in index.php.
HTML Code:
<span style="color:#990000">
<?php include('warning.inc'); />
</div>
.inc is just a popular extension for php includes when including text and the like. So now let's assume warning.inc looks like this....
HTML Code:
Abandon Ship!!
When someone on the Internet requests your index page PHP fires up and outputs this to your server:
HTML Code:
<span style="color:#990000">
Abandon Ship!!
</div>
Then your server outputs that to the person's browser. After the browser has it then it reads the CSS, it reads the text, and you now have something like Abandon Ship!!

PHP plays no part on how the browser reads the page. This is because, as you can see, the browser never gets to see any PHP. You would get this same effect if you just statically wrote:
HTML Code:
<span style="color:#990000">
Abandon Ship!!
</div>
on an .html page.
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-28-2007, 02:02 PM
  #4
Pegasus
Mama Hen
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 22,386
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
Another question, I think. Some of my pages have anchor links to other pages. The link looks like this: <a href="page.php#section">Page Section</a>

The PHP page has an include for the content. It reads: <?php include("content.html"); ?>

By your logic, the anchor link should arrive at the correct anchor without a hitch, right?

Peg
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is online now   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 04-02-2007, 09:26 PM
  #5
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
So long as the id "section" exists on the page, yes
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 04-02-2007, 11:52 PM
  #6
Pegasus
Mama Hen
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 22,386
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
There's an <a name="section"> (or whatever the section name happens to be) on each place, yes.

Okay, I think I've got it figured out. Thanks.

Peg
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is online now   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 07-29-2008, 03:47 PM
  #7
JoeyD
Paladin (Level 15)
 
JoeyD's Avatar
 
Join Date: Jun 2005
Posts: 303
iTrader: (1)
JoeyD will become famous soon enough
Uh... yeah... only i just discovered if you're using dynamic includes based on user input, e.g.,
PHP Code:
<?php  
include($_GET['page']);  
?>
... it's a potential disaster.

Sample exploit:
h ttp://yoursite.com/index.php?content=../../../../../insecure_php_scripts/shell_access.php

The place i found that gave me a simple solution as so:
PHP Code:
if ( substr$_GET['content'] , ) != '.' // nothing starting with a period is allowed, preventing backing out of a directory.
include( 'includes/' $_GET['content'] ); 
...and there's more on the subject here.

/JoeyD goes off to fix a bunch of web pages...
JoeyD is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 10-10-2008, 09:34 PM
  #8
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,859
iTrader: (0)
erisco will become famous soon enougherisco will become famous soon enough
JoeyD, that is not a safe way to protect against this. I could easily start with a forward slash or easily enter a parent directory and return back to the child. The way to protect against this is to make sure the path begins with an expected root path.
erisco is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 01-07-2009, 08:34 AM
  #9
AkkeDaBest
Veteran (Level 7)
 
AkkeDaBest's Avatar
 
Join Date: Nov 2008
Location: Finland
Posts: 67
iTrader: (0)
AkkeDaBest is an unknown quantity at this point
That is EXACTLY what i use SSI for!
__________________
Visit AkkeOnline.com - Free money!
AkkeDaBest is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 08-29-2009, 07:05 PM
  #10
c-a-a
Novice (Level 1)
 
c-a-a's Avatar
 
Join Date: Aug 2009
Location: Long Island, NY
Posts: 5
iTrader: (0)
c-a-a is an unknown quantity at this point
Quote:
Originally Posted by johnz View Post
PHP Code:
<?php include("yournavfile.html"); ?>
i always use a doc root
PHP Code:
<? include ($DOCUMENT_ROOT '/your/nav/file.htm');?>
which is better? does it matter?
c-a-a is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-21-2009, 12:04 AM
  #11
Pegasus
Mama Hen
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 22,386
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
Okay, I have another silly question. This include works:

<?php include("header.php"); ?>

This does not:

<?php include("../header.php"); ?>

Why and what's my work around for it?
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is online now   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-24-2009, 02:43 PM
  #12
JoeyD
Paladin (Level 15)
 
JoeyD's Avatar
 
Join Date: Jun 2005
Posts: 303
iTrader: (1)
JoeyD will become famous soon enough
That is a valid path and should work, if the include is one directory up from the script calling it. Do you get a PHP error, complaining about not finding the file?

¥åßßå once suggested the following, i think as a means to help ensure that wherever the script goes, it can find its files...
PHP Code:
<?php
include dirname(__FILE__).'/../header.php';
?>
...the dirname(__FILE__) bit appends the full server path to the file, e.g. /home/myweb/public_html/test/../header.php, which i think means "descend into the /test directory and then look for header.php in its parent directory". I think.

I have no guess as to whether it's a secure thing to do or not, but telling someone to do the wrong thing is the fastest way to find out.

- joey
JoeyD is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-27-2009, 12:36 AM
  #13
blackpepper
Gampi
 
blackpepper's Avatar
 
Join Date: Jul 2005
Location: metro - nyc
Posts: 1,280
iTrader: (0)
blackpepper will become famous soon enough
peg,
that syntax is correct. Are you certain that the file your referencing exists in the parent directory
__________________
blackpepper is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 01-07-2010, 10:41 PM
  #14
Vincent Puglia
Paladin (Level 15)
 
Vincent Puglia's Avatar
 
Join Date: Jun 2000
Location: where the world once stood
Posts: 411
iTrader: (0)
Vincent Puglia is on a distinguished road
According to the online manual on php.net
Quote:
If a path is defined (full or relative), the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
the link to the above
[ http://www.php.net/manual/en/function.include.php ]

the link to the 'include path'
http://www.php.net/manual/en/ini.cor...i.include-path
__________________
GrassBlade
Where the world once stood the blades of grass cut me still
Vincent Puglia is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 01-23-2010, 11:33 PM
  #15
cascading-style
Paladin (Level 15)
 
cascading-style's Avatar
 
Join Date: Dec 2009
Posts: 483
iTrader: (0)
cascading-style will become famous soon enough
If you're asking why the ../ is necessary, Pegasus, it's because the header.php file is above the current file directory-wise. For example:

If you have header.php in your root directory, but your current file, say header.html is in a directory up (root-directory/this-directory/header.html) you would have to include a ../ when linking to header.php from the header.html file.

Confusing, right?
__________________
Build your own personal server complete with PHP and MySQL: http://www.htmlforums.com/client-sid...-126209.html#3

Learn the languages and show off your skills: http://www.headfirstlabs.com/
cascading-style is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart



 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 08:13 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.