Calling a php program from another php program

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

rvoyek
Forum Newbie
Posts: 5
Joined: Wed Jul 03, 2002 1:28 pm

Calling a php program from another php program

Post by rvoyek »

I'm new to php so this may be a simple question, but I can't seem to figure it out.

I am trying to minimize the number of pages on my web site so I came up with and index.php that has all my includes in it and I also pass to it what page I want included in the body. THis works fime for simple pages, but I have a few other programs that I want to execute from the index.php page. My problem is the other php programs need to execute from their installed directory not form the directory where index.php resides.

eg. code snippet from index.php:

<?
include( "$body" );
?>

now i pass index.php?body=kisgb/index.php this guestbook app won't run becuase it can't find it's files.

What do I need to make this work???
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

you need to use the local directory for the file you need to include, not the URI for the file. e.g.

Code: Select all

include 'kisgb/index.php'
if that is where the file is located (relative to index.php).
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: Calling a php program from another php program

Post by protokol »

rvoyek wrote: <?
include( "$body" );
?>

now i pass index.php?body=kisgb/index.php this guestbook app won't run becuase it can't find it's files.

What do I need to make this work???
I'm willing to bet that the problem lies in the include("$body");

Try this instead:

include_once "{$_GET['body']}";
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

oops, misread the Q...my bad, prot is right
rvoyek
Forum Newbie
Posts: 5
Joined: Wed Jul 03, 2002 1:28 pm

Tried it

Post by rvoyek »

I tried it and this is the message i am getting:
Warning: Failed opening '' for inclusion (include_path='') in /home/www/johnstonsoccer/test1.php on line 53

Line 53 is the $_GET line
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

could you post the line that is giving the error, or is it the line above?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

also post the url given which is processed
rvoyek
Forum Newbie
Posts: 5
Joined: Wed Jul 03, 2002 1:28 pm

some code

Post by rvoyek »

ok here is the line from test1.php taht is giving me the error message

include_once ( "contentfiles/top-border.inc" );
include_once "{$_GET['$body']}";
include_once ( "contentfiles/top-border.inc" );

Test1.php is the going to be the main page for the web site. To get to the other pages or application I pass an argument through test1.php
example: http://myweb.com/test1.php?$body=kisgb/index.php
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

do a

Code: Select all

print_r($_GET);
and show us what you get; the $ in the URI might be messing it up
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: some code

Post by protokol »

rvoyek wrote: include_once ( "contentfiles/top-border.inc" );

include_once "{$_GET['$body']}";
include_once ( "contentfiles/top-border.inc" );

example: http://myweb.com/test1.php?$body=kisgb/index.php
$_GET['$body'] is incorrect. The way I typed it was not a typo. The correct statement is:
include_once "{$_GET['body']}";

Also, don't pass the body var with $body. Use this:
http://myweb.com/test1.php?body=kisgb/index.php
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You really don't need all those parenthesis and quotes around include statements:

Code: Select all

include_once 'contentfiles/top-border.inc'; 
include_once $_GET&#1111;'body']; 
include_once 'contentfiles/top-border.inc';
Should work fine.

Also you should be sure that the way you are passing the name of the file to include isn't open to abuse, surely giving the full path and filename means that someone could pretty much choose any file to display they want, including one from their own file system with whatever code they want on it...

Mac
Last edited by twigletmac on Fri Jul 05, 2002 1:36 am, edited 2 times in total.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

it's probably just because he was doing ?$body ... and if it still doesn't work, he might try $HTTP_GET_VARS incase he is running an old version
rvoyek
Forum Newbie
Posts: 5
Joined: Wed Jul 03, 2002 1:28 pm

Further explanation

Post by rvoyek »

I need to give a little more info I think. It looks to me like the application I'm trying to execute is not executing in it's home directory. It llok slike the execution is taking palce where test1.php is located.

Here is the code that's failing and the output form it.

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<body>
<?
print_r($_GET);
include_once $_GET['body'];
?>

</BODY>
</HTML>


Here is the line I'm excuting:
http://myweb/test2.php?body=kisgb/index.php


Array ( [body] => kisgb/index.php )
Fatal error: Failed opening required 'config.php' (include_path='') in /home/www/myweb/kisgb/index.php on line 12

kisgb is located just of the root and test1.php is in the root.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It looks like the execution is taking palce where test1.php is located
That's how include() works, all paths have to be relative to the first file (test1.php) not to the included file (kisgb/index.php).

Mac
rvoyek
Forum Newbie
Posts: 5
Joined: Wed Jul 03, 2002 1:28 pm

Is there a better Way????

Post by rvoyek »

Mac,

Is there a way to do what I'm trying to accomplish??

RV
Post Reply