FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Calling a php file from another on apache server
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Calling a php file from another on apache server [message #181077] Wed, 10 April 2013 19:49 Go to next message
Jordan Thompson is currently offline  Jordan Thompson
Messages: 4
Registered: April 2013
Karma: 0
Junior Member
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!
Re: Calling a php file from another on apache server [message #181078 is a reply to message #181077] Wed, 10 April 2013 20:11 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/10/2013 3:49 PM, 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!
>

First of all, do you have permission to issue the exec() function? Many
shared systems have it disabled for security reasons.

If you do have permission, is the php executable available to you?
Again, on a shared system, it may or may not be.

Finally, if it is available, the second script is going to be executed
in the cli environment, not the web. This means the session_start()
will fail, as there is no web server involved (well, it may not fail -
but it won't do anything). And its output goes to stdout, (which is fed
back to the second parameter of exec(), which you are not using, so it
is thrown away), not to the web.

IOW, what you're trying to do is definitely not common, and probably not
the right way to go about things.

What is it you're really trying to do?


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Calling a php file from another on apache server [message #181079 is a reply to message #181078] Wed, 10 April 2013 20:34 Go to previous messageGo to next message
Jordan Thompson is currently offline  Jordan Thompson
Messages: 4
Registered: April 2013
Karma: 0
Junior Member
Hi Jerry,
thanks for getting back...
> First of all, do you have permission to issue the exec() function? Many
*** Yes, because the "hello.txt" file that is being touched in the lower-level file is being created by that file.
>
> shared systems have it disabled for security reasons.
>
>
>
> If you do have permission, is the php executable available to you?
*** Yes - see above
>
> Again, on a shared system, it may or may not be.
>
>
>
> Finally, if it is available, the second script is going to be executed
>
> in the cli environment, not the web. This means the session_start()
>
> will fail, as there is no web server involved (well, it may not fail -
>
> but it won't do anything). And its output goes to stdout, (which is fed
>
> back to the second parameter of exec(), which you are not using, so it
>
> is thrown away), not to the web.
*** I am not getting any errors in either the apache log or the php log, but that would explain why nothing is displaying on the browser. How can I do this?
>
>
>
> IOW, what you're trying to do is definitely not common, and probably not
>
> the right way to go about things.
>
>
>
> What is it you're really trying to do?
Fair enough. I want to call a php file that will access a database and return the results of its query into the web page so it looks seamless to the user.

I tried doing this via an include. It worked, but I would have to expose the username/password in a file accessible to the web (under htdocs.)

I tried putting it in cgi-bin (and including it from there), but that did not work because apache correctly won't allow a client into cgi-bin (but calling it from cgi-bin seems to work, but nothing is displayed to the user.)

What is the correct way to hide the username/password from the user (preferably in a single file that is included in various php files) and display the results in html for a browser?
Re: Calling a php file from another on apache server [message #181080 is a reply to message #181079] Wed, 10 April 2013 21:26 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/10/2013 4:34 PM, Jordan Thompson wrote:
> Hi Jerry,
> thanks for getting back...
>> First of all, do you have permission to issue the exec() function? Many
> *** Yes, because the "hello.txt" file that is being touched in the lower-level file is being created by that file.
>>
>> shared systems have it disabled for security reasons.
>>
>>
>>
>> If you do have permission, is the php executable available to you?
> *** Yes - see above
>>
>> Again, on a shared system, it may or may not be.
>>
>>
>>
>> Finally, if it is available, the second script is going to be executed
>>
>> in the cli environment, not the web. This means the session_start()
>>
>> will fail, as there is no web server involved (well, it may not fail -
>>
>> but it won't do anything). And its output goes to stdout, (which is fed
>>
>> back to the second parameter of exec(), which you are not using, so it
>>
>> is thrown away), not to the web.
> *** I am not getting any errors in either the apache log or the php log, but that would explain why nothing is displaying on the browser. How can I do this?
>>
>>
>>
>> IOW, what you're trying to do is definitely not common, and probably not
>>
>> the right way to go about things.
>>
>>
>>
>> What is it you're really trying to do?
> Fair enough. I want to call a php file that will access a database and return the results of its query into the web page so it looks seamless to the user.
>
> I tried doing this via an include. It worked, but I would have to expose the username/password in a file accessible to the web (under htdocs.)
>
> I tried putting it in cgi-bin (and including it from there), but that did not work because apache correctly won't allow a client into cgi-bin (but calling it from cgi-bin seems to work, but nothing is displayed to the user.)
>
> What is the correct way to hide the username/password from the user (preferably in a single file that is included in various php files) and display the results in html for a browser?
>

Normally, you put the username/password in a configuration file outside
of the web server's document root. Many shared hosting systems allow
you access to one directory below the document root where you can place
other files.

Files outside of the document root are not available via the web, but
are still available to PHP.

You *can* place them in the inside the document root hierarchy, and
include them in PHP. Place the userid and password in PHP code (with a
..php extension - just like any other php file) and the userid/password
will not normally be available to outside users. You can even protect
it from being downloaded with a .htaccess file (on Apache).

The only potential problem here is - I said the userid/password are not
*normally* available. It would take a web server misconfiguration or
other similar error to deliver the php file's source code to the user.
It is a very rare occurrence, but it *could* happen. Keeping the file
out of the document root means the userid/password won't be available to
anyone, even if there is a server misconfiguration.

In either case, then just include() (or, more preferably, require_once()
the file in your other PHP code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Calling a php file from another on apache server [message #181081 is a reply to message #181079] Wed, 10 April 2013 22:43 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 10/04/13 21:34, Jordan Thompson wrote:
> Hi Jerry,
> thanks for getting back...
>> First of all, do you have permission to issue the exec() function? Many
> *** Yes, because the "hello.txt" file that is being touched in the lower-level file is being created by that file.
>> shared systems have it disabled for security reasons.
>>
>>
>>
>> If you do have permission, is the php executable available to you?
> *** Yes - see above
>> Again, on a shared system, it may or may not be.
>>
>>
>>
>> Finally, if it is available, the second script is going to be executed
>>
>> in the cli environment, not the web. This means the session_start()
>>
>> will fail, as there is no web server involved (well, it may not fail -
>>
>> but it won't do anything). And its output goes to stdout, (which is fed
>>
>> back to the second parameter of exec(), which you are not using, so it
>>
>> is thrown away), not to the web.
> *** I am not getting any errors in either the apache log or the php log, but that would explain why nothing is displaying on the browser. How can I do this?
>>
>>
>> IOW, what you're trying to do is definitely not common, and probably not
>>
>> the right way to go about things.
>>
>>
>>
>> What is it you're really trying to do?
> Fair enough. I want to call a php file that will access a database and return the results of its query into the web page so it looks seamless to the user.
>
> I tried doing this via an include. It worked, but I would have to expose the username/password in a file accessible to the web (under htdocs.)
That is how you do it. But the file can reside anywhere on the server.

> I tried putting it in cgi-bin (and including it from there), but that did not work because apache correctly won't allow a client into cgi-bin (but calling it from cgi-bin seems to work, but nothing is displayed to the user.)

?? me not understand this part
> What is the correct way to hide the username/password from the user (preferably in a single file that is included in various php files) and display the results in html for a browser?

php files on a correctly configured system cannot be read as source
anyway. They can at best be interpreted by the php interpreter.

If all you have is a ftp accessible server on someone elses machine and
you are really paranoid., create a directory off the web servers root
called 'private,. and block WEB access to it with a .htaccess file that
disallows browsing in it and include files that reside inside it.
i.,e
/index.php - your main file which will include('private/database.php');
/private/.htaccess - your access file that disallows browsing or direct
access inside it
/private/database.php - your saucerful of secrets :-)

--
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.
Re: Calling a php file from another on apache server [message #181087 is a reply to message #181077] Thu, 11 April 2013 16:27 Go to previous messageGo to next message
Jordan Thompson is currently offline  Jordan Thompson
Messages: 4
Registered: April 2013
Karma: 0
Junior Member
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?

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!
Re: Calling a php file from another on apache server [message #181088 is a reply to message #181087] Thu, 11 April 2013 16:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/11/2013 12:27 PM, 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?
>
> 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!

Are you sure you don't have more than one db.php on your system?
hello.php is finding a script (or the require_once() would have failed),
but it may or may not be the right one.

I always recommend any include/require statements use paths relative to
$_SERVER['DOCUMENT_ROOT']. That way if files the including file gets
moved to another directory it still gets the correct file.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Calling a php file from another on apache server [message #181089 is a reply to message #181087] Thu, 11 April 2013 16:47 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <d2ddcea1-845c-4035-b46b-41c2a4b748e4(at)googlegroups(dot)com>,
Jordan Thompson <jorythompson(at)gmail(dot)com> wrote:

> 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

Obviously $username is undefined at the point you use it in hello.php.
From which I'd deduce that your require_once('db.php') is wrong,
probably because the path to db.php is not properly specified.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: Calling a php file from another on apache server [message #181090 is a reply to message #181087] Thu, 11 April 2013 16:58 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
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.
Re: Calling a php file from another on apache server [message #181091 is a reply to message #181077] Thu, 11 April 2013 19:20 Go to previous messageGo to next message
Jordan Thompson is currently offline  Jordan Thompson
Messages: 4
Registered: April 2013
Karma: 0
Junior Member
Excellent!! I did this:
require($_SERVER['DOCUMENT_ROOT'].'/../db.php');
and everything is behaving correctly.

You are right: there were (several!) db.php files laying around (it wasn't me!)

thanks so much for everyone's help.


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!
Re: Calling a php file from another on apache server [message #181095 is a reply to message #181087] Thu, 11 April 2013 23:42 Go to previous message
David Robley is currently offline  David Robley
Messages: 23
Registered: March 2013
Karma: 0
Junior Member
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?

Include/require effectively put the contents of the requested file(s) in the
program flow; so here is roughly what is happening based on the code you
show above:

index.php:
<!DOCTYPE HTML>
<html>
Hello World<br>
>
<?php
session_start();
//include('../../cgi-bin2/hello.php');

//require_once('db.php');
$username="user";
$password="pass";
$database="test";
$host="localhost";
echo "hello from ".$username." db.php<br>";

echo "hello from ".$username." bottom<br>";
$username="Alex";

echo "hello " . $username . " from top<br>";
?>
from html
</html>


--
Cheers
David Robley

Plagiarism is the sincerest form of flattery.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Error while Generating SiteMap
Next Topic: Calling child class from parent class
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 16:02:52 GMT 2024

Total time taken to generate the page: 0.02274 seconds