Why Doesnt PHP Parse My Scripts? - php

I'm trying to get the php executable to parse scripts, but it's not working. i run something like this:
php c:\test.php
and test.php contains this:
<?
echo 'hello!';
?>
and that is exactly what running the command returns. It returns the raw code. How can I get it to actually parse the code and return "hello!" instead?

Make sure you have the short_open_tag setting enabled in your php.ini. Failing that, use the ever-so-slightly longer <?php to start your script.

Another option might be to try using the -f command line option.
php -f c:\test.php

It's been a while since I've ran php off the command line, but try this:
<?php
echo 'hello!';
?>

Also make sure that your webserver is correctly configured. If it is not configured to run .php pages through the php interpreter it may simply display as a text file.

Related

I use php exec another php but this not working

i have this file:
pippo.php
exec(ExportExcel.php);
ExportExcel.php
#!/usr/local/bin/php
<?php
$stop=1;
......
php code
......
?>
The exec command runs the file but I don't know what it is doing. It remains hung up and I don't get to activate the breakpoint placed in the file.
Any idea?
Thnak's
You need to pass a string to exec(), so maybe try exec('./ExportExcel.php');
Reference: https://www.php.net/manual/en/function.exec.php

Issues with exec() on php running a shell script

I'm trying to use a php script for executing a shell script. However I'm having some issues apparently with the web server.
I have a bash script called switch_audio.sh. It basically changes the active audio output of the system.
I also have a script.php that runs the code below:
<?php
echo "It's working";
exec("/var/www/html/switch_audio.sh");
?>
When I execute php script.php it's working fine. However, when I try to run it on the web browser by localhost/script.php I just get as ouput the "echo" part.
I've already tried to:
remove 'exec()' from the disable functions in php.ini;
give permissions for everybody in every folder on this path from "/" to the localhost folder;
Any thoughts about it?
Simple solution: exec() returns a string, but you also have to output it to the user:
<?php
echo "It's working";
echo exec("/var/www/html/switch_audio.sh");
?>
You already said you allowed the shell function on your php.ini configuration but it seems it's not working yet... so maybe:
You didn't restarted webserver service after changes
You have somewhere other statement more restrictive... maybe ini_set in your php files.
As a reminder, be sure that you are doing well on your php.ini file, check it for this kind of statement:
disable_functions=exec,passthru,shell_exec
Maybe you can try instead of exec other similar php function like shell_exec to check if it works. Or maybe is working and not showing anything.

Find out what PHP is running on

I have a script which is running on Apache and also can be executed from command line.
How can i know, on what is php script running, apache or command line?
There is a constant build in PHP that you can use PHP_SAPI. If you are on the commandline the value of this constant is cli than you are on the command line. Every other value like cgi, cgi-fcgi, etc.
Why not add a parameter and only pass it when invoking from the command line?
http://php.net/manual/de/reserved.variables.argv.php
Create a page upload it and browse to it.
See page contents below.
<?php phpinfo(); ?>

How to execute external PHP script in command line from your browser

I have an external php script named external.php which includes this:
<?php
echo 'External Output';
?>
When I run that on command line with command:
php external.php
I get output as
External Output
But when the same script I execute from my browser's php file named index.php which has this code:
<?php
$exe=exec('php external.php',$out,$ret);
print_r($out);
?>
Then I get no output.
When I modify it as:
<?php
$exe=exec('php external.php 2>&1',$out,$ret);
print_r($out);
?>
Then I get this output:
php: /opt/lampp/lib/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by php)
My question is, how can I execute that "external.php" file in commandline from index.php and get output on my browser (i.e. on index.php) ? I have tried system() function too, it doesn't work as well.
Edit:
I cannot include the external.php in index.php because external.php can take lot of time to execute (more than 10 hours).
Edit#2
Solved it, giving the full path to PHP solved the problem.
Here is an example:
<?php
$exe=exec('full/path/to/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Thankyou Oleg and Scopey for giving me some hint help.
Specify the full path of external.php:
<?php
$exe=exec('php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try also specifying the full path to php on the command line and check that it works:
$/usr/bin/php "/full/path/to/external.php"
If that works, try it also in your php file:
<?php
$exe=exec('/usr/bin/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try running phpinfo() from the command line. Often installations of PHP use a different PHP.ini for CLI usage. Running phpinfo() will show you the location of the ini file in use. It appears as though this ini might not be configured properly for your installation (as a guess)

Problem with exec in PHP

I am running through a weird problem with exec's in PHP. When I load in the browser the script x.php, the browser hangs and when I run a ps I can see multiple threads being created. The only way to stop them is by restarting apache.
However, instead of running a php script, if I do something like system('ls'), it works fine. So it seems to be problem when a PHP script tries to run another script using exec/system/passthru (I've tried them all).
x.php is defined as following:
<?php
var_dump(system('php -f t.php'));
?>
t.php is defined as following:
<?php
echo 'Hello world';
?>
Take a look at first comment in exec() manual.

Categories