Can't write log file in Linux using PHP - 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.

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.

changing a local registry key with PHP

I'm creating a PHP script in other to automate a browser performance test. One of the key features is to be able to enable / disable a plugin in Internet Explorer or Firefox.
Reading about it online, I saw I have to execute a similar command to :
[HKLM\Software\Microsoft\Internet Explorer\Extensions\{6D53EC84-6AAE-4787-AEEE-F4628F01010C}]
“Flags”=dword:00000001
In otehr to accomplish what I need. However, my question is: how can I accomplish that through PHP? I thought about using exec('cmd') but I'm not sure whether that's the correct way.
Does someone have done that before? The script is local only, and always going to work on Windows environment.
Thank you
Obviously, you can only do this running a PHP script locally on a Windows machine, it is not possible* to modify the Windows Registry of some other machine that just sends a request to a PHP script on your server.
But you should be able to run exec in order to change the local machine, some quick googling returned how to delete and add registry keys from the command line which seems promising.
Something like:
<?php
// Write the value you want to save to a .reg file
exec('reg import tweakExtension.reg');
?>
Ought to work. A little more instruction on using the reg command can be gleaned by running reg /?.
* we hope...

"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.

shell whoami doesn't equal php shell_exec whoami?

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.

Shell_exec with git pull?

I am setting up a github account, to work on a small project with some friends.
I would like to have my home machine able to do a git pull via php, so that we just have to call this small php file for the machine to be up to date.
As of right now :
<?php
$output = shell_exec('git help');
echo "<pre>$output</pre>";
?>
This works perfectly and I get the output, I am in the right directory, so git pull should work just as well, but I get a hanging page, no error, nothing.
Any idea ?
EDIT : A few precisions, the repo is pretty small, around 300K, it takes only a few seconds from the command line. I also tried shell_exec("dir"), and I am in the right directory. I am running the default installation of xampp on Windows 7 x64, if I can be precise enough :)
I suggest exploring set_time_limit() , as well as making sure your git pull does not stop if the user disconnects via ignore_user_abort(). Even running from a gigabit connected server, some repositories just take a while to clone.
Also, check PHP's working directory, and ensure the user running PHP has privileges to write to the repo. If you ran this via CLI and it 'just works', its a good chance that PHP was running without appropriate privileges when accessed via whatever web server you are using.
If you chmod the destionation directory as 777 and it works, there's a very good chance that you need to recompile apache/php for suexec support. Please, don't just leave it as 777 if that is the case :)
Either way, time out and user aborts are still valid considerations, even after you get it working.
So, to answer my own question.
It was in fact a permission problem (thanks tim), from the PHP CLI, the script was working.
The problem was that the service php installation is using some strange permissions.
So you/I need to start the PHP server via the command line (or in this case the Xampp control panel).
Now it's working, giving me the "Already up-to-date." answer I was waiting for :)

Categories