Page 1 of 1
Calling PHP-Script from a PHP-Script??
Posted: Tue Jun 17, 2003 2:11 pm
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
Posted: Tue Jun 17, 2003 3:03 pm
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.
Posted: Tue Jun 17, 2003 3:13 pm
by nielsene
or use a header("Location: script2.php?arguments....");
Posted: Tue Jun 17, 2003 3:20 pm
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.
Posted: Tue Jun 17, 2003 3:34 pm
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.
You could also...
Posted: Tue Jun 17, 2003 3:54 pm
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";
} else {
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)) {
$result .= fread($fp, 512);
}
fclose($fp);
}
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.
Posted: Tue Jun 17, 2003 4:40 pm
by hedge
Why not just include the other script?
Posted: Tue Jun 17, 2003 5:19 pm
by redcircle
hedge wrote:Why not just include the other script?
KISS
The simplest answers are usually the hardest to figure out.
Finally a real nice solution...
Posted: Wed Jun 18, 2003 6:44 am
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]