Calling PHP-Script from a PHP-Script??

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
User avatar
derschamane
Forum Newbie
Posts: 6
Joined: Fri Dec 27, 2002 1:44 pm
Location: Austria
Contact:

Calling PHP-Script from a PHP-Script??

Post by derschamane »

Seems to be a simple problem, but I was not able to found any hints:

I would like to call a PHP-Script from a PHP-Script

Example:

phpscript1.php

If ($check1 > 0)
{
phpscript2.php?ErrorNr=$check1
}

If $check1 is greater than 0, the Second Script should be called with the parameter as shown above.

Has anybody of you an idea, how to program this?
Any suggestions and hints are very welcome

Regards from Austria
Ewald, the modern shaman
bjg
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2003 9:42 am
Location: .au

Post by bjg »

Probably the best (and only?) way to do this, is executing it via command. I'm not %100 sure though if you can pass variables via query string. Might want to Google that if it doesn't work.

Code: Select all

if ($check1 > 0)
{
    $command = '/path/to/php -f /path/to/phpscript2.php?ErrorNr='.$check1;
    exec($command);
}
If you want the output of phpscript2.php returned, look into using system() instead.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

or use a header("Location: script2.php?arguments....");
bjg
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2003 9:42 am
Location: .au

Post by bjg »

That would redirect to a different page..

And what if he's writing a shell script? header()'s no good if the protocol isn't HTTP.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Well, yes its a different page, but from the details given that might be enough. You can't tell if we need to spawn a new process, if we need the results from the new process, etc. While I use PHP for shell scripts sometimes, most people don't. Many people don't even have a cli-php compiled/availible.

Header is also safer as you don't need to cleanse all the dangerous shell arguments, etc. But this is only an option if the user wants to pass full control to the new page.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

You could also...

Post by trollll »

You could try the following, but it would probably only work for the web.

Code: Select all

$fp = fsockopen("127.0.0.1", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
&#125; else &#123;
    fwrite($fp, "GET phpscript2.php?ErrorNr=$check1 HTTP/1.0\r\n");
    fwrite($fp, "Host: 127.0.0.1\r\n\r\n");
    while(!feof($fp)) &#123;
        $result .= fread($fp, 512);
    &#125;
    fclose($fp);
&#125;
If you set up phpscript2.php to just write the output out without formatting (or in XML or something else useful) you could use the result in phpscript1.php.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

Why not just include the other script?
User avatar
redcircle
Forum Commoner
Posts: 43
Joined: Fri Jan 31, 2003 8:47 pm
Location: michigan, usa

Post by redcircle »

hedge wrote:Why not just include the other script?
KISS

The simplest answers are usually the hardest to figure out.
User avatar
derschamane
Forum Newbie
Posts: 6
Joined: Fri Dec 27, 2002 1:44 pm
Location: Austria
Contact:

Finally a real nice solution...

Post by derschamane »

Finally I found a real nice and simple solution - thanx for every answer, I appreciate your help and at least your ideas gave me the final hint.


if ($check1)
{
echo "<script type=\"text/javascript\">";
echo "document.location.href = \"news_fehlerausgeben.php?ErrorNr=" . $check1 . "\"";
echo "</script>";
}

Conclusion: Sometimes, I've to look over the fence -> JavaScript instead of PHP ;-)

Ewald, the modern Shaman
http://www.schamane.cc[syntax=php]<?php

?>[/syntax]
Post Reply