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, 04: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, 01:59 AM
  #2
Pegasus
Extremely Flighty Admin
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 21,465
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, 11:10 AM
  #3
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,858
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, 03:02 PM
  #4
Pegasus
Extremely Flighty Admin
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 21,465
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, 10:26 PM
  #5
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,858
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-03-2007, 12:52 AM
  #6
Pegasus
Extremely Flighty Admin
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 21,465
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, 04:47 PM
  #7
JoeyD
Paladin (Level 15)
 
JoeyD's Avatar
 
Join Date: Jun 2005
Posts: 301
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, 10:34 PM
  #8
erisco
Catapulted
 
erisco's Avatar
 
Join Date: Dec 2005
Location: Within the division of zero
Posts: 5,858
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

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 07:44 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, 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.