How do I use include...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MarkT78
Forum Newbie
Posts: 6
Joined: Sat Dec 16, 2006 4:16 pm

How do I use include...

Post by MarkT78 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi there.  I'm still step-by-step learning PHP.  I now have a grasp on how to use include to grab external HTML files and include them in my present page.  I would like to know how to use include to grab an external file (ie - a file that I have on a different free web server).  Is that possible?  Here's what I have right now:

Actual Code:

Code: Select all

<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
include ("PHP/docdeclare.php");
?>

<!-- -->
<head>
<title>TESTY PHP</title>
<?php  

include ("PHP/stylesheet.php");
?>
</head>

<body>
<?php  
include ("test.php"); 
include ("blackheader.php");
?>
</body>
</html>
Output Code:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<!-- -->
<head>
<title>TESTY PHP</title>
<link rel="stylesheet" href="http://marktsmith78.110mb.com/CSS/index.css" type="text/css" /></head>

<body>
<p>I'm A Happy Testing File</p>
</body>
</html>
I'd like to change the code referring to my stylesheet to refer to an absolute url like:

http://marktsmith78.ifastnet.com/PHP/stylesheet.php

Thanks for any help you can provide.

Mark


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

Do you want the remote page to return your stylesheet, or do you want it to return the html to your stylesheet?

Assuming the former, have the script print out the stylesheet with:

Code: Select all

header('Content-Type: text/css');
// Print out your stylesheet here
Your html:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<!-- -->
<head>
<title>TESTY PHP</title>
<link rel="stylesheet" href="http://marktsmith78.ifastnet.com/PHP/stylesheet.php" type="text/css" />
</head>
<body>
<p>I'm A Happy Testing File</p>
</body>
</html>
Last edited by mibocote on Tue Jan 02, 2007 12:00 pm, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

<link rel="stylesheet" href="http://marktsmith78.110mb.com/CSS/index.css" type="text/css" />
That line in itself IS a link to an external stylesheet...

Including PHP files from other servers is bad practice.

That being said, if you MUST do it for some reason look into file-get-contents() and eval() - but I would strongly encourage you to avoid this technique.
MarkT78
Forum Newbie
Posts: 6
Joined: Sat Dec 16, 2006 4:16 pm

Post by MarkT78 »

mibocote wrote:Do you want the remote page to return your stylesheet, or do you want it to return the html to your stylesheet?
I want it to return a reference to my stylesheet. To where in all the pages I create, I use the reference to this file and this file refers to whatever my current stylesheet is.
MarkT78
Forum Newbie
Posts: 6
Joined: Sat Dec 16, 2006 4:16 pm

Post by MarkT78 »

Kieran Huggins wrote:

Code: Select all

<link rel="stylesheet" href="http://marktsmith78.110mb.com/CSS/index.css" type="text/css" />
That line in itself IS a link to an external stylesheet...

Including PHP files from other servers is bad practice.

That being said, if you MUST do it for some reason look into file-get-contents() and eval() - but I would strongly encourage you to avoid this technique.
I guess I don't have to refer to it on a different server. I think my point is that I want the ability to do so. However, could you share with me why it's bad practice?

Mark

EDIT: Also, I know that line you quotes was a reference to a stylesheet. That's what comes up through the included file. I wanted to know how to reference an absolute external url with PHP's include. I will, however, look into the two commands you suggested. Thank you.
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

Kieran Huggins wrote:
That being said, if you MUST do it for some reason look into file-get-contents() and eval() - but I would strongly encourage you to avoid this technique.
Actually, you can use include on files from remote servers, provided the allow_url_fopen is set accordingly. Problem is, you will just be getting the output of the script (I assume you want code to be included, in which case simply rename the file to have a non *.php style fileneame (or other depending on server configuration)).
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

MarkT78 wrote: I guess I don't have to refer to it on a different server. I think my point is that I want the ability to do so. However, could you share with me why it's bad practice?
It is considered bad practice because anytime you access something you have to consider how secure it is. Do you know for sure that the DNS entry is pointing to the correct IP and not another server that might have a page injected that would cause harm to your script, or that that website has not been hacked?
MarkT78
Forum Newbie
Posts: 6
Joined: Sat Dec 16, 2006 4:16 pm

Post by MarkT78 »

mibocote wrote:
Kieran Huggins wrote: Actually, you can use include on files from remote servers, provided the allow_url_fopen is set accordingly.
And how is this done? Just typing in the url? If so, how do I check to see if allow_url_fopen is available on my own system (PHP5 with Apache 2.2)?

Mark
User avatar
mibocote
Forum Newbie
Posts: 18
Joined: Sun Aug 20, 2006 9:51 am
Contact:

Post by mibocote »

MarkT78 wrote: And how is this done? Just typing in the url? If so, how do I check to see if allow_url_fopen is available on my own system (PHP5 with Apache 2.2)?
The syntax:

Code: Select all

include 'http://example.com/includedscript.php'; // This will return to your script the output of  that script 
include 'http://example.com/includedscript.inc'; // On most default configurations this will add the code in like a local include would
I do recommend against this.

Edit:
allow_url_fopen is part of your PHP configuration (php.ini)
MarkT78
Forum Newbie
Posts: 6
Joined: Sat Dec 16, 2006 4:16 pm

Post by MarkT78 »

mibocote wrote:
MarkT78 wrote: I guess I don't have to refer to it on a different server. I think my point is that I want the ability to do so. However, could you share with me why it's bad practice?
It is considered bad practice because anytime you access something you have to consider how secure it is. Do you know for sure that the DNS entry is pointing to the correct IP and not another server that might have a page injected that would cause harm to your script, or that that website has not been hacked?
Excellent points to consider. Thank you.

Mark
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

mibocote wrote:
Kieran Huggins wrote:That being said, if you MUST do it for some reason look into file-get-contents() and eval() - but I would strongly encourage you to avoid this technique.
Actually, you can use include on files from remote servers, provided the allow_url_fopen is set accordingly. Problem is, you will just be getting the output of the script (I assume you want code to be included, in which case simply rename the file to have a non *.php style fileneame (or other depending on server configuration)).
Doh! Forgot that point... nice catch mibocote! :)

To add to the security considerations:

If you don't own / exclusively manage both domains, you can only trust the code to the extent that you can personally trust whoever has access to it in addition to the security of the system, and the transmission.

If you do own / exclusively manage both domains, why not just copy the files once and save the traffic & load time overhead on each page?
Post Reply