PHP/Apache - PUT/DELETE data is lost after 5 requests - php

I'm simply trying to do PUT/DELETE requests in my PHP application, but after the first 5 requests, the data is lost.
I've reduced the code to this to illustrate the issue:
index.php
$h = fopen('php://input', 'r');
var_dump(fread($h, 1024));
die();
CLI input
curl -X PUT http://cms.dev.com -d '{"foo":"bar"}'
So, for the first 5 times I run that, I get:
string(13) "{"foo":"bar"}"
Then, from the 6th onwards, I get:
string(0) ""
I'm running PHP Version 5.6.0beta1 and Apache/2.2.26, installed on OSX 10.9.3 via Mac Ports.
POST works fine.
EDIT It might be worth noting this can be replicated on 2 other colleague's MacPorts setups, but can't on MAMP, which seems to act correctly.
Does anyone have ideas? It's driving me crazy!
Thanks,
Todd

I cannot reproduce this using the built-in webserver, which might indicate an interaction between apache and php.
Try running :-
php -S localhost:8001
in one terminal and
for n in `seq 1 100`; do curl -X PUT http://localhost:8001 -d '{"foo":"bar"}'; done
in another - see if it's limited to php or at the mod_php level.
My testing is on a linux box with 5.4.9 fwiw.

Upgrading to php 5.6beta3 fixed the issue.
Must have been a bug in php 5.6beta1!
Hopefully this'll help someone else :)

Related

PHP exec() under Apache returns null and 255 when running a script

Ok, after having bashed my head on this for hours I decided to ask for help. I have a Windows Server 2008 running Apache 2.4 and PHP 7.1. My application must run a PHP script on the server when the user clicks a button on the browser.
This is working fine on my desktop with Windows 10. However, on the server, exec() returns "null" and an exit code of 255.
I read all I could find on exec() issues and tried the following:
exec("C:\\PHP7\\php.exe -v", $output);
I got the proper response containing PHP's version information.
Then I decided to check the configuration files:
exec("C:\\PHP7\\php.exe --ini", $output);
All files were in place.
Then I decided to perform a syntax check on my script:
exec("C:\\PHP7\\php.exe -d display_errors=1 -l C:\\Apache24\\htdocs\\script.php", $output);
No errors were found.
Finally I decided to check the user account:
exec("whoami", $output);
Got "NT Authority\SYSTEM" as expected. To make sure that the script was able to run under the SYSTEM account I used SysInternals psexec:
psexec -s C:\PHP7\php.exe C:\Apache24\htdocs\script.php
Everything ran smoothly.
In other words, the script shows no problems when executed from the command line, either under a user account or the system account. I have also proven that PHP is being properly invoked by exec().
So, then I decided to check for "hidden" errors in my code adding the following two lines to the very beginning of the script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
But, no joy. And I'm out of ideas.
Can any good soul help me on this?
Thanks a bunch,
Miguel.
Finally! The key to the answer was here: PHP exec() git fetch failing with return value 255.
I was unable to see any errors, even adding the "2>&1" pipe redirection to my commands. After reading that article, I learned that proc_open() is way better than exec(). Quoting from PHP's documentation:
proc_open() is similar to popen() but provides a much greater degree of control over the program execution
So, I replaced my exec() for a few lines of code (refer to the example in the manual) and found that the problem was being caused by the Zend Opcache, which was enabled for CLI. The quickier solution for me was to disable it in the command line:
php.exe -d opcache.enable_cli=0 myscript.php
VoilĂ ! Problem solved!

Setting Up a Cronjob on MAMP - InfinteWP

I am installing InfiniteWP on a local environment using MAMP. Everything works perfectly except for my inability to figure out how to setup a cronjob properly. I did find some information (dated?).
I found this: http://www.freewaytalk.net/thread/view/114906 . But am a bit worried that I may be implementing something dated.
The instruction given by InfiniteWP in order to start the cronjob say this:"php -q -d safe_mode=Off /Applications/MAMP/htdocs/IWP/cron.php >/dev/null 2>&1" So I need instruction on how to implement this php command on my Mac. Terminal?
I'd really appreciate advice and validation on the best course of action.
You could always use a GUI such as : http://code.google.com/p/cronnix/
This is simply a front end to the crontab tool, then simply set the time you want it to run and the URL to:
/Applications/MAMP/htdocs/IWP/cron.php
I didn't have any luck with that google code option but Mac has cron natively and your InfiniteWP code can be added in a few lines. See instructions here: http://www.maclife.com/article/columns/terminal_101_creating_cron_jobs

PHP exec using LP command fails on Mac OS X

