Forum Moderators: coopster

Message Too Old, No Replies

Including PHP Script

         

SuperJ

3:27 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



How can I include a php script within another....

I know that you would normally use;

include("script.php");

but my question is slightly different....

what i have is a template system that uses an html template - which uses the search and replace function to replace "<% tags %>" with the "$content" - where $content varies from page to page, and is included on the appropriate page. What I am wanting to do is include script.php into the $content variable - but not sure how to.

I have tried searching around, and came up with some javascript equivalent <script language="javascript" src="script.php"></script> - but couldnt get this to work.

Basically - script.php contains more variables, and php scripting, as well as html to go on the output - a refer to a friend script.

basically the script putting template + content together is like this - there are other variables in here - but i left just 1 example in there, content - which is where i want the content to appear.

<?php

$content = 'content html stuff goes here';

function template() {
global $page_content;
$filename = 'template.html';
if(!$fd = fopen($filename, "r")) {
$error = 1;
}
else {
$template = fread ($fd, filesize ($filename));
fclose ($fd);
$template = stripslashes($template);
$template = eregi_replace("<% page_content %>", "$content", $template);
echo "$template";
}
}
function home() {
global $page_content;
template("$data");
}

switch($action) {
default:
home();
break;
}

?>

And basically on the html page is just the tag <% page_content %> which the script replaces with the content of the variable $content.

If anyone could help - this would be great.

SuperJ

3:35 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



i have found that i could probably use;

$inc = "http://www.domain.tld/path/script.php";
$content = file_get_contents($inc);

but i get the error;

Fatal error: Call to undefined function: file_get_contents() in [Root path to the script] Line XX

SuperJ

10:11 am on Dec 5, 2004 (gmt 0)

10+ Year Member



anyone?

coopster

12:56 pm on Dec 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, SuperJ.

PHP has a function that allows you to Evaluate a string as PHP code [php.net], but I'm not sure what you are attempting to do is the best design. Be very careful you aren't getting yourself into an endless loop.

DaButcher

9:57 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



I think I know what you are after:

You are after a system that includes a file, based on a variable?

eg:
if $content == "home", you want it to include "home.php"?

If so, its truly very simple!

The only things you have to consider, is the possibilites of abusing the include, to include sensitive information.. (/etc/passwd, anyone?)

You can "control" the includes, either by searching the include variable, or you can define which pages are allowed to be included.

There are a million ways of doing this, you could either:
define an array, with pages..

$page['Home'] = "includes/home.php";

then you do:
include($page[$querystring_variable]); (here querystring would be "Home")

Or, you can define it as integers:
$page[0] = "includes/home.php";

then you do:
include($page[$querystring_variable]); (here querystring would be 0 (the integer 0))

Or, you could:
include($querystring_variable . ".php");

The last example here, would be limited in filetype, as well as you would need to check if it includes slashes, etc. to prevent abuse. You would also have to check that it is not trying to include it self, so you wont have an infinite loop of includes!

If you want more, I can provide an working example of the last one here, but I guess you get the general idea.

SuperJ

11:54 am on Dec 7, 2004 (gmt 0)

10+ Year Member



ok - i can get the file to be included - but it always appears at the top of the page, and not where i want it to appear exactly

SuperJ

11:55 am on Dec 7, 2004 (gmt 0)

10+ Year Member



unless it has something with me using div's

SuperJ

11:58 am on Dec 7, 2004 (gmt 0)

10+ Year Member



o - and it appears above the html, head, body tags etc etc

sorry for the amount of replies - havent discovered a reply button yet

DaButcher

12:18 pm on Dec 7, 2004 (gmt 0)

10+ Year Member




o - and it appears above the html, head, body tags etc etc

Well, why are you including it outside of your html then?

You can simply include it *anywhere* in the document!
eg. if you have a div for content:


<div class="content" id="content" title="content">
<!--#Content-->
<?php
// your code goes here
?>
</div>

ps. if you already did include it inside the content, check the included file for html errors.. (some tags may not be closed, etc.)

SuperJ

12:34 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



i'm not including it outside the html area

... as mentioned above, i am using a search and replace method for my html template page - and the php script finds some <% tags %> and replaces it with the nessisary $variable strings - i just want to replace one of this $variables with content from another php page, as explained above - using the find replace method at the very above

DaButcher

2:33 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



Why do you replace text, instead of just including the file?

ps. your problem might be the %, as % in co-operation with some characters, may mean something else..

eg. %20 = space (" ")
you could try to replace <!--#content--> with the $content.

but this is no include, by using your method, you would first read thru the entire file and then replace it.. or maybe you get the text from db?

if you get it from DB, you simply parse it as wanted.