Run a php shell from an php exec on a site - php

I have a problem when i try to run this exec statement. The exec wont start the php shell file as i want it too.
I've tried using shell_exec but that wont work either. And when i try to echo out the returning array and var i get the var to "1" and an empty array.
Here is my code I'm trying to execute:
<?php
$user = $_GET['u'];
session_start();
if ($_SESSION['user'] === $user) {
$sID = $_GET['si'];
exec('php C:\\xampp\\htdocs\\r\\pages\\create-csgo-server3.php');
echo 'Server is being created.<br>';
} else {
echo 'Permission denied.';
}
?>
It says "Server is being created." but it wont run the exec correctly.
php C:\xampp\htdocs\r\pages\create-csgo-server3.php
I tried putting this over into the cmd and it worked correctly.
Thank you for taking your time!

I'm sure there is a duplicate, but while I'm searching; the webserver user (user that runs Apache or IIS, etc.) doesn't have the path to php.exe in its path environment variable, but you must as it works for your user in a terminal. Always use full paths:
exec('C:/path/to/php C:/xampp/htdocs/r/pages/create-csgo-server3.php');

Related

PHP misses system variables when using exec

So i have a project where I send spotify track URL to server and store it. I use laragon with PHP 7.4 installed, windows 11. I use that downloader. It works via cmd command line. When I try to use it by hand it works fine, but when i send it by exec() it doesn't work. I can't get any return message neither. I have tried using exec(), system() and shell_exec() functions. The problem might be with with system variables, as these might not be visible for PHP. I have also tried turning on/off server and it didn't work. I have also tried to put putenv('PATH=' . $_SERVER['PATH']) at the beginning of file. I tried to check laragon path variables itself - I couldn't see these i have added. Any other default windows commands works as should be. Any ideas on how to fix it?
Here is my PHP code:
function createFile($url, $token){
function execCommand($dir, $url){
$command1 = "cd $dir";
$command2 = "spotdl $url";
if(!shell_exec($command1. "&& ". $command2)) return false;
return true;
}
$path = "C:\Laragon/laragon/www/temp/";
$dir = $path.$token.'/';
if(!mkdir($dir, 0777)) throwError("Server error");
if(!execCommand($dir, $url)) return false;
return true;
}
I know i'm not returning any output from console, but that is post updates. Second command is definitely beeing called, i have tested it on some other commands (like mkdir)
In many os the errors output (stderr) of the commands isn't the same that the normal output (stdout), you need to redirect the errors to the stdout. so the command must be:
$ command $arg1 $arg2 ... 2>&1
This cause that the errors messages will be sent to the stdout, Is util remember this when you are working with system calls.
Now in your code i prefer to use the exec function in php
<?php
$result = 0;
$output = [];
$command = "your command with the 2>&1";
exec($command, $output, $result);
if ($result != 0) {
// here an error ocurred and you can see the error on the ouput array
exit();
}
// here you know that the command was executed successfully
After re-installing laragon it worked. I Have checked before uninstalling path variables and i haven't seen mine that should be in.
I couldn't modify it either so I just reinstalled it.

PHP file not gettinig executed as expected

I created a php file as shown below.
<?php
$ar = 4600;
$az = "redshift -O ".$ar;
echo "Command executed: ";
echo $az;
system($az,$status); // It is not working on browser but working on terminal.
system("ls",$asd); // It is working on both browser and terminal.
if($status == NULL) {
echo "<h3>Operation not successful</h3>";
}
else
echo "<h3>Temperature Succesfully set to ".$ar."</h3>";
?>
Now, the matter is that
when i am running this file on terminal using command
php file.php
the 'ls' command is getting executed and 'redshift -O 4600' also getting executed.
But when i am executing this file on browser using url.
127.0.0.1/file.php
Only 'ls' command is getting executed. 'redshift -O 4600' is not getting executed. Is there some way through which i can execute such programs.
I have also tried other functions like exec etc.
It would not work as you are setting up your server at /var/www/html location which is used by public to access your computer through localhost. And since in localhost there is no such thing as redshift so it would run nothing and the operation would be executed and you will see no after effects. So, to run the system's command on localhost, you must run the server on any folder other than /var/www/html. So, to run the server on any other location, just go to that location and run
php -S localhost:8080.

Execution of DalekJS with PHP does not work

I use the testing framework DalekJS to test the user interface. To execute my testing script mytest.js I type into the shell:
cd /Applications/XAMPP/xamppfiles/htdocs/tests
dalek mytest.js
and it works fine. Now I would like to execute the same script with PHP. My code:
<?php
chdir('/Applications/XAMPP/xamppfiles/htdocs/tests');
$command = 'dalek mytest.js';
exec($command, $return, $return_var);
var_dump($return);
var_dump($return_var);
Running my PHP-script in browser, it prints:
array(0) { } int(127)
The DalekJS script generates a screenshot when executing in shell but running with PHP it does not happen anything. I have also tried shell_exec(), system() and passthru() without success.
Do you have any idea why the PHP script does not work?
Calling dalek from PHP works fine for me. Is it possible your PHP process is running with a different environment (containing PATH, amongst other things) that your user? Maybe the apache user or some such?
<?php
// change current working directory of the current PHP process,
// this will subsequently change the initial CWD of exec()
$whereMyTestsAre = __DIR__;
chdir($whereMyTestsAre);
// locate dalek
$dalek = exec('which dalek');
// abort if there is no dalek,
// you may want to check PATH or supply the full path yourself
if (!$dalek) {
echo "could not find dalek executable, please check your path";
$PATH = exec('echo $PATH');
echo '$PATH is ', $PATH, "\n";
exit(1);
}
// relative to $whereMyTestsAre
// exec() blocks until the child process (dalek) exits
exec('dalek mytest.js');

exec not working in php script ( WAMP Server )

I am using WAMP server on my system to execute php scripts.
I want to execute a script test.php from my main script main.php.
For that I am using exec function like this exec('php test.php');. In test.php I have given one echo statement.
But when I run my main.php script from the browser I am not able to see output of test.php script.
What am I doing wrong ? please suggest.
You have to give the proper path of php.exe
exec("c:\wamp\<where ever you exe is>/php.exe test.php");
so it has to be a proper path
use this command
echo exec('php test.php', $output); //this will print your echo statement.
print_r($output);

exec bash script from php website, script not running

There is a website with a button, cliking the button should lunch a bash script that suppose to unmount a directory. The button calls functions.inc php script with this function:
function sftmz_release_s3test_connections($bucket_name){
if($bucket_name == 's3test'){
drupal_set_message('Check mount status ! - released?');
$cmd = '/var/www/html/company/sites/default/modules/rp_minisite/admin/script.sh';
exec($cmd);
}
}
My problem is: When im in the shell and running the command:
/var/www/html/company/sites/default/modules/rp_minisite/admin/script.sh
It works fine.
When I click the button, the test appears, but it does not run the script. How can i view logs ? Can i print logs to shell ? i cant since its being activated using a button on html...
I assume this is permission issues?
Try getting some more info about what actually happens when you call exec:
exec($cmd, $return, $status);
echo '<pre>';
var_dump($return);//is an array, containing the commands output
echo '</pre>';
if ($status === 0)
{//normally, if a cmd exits with 0, all is well
echo 'Command executed';
}
If there is something funny going on, you might want to check if your script is relying on environment variables/aliases and the like being set. Perhaps you'll have to load a .profile file for the script to work.
Here you can see how to do this
Have you checked the return value of the exec() function ? See the doc for more info.
Try using the other arguments of the exec command..

Categories