I am trying to spool a file to a printer attached to the server using PHP.
I send a command such as lp -d 'Brother_QL-570' '/Users/user_name/Documents/wwwroot/mmsprint/mmsUw8Vv9 using EXEC in PHP and get a return value of 5. The command works fine in the terminal window.
If I send ls, whoami etc, the command executes fine.
whoami and ls live in /bin, so I created a symbolic link for lp and that made no difference.
Also tried /usr/bin/lp and that did not work either. I'm pulling what little hair I have left out over this.
Safe mode is off. The path variables are the same between PHP and my terminal environment.
I am running MAMP on Lion.
FWIW, the command cat /Users/user_name/Documents/wwwroot/mmsprint/mmswKJqYK | lp -d Brother_QL-570 has a return value of 133.
This is my code:
$temp_file = tempnam("./", 'mms');
$pdf->Output($temp_file, "F");
$pdf->Close();
$cmd = "lp -d 'Brother_QL-570' '".$temp_file."'";
exec($cmd,$output,$retval);
error_log("-----cmd=".$cmd."\n", 3, "printCard.log");
foreach ($output as $a) {
error_log("-----output=".$a."\n", 3, "printCard.log");
}
error_log("-----retval=".$retval."\n", 3, "printCard.log");
Any thoughts? What am I missing? This should be easy. Argh!!
It turns out to be a problem with MAMP. I googled "MAMP exec fails" and came up with several good answers. Its a compatibility problem with libraries. I added DYLD_LIBRARY_PATH='' && to the command and Bob's my uncle. Thanks for the replies.
What is the output from the command?
Most likely the PHP user (apache probably) has no permission to print things. Add that user to the lp group. (Assuming macs work similar to unix in this.)
I found a similar solution to the one mentioned about using DYLD_LIBRARY_PATH='' &&. Using this worked for me, so I tried to find a more permanent fix than including this in all my code. I opened up the file /Applications/MAMP/Library/bin/envvars and commented the following two lines out:
DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH
After restarting apache, this is working GREAT! Not sure why this is happening to me though. Might have to do something with the fact that I am running 2 virtual hosts.

php cli not working in my web server

I am trying to run a php file in my web server in command line using
php test.php
But it is not working. it runs forever and takes 100% resources.
It was working yesterday and now it is not working.
I tried to debug the process using
strace -p <pid>
and got lots of
times(NULL) = -2058427839
Can anyone help me to debug this? Rebooting and upgrading is my last resort. Please suggest a solution without reboot or upgrade.
Try to run your php file in cli without the configuration file using the following command:
php -n <your-filename.php>
and then check whether its working?
Place this into your test.php file to check that it is working and to see what is available.
phpinfo();

PHP from commandline starts gui programs but apache doesn't

First, I read some threads by people with similar problems but all answers didn't go beyond export DISPLAY=:0.0 and xauth cookies. So here is my problem and thanks in advance for your time!
I have developed a little library which renders shelves using OpenGL and GLSL.
Last few days I wrapped it in a php extension and surprisingly easy it works now.
But the problem is it works only when I execute the php script using the extension from commandline
$php r100.php(i successfuly run this from the http user). The script is in the webroot of apache and if I request it from the browser I get ** CRITICAL **: Unable to open display in apache's error_log.
So, to make things easier to test and to be sure that the problem is not in the library/extension, at the moment I just want to start xmms with following php script.
<?php
echo shell_exec("xmms");
?>
It works only from the shell too.
I've played with apache configuration so much now that I really dont know what to try.
I tried $xhost + && export DISPLAY=:0.0
In the http.conf I have these
SetEnv DISPLAY :0.0 SetEnv XAUTHORITY /home/OpenGL/.Xauthority
So my problem seems to be this:
How can I make apache execute php script with all privileges that the http user has, including the environment?
Additional information:
HTTP is in video and users groups and has a login shell(bash).
I can login as http and execute scripts with no problem and can run GUI programs which show up on display 0.
It seems that apache does not provide the appropriate environment for the script.
I read about some difference between CLI/CGI but cant run xmms with php-cgi too...
Any ideas for additional configuration?
Regards
Sounds bit hazard, but basically you can add even export DISPLAY=:0.0 to apache start-up script (like in Linux /etc/init.d/httpd or apache depending distro).
And "xhost +" need to be run on account which is connected to local X server as user, though I'm only wondering how it will work as php script should only live while apache http request is on-going.
Edit:
Is this is kind of application launcher?, you can spawn this with exec("nohub /usr/bin/php script.php &"); .. now apache should be released and php should continue working in background.
In your console, allow everyone to use the X server:
xhost +
In your PHP script, set the DISPLAY variable while executing the commands:
DISPLAY=:0 glxgears 2>&1

Categories