shell whoami doesn't equal php shell_exec whoami? - php

In a shell I do simple whoami and I get geoff, which is good, since that's who I am.
In a php file I have shell_exec('whoami'); and I get nobody.
This seems to me to be a difference that could be explaining my original problem: imagemagick works from a shell but doesn't work from within a php script.
Now from working through this site I think this means that my apache xampp assigns a different user name rather than using my usual user name.
I have tried to change my login within the php by way of sudo but this isn't working for me as of yet.
Can anyone help?

It should be simple to understand.
In shell you are logged in as Unix user geoff and that's what you get.
PHP is run in a httpd process in Unix which has nobody as the owner hence that's what you get when you shell_exec('whoami'); from PHP.

#anubhava is correct about why you get two different answers when you run whoami. However, if you're trying to convert a PDF to a PNG using ImageMagick (like in your comment on the question), even using the full path to ImageMagick's convert won't work if the script's PATH doesn't contain the path location to Ghostscript also. Without messing with any user paths, you could add:
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
Or something similar depending on your setup. The gs executable has to be in your script user's path somewhere or ImageMagick will fail to convert PDF or EPS files.

The result from whoami you obtained simply means that your apache is running as nobody. This is as it should be and I strongly recommend not to change this.
The reason imagemagick doesn't work isn't probably directly related to the privileges with which your php code is run. There isn't enough information to diagnose the problem exactly, but it is most likely an executable or library search failure. Make sure that the binary and libraries are readable by nobody and that they are in a location where apache will find them. In particular ensure that $PATH and $LD_LIBRARY_PATH are set up correctly.

add "2>&1" to your command
specify the full path to shell_exec(nobody has no $PAHT, so it just don't know where imagemagic binary is stored.
try to run the script as user on the console to see that is going wrong.

Related

mbstowcs from apache not getting executed

I am calling a perl script from one php page. This perl script calls some other scripts inturn and a C binary file. The C binary uses mbstowcs function inside. This is not getting executed correctly.
But the same thing If I call from terminal(the perl script) everything works fine.
I have given proper permissions to all the files before executing(Even gave 777 permission).
Is apache uses some other terminal session/ something else to run the scripts.
I am using ubuntu 14.04 and apache webserver.
Without given any further information, I suspect you may have run across the following situation:
Use of non standard mbstowcs feature
This information is dated I realize but it's all I can offer at this time unless you can provide some more information like an error message from your Apache log file.
Sorry I could not be of more help.

Can't write log file in Linux using PHP

I'm running CentOS 6.5 on a Google Compute Engine instance which I use for an ejabberd XMPP server. I also have php 5 installed and ejabberd is configured to use a php script to authenticate users.
So far so good - ejabberd executes the script and recieves the correct result from it. The problem is: I want the PHP script to write a log file. So far I've tried:
Writing a file using file_put_contents to /var/log/mlog.log - this didn't work. so I've tried manually creating the file and giving it chmod 777 (for testing). No result - the file remains empty. But - when I execute the script manually using php from terminal the log is written.
Writing to syslog - I've configured php.ini to use syslog and then tried logging. Same result: nothing when ejabberd runs the script, but when I manually run it it works.
Configuring error_log file and using error_log($message). Again, it didn't work.
I came to realize it must be something wrong with the write permissions of the ejabberd user (which runs the php scripts), but even when I set chmod 777 to every file in every option of the above, the log remains empty.
Any hints? What am I missing? (as you can probably tell, I don't have much knowledge in Linux and this is the first time I'm using it in a project)
This may not be the answer you are seeking. I am not much familiar with Linux. There is a KeyLogging php class know as KLogger. You can create logs using this class. It is very easy to use, You have to download php file and use it. You can find it in github. Hope this might solve your problem.

"Running" a PHP file

I think this has to be a very simple question but I am not finding any answers for what I am trying to accomplish.
I have a PHP file which will send out emails. This part works. The thing is, I need this PHP file to "run" every 5 minutes.
My problem is not with the scheduling, I'm fairly certain I understand how to do that.
My issue is with the "running" of the PHP file.
My mind is totally void of any information of how to "execute" a PHP file rather than just make it open in a browser and spit its code everywhere.
I know that this PHP file works because when I am working on it in Dreamweaver and I click the "Discover" link where it says "Dynamically-related files for this document may have changed and should be re-discovered by the server" it executes this PHP file and I get my emails sent.
I've tried going through command line like THIS but all it does is spit out the php code as if I was running it in a browser. Nothing actually sends.
Is there some way to change the way the PHP file itself is formatted that would make it do this?
What is Dreamweaver doing when I click "Discover" that makes it work?
I feel like this should be a simple thing and its insane that I am not understanding it.....
Assuming you installed PHP with default settings, the PHP folder should be in your PATH variable. In which case, all you have to do is run the command php filename.php.
If not, you'll have to direct the command to where PHP is installed, maybe something like "C:\Program Files\PHP\php.exe" filename.php - either way, all you're doing is invoking the PHP binary on the file.
You have options. If it's a web page that you can access through your browser (and that sends the email as you'd expect), you can just request that page via HTTP every five minutes. Personally, I'd do this with cURL (e.g., curl.exe http://localhost/myfile.php). You can find Windows binaries here. You could also use PowerShell or something.
If this isn't a web page, or if you just don't feel like going that route, you can execute the PHP through PHP's command-line interface (CLI), as you've already figured out. C:\path\to\php.exe myfile.php will execute your code and spit the output to STDOUT. If everything is working with this except for sending email—which I'm guessing you're doing through mail—the issue may be with your PHP configuration. By default, PHP looks for the php.ini file in these locations. You can also explicitly tell the PHP CLI what php.ini file to use with the -c option (e.g., php.exe -c C:\path\to\php.ini myfile.php). Of course, you'll need to have everything configured properly in php.ini to send mail. This can be a bit trickier on Windows than on your average Linux machine with sendmail available.

