Re: Calling a php file from another on apache server [message #181090 is a reply to message #181087] |
Thu, 11 April 2013 16:58 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 11/04/13 17:27, Jordan Thompson wrote:
> Thanks for helping with these apparently fundamental concepts (I do C++, C, Java, etc, this web-stuff is new to me.)
> I am able to get the first tier of include to work, but the second does not seem to make its variables available to the parent.:
>
> index.php:
> <!DOCTYPE HTML>
> <html>
> Hello World<br>
>
> <?php
> session_start();
> include('../../cgi-bin2/hello.php');
> echo "hello " . $username . " from top<br>";
> ?>
>
> from html
> </html>
>
> hello.php:
> <?php
> require_once('db.php');
> echo "hello from ".$username." bottom<br>";
> $username="Alex";
> ?>
>
> db.php:
> <?php
> $username="user";
> $password="pass";
> $database="test";
> $host="localhost";
> echo "hello from ".$username." db.php<br>";
> ?>
>
>
> Here is the output from accessing index.php:
> Hello World
>
> Notice: Undefined variable: username in C:\xampp\cgi-bin2\hello.php on line 3
> hello from bottom
> hello Alex from top
> from html
>
>
> NOW what am I doing wrong?
I think the path of db.php must be relative to the root..not to the
document from which it is called.
try
require_once('../../cgi-bin2/db.php');
(one reason I avoid nested includes)
That is, the PHP interpreter establishes its 'working directory' in
terms of the first place it is invoked. I,e, where index.php resides. It
doesnt change that when it includes a file, just because the file is in
a different directory.
As I said and will say again, avoid nested includes if you can, because
you will get differing results depending on where the the middle include
is included FROM.
Now I could be wrong in all this, but there are some PHP developers who
can confirm this behaviour..
More detail here
http://stackoverflow.com/questions/2860143/php-nested-include-behavior
>
> On Wednesday, April 10, 2013 3:49:22 PM UTC-4, Jordan Thompson wrote:
>> Hi there. I am new to php. I want a top-level php file to call another that will generate html. When I access the top-level file, I only get:
>>
>> "hello from top"
>>
>>
>>
>> I checked the logs and there are no errors. "Hello.txt" is generated in the htdocs folder, so I know that the lower-level file is executed correctly,
>>
>>
>>
>> Here is the top-level php file ("top.php") that is located in the htdocs folder:
>>
>> <?php
>>
>> session_start();
>>
>> echo '<!DOCTYPE HTML>';
>>
>> echo '<html>';
>>
>> echo 'hello from top';
>>
>> exec("php hello.php");
>>
>> echo '</html>';
>>
>> ?>
>>
>>
>>
>> Here is the lower-level php file ("hello.php") that is also located in the htdocs folder:
>>
>> <?php
>>
>> session_start();
>>
>> echo "hello from bottom";
>>
>> phpinfo();
>>
>> touch("hello.txt");
>>
>> ?>
>>
>>
>>
>>
>>
>> This should be really easy, but I am very confused.
>>
>>
>>
>> thanks in advance!
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.
|
|
|