PHP -> python seems to not work on web server

I have a few pages (one html, one php, and one python script) that takes a user input, and then outputs a pdf file (using reportlab) for the user. When I test on apache (localhost), it works perfectly.
However, online on a real web host it is not working. The version of PHP on the web server is 5.2.17 and python is installed.
Could the version be an issue? I am very lost because I'm not very experienced and it works perfectly on Apache and not at all on the internet.
Last thing, the command I use to call the python function from php is this:
$ed = exec("python pdfgeneration2.py $name $age");
I also thought maybe there is a better command for this?
My question may be vague and unclear but if anyone has any ideas it would be greatly appreciated.
Look at /var/logs/httpd/error_log, or wherever your distro's apache stores logs. Odds are you'll find an error message there from PHP.
In addition to checking the path like sberry recommended, double check perms, too. Apache will need execute permissions on the python script: chown apache:apache pdfgeneration2.py

Run a PHP CLI script from a webpage

I have a (possibly dumb) question.
I have a script made in php, constructed for cli usage. Works fine when I run it from the command line, no problem there. The problem is that the site I'm working on has ssh restrictions on the hosting server and I cannot ssh there to run it. Hence my question: how can I run the script from another php that is web-accessible? Already tried with exec(), system(), etc.
The main problem is that I need he $_SERVER['SHELL'] variable set, and when the call is comming from a web browser of course php doesn't set it.
Any ideeas will be greatly apreciated, thanx.
There are many possibilities why exec() and related function calls are not working for you.
Your webhost does not have PHP-CLI installed. Just a webserver module
You need to use the full path to the php binary for lack of a decent shell environment. E.g. /usr/bin/php <script> instead of php <script>.
Your webhost has installed PHP-CLI on a non-standard path (e.g. /usr/local/bin/php, or /opt/php5/php)
The webserver user does not have rights to access the php binary
Et cetera..
maybe update the php script to be both an include and a cli script.
use
__FILE__
to check if it's a file, then read the params. otherwise do nothing.
and as an include just call the function you want directly.

Categories