For example instead of getting the following
post:Array (
"a" => "b",
"c" => "d"
)
I just get this:
post:Array (\n "a" => "b",\n "c" => "d"\n)
It's really uncomfortable to read this while debugging my code. So if you have any suggestion on why this couldn't work alright, tell me.
I am running it in a Windows7 Putty connected to an Ubuntu virtual server, which runs supposedly it's default Apache/PHP configuration. (well probably not, but as always nobody in the team remembers to have changed anything)
edit: Someone requested the code that writes to the error.log:
<?php
error_log(print_r(array("a"=>"b","c"=>"d"),1));
?>
The commands to view the error log are:
sudo tail -f /var/log/apache2/error.log
sudo vim /var/log/apache2/error.log
sudo cat /var/log/apache2/error.log
In all instances the problem occurs that \n is not executed as expected.
I also faced the same problem, but after spending a few minutes I got a solution.
When you do tail, use as below:
sudo tail -f /var/log/apache2/error.log | sed -e 's/\\n/\n/g'
If you want, you can create a file. Give it some name and paste the above command and place that in the /usr/bin/ folder.
For example
vi tailme
With the contents:
#!/bin/bash
tail -f /var/log/apache2/error.log | sed -ue 's/\\n/\n/g'
And put this in /usr/bin/. Now you can use tailme as a command.
In some cases (e.g. Mac) using Perl might work better:
tail -100f /var/log/apache2/error.log | perl -pe 's/\\n/\n/g'
The problem is caused when the Apache process can't write into the error_log file, so the syslog writes into the file instead. The syslog messes up the line breaks.
So just do:
chmod 777 error.log
This should solve your problem. Source.
When calling error_log, you can force PHP to log directly instead of passing it to Apache which is the default behavior. To do that, just specify 3 as the second argument and a file path as the third argument.
error_log("error message", 3, $logFileLocation);
For more information, check out PHP error_log documentation.
If you are viewing the output in the browser, try wrapping your output statement with the <pre> tags.
<?php
$post = Array("a" => "b", "c" => "d");
echo "<pre>";
print_r($post);
echo "</pre>";
?>
outputs to a browser a formatted array:
Array
(
[a] => b
[c] => d
)
Related
I am trying to execute a couple of scripts by using a remote interface. The environment is Raspbian on a Raspberry Pi (although I will be using Debian later as well) running LAMP.
The files are test.php and test.sh in the root directory of the webserver (say example.com)
test.sh
#!/bin/bash
sudo pkill chromium-browse
sudo reboot
test.php
<?php
$output=null;
$resultCode=null;
exec("./test.sh", $output, $resultCode);
// $ouptut = shell_exec('./test.sh 2>&1'); //tried this too
// echo shell_exec("./test.sh"); // as well as this
echo "Returned with status $resultCode and output:\n";
print_r($output);
?>
Initially, I had used
chmod u+x test.sh
but got an error code of 126. So I did this:
chmod 777 test.sh
Now I get an error code of 1, but it still doesn't execute. I have also tried
sudo visudo
then added
pi ALL=(ALL) NOPASSWD: ALL
(pi is the current loggedin user)
Currently I am getting this:
Array
(
[0] =>
[1] => We trust you have received the usual lecture from the local System
[2] => Administrator. It usually boils down to these three things:
[3] =>
[4] => #1) Respect the privacy of others.
[5] => #2) Think before you type.
[6] => #3) With great power comes great responsibility.
[7] =>
[8] => sudo: no tty present and no askpass program specified
)
Note: I use sudo all the time at the command line without being asked for a password.
I do have another php file in the same directory that executes an actual system command successfully. It has this line:
$uptime = exec("uptime");
which works just fine, so I know system commands are possible. Is there any way to do this? I have seen other similar questions on SO and other sites, but none of those answers have worked for me.
Any help appreciated.
I have this script,
<?php
exec('/usr/local/bin/antiword /var/www/html/rezyme_2015.doc', $output);
var_dump($output);
?>
but in browser I see:
array (size=0)
empty
I have in /usr/bin/antiword and I copy to /usr/local/bin/antiword. If I run in console:
php antiword.php
Output is
[0] =>
string(0) ""
[1] =>
string(52) "hello"
[2] =>
string(0) ""
In one forum, I saw the exact problem reported. The person that has this problem says that he solved this problem. Here is the quotation:
"Yes, it turned out to be the accessibility of the map files - they weren't world readable, once I changed permissions, it worked great. Thanks all"
However, I did not unnderstand how he solved the problem and how also I can reach and allow those map files of antiword.
And I found:
Maybe the user apache or www-data or nobody or whatever your webuser is
called, has no permissions to execute it?
You can simply check this by:
1) sudo su www-data (or whatever etc.etc see above)
2) call the script as you do in your exec() command
Or you blocked exec() in PHP.ini.
I not found in PHP.ini in /etc/php5/apache2/ and /etc/php5/cli/ this blocked exec() or exec()
And I run in my terminal:
sudo su www-data
This account is currently not available.
My question, why in my browser this script not correctly work:
array (size=0)
empty
When I get chmod 777 -R for file.doc then I have return array, but how do right ?
and what I need for fix this problem, step-by-step, whats create, whats change, help please.
I'm having trouble using PHP to execute a casperjs script:
<?php
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
var_dump(exec("echo \$PATH"));
exec("/usr/local/bin/casperjs hello.js website.com 2>&1",$output);
var_dump($output);
Which results in the following output:
string(43) "/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:."
array(1) {
[0]=>
string(36) "env: node: No such file or directory"
}
The only stackoverflow posts I could find hinted that there's a problem with my paths, and that maybe the PHP user can't access what it needs.
I have also tried the following: sudo ln -s /usr/bin/nodejs /usr/bin/node
Does anyone know what I would need to do or change for this error to resolve?
Thanks
My guess is you have something, somewhere, that assumes node is installed.
First, are you running php from the commandline? I.e. as php test.php in a bash shell. If so, you can run the commands, below, as they are. If through a web server the environment can be different. I'd start with making a phpinfo(); script, and then run the troubleshooting commands through shell_exec() commands. But, as that is a pain, I'd get it working from the commandline first, and only mess around with this if the behaviour is different when run through a web server. (BTW, if you are running from a cron job, again, the environment can be slightly different. But only worry about this if it works from commandline but does not work from cron.)
Troubleshoot hello.js
The easy one. Make sure your script does not refer to node anywhere. Also remember you cannot use node modules. So look for require() commands that should not be there.
Troubleshoot your bash shell
Run printenv | grep -i node to see if anything is there. But when PHP runs a shell command, some other files get run too. So check what is in /etc/profile and ~/.bash_profile . Also check /etc/profile.d/, /etc/bashrc and ~/.bashrc. You're basically looking for anything that mentions node.
Troubleshoot phantomjs/casperjs
How did you install phantomjs and casperjs? Are the actual binaries under /usr/local/bin, or symlinks, or are they bash scripts to the . E.g. on my machine:
cd /usr/local/bin
ls -l casperjs phantomjs
gives:
lrwxrwxrwx 1 darren darren 36 Apr 29 2014 casperjs -> /usr/local/src/casperjs/bin/casperjs
lrwxrwxrwx 1 darren darren 57 Apr 29 2014 phantomjs -> /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs
And then to check each file:
head /usr/local/src/casperjs/bin/casperjs
head /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs
The first tells me casper is actually a python script #!/usr/bin/env python, while the second fills the screen with junk, telling me it is a binary executable.
I'm having a little issue with adding shebang #! with my php script on RedHat linux. I have a small piece of test code with shebang added (I've tried different variations as well), but I get the following error message everytime I try to run the script.
Error msg:
-bash: script.php: command not found
Test script:
#!/bin/env php
<?php echo "test"; ?>
Shebang #! variations:
#!/usr/bin/php
#!/usr/bin/env php
It should (for most systems) be #!/usr/bin/env php, but your error isn't related to that.
-bash: script.php: command not found
It says that script.php is not found.
If the problem was the shebang line then the error would say something like:
bash: script.php: /usr/env: bad interpreter: No such file or directory
Presumably, you are typing script.php and the file is not in a directory on your $PATH or is not executable.
Make it executable: chmod +x script.php.
Type the path to it instead of just the filename, if it is in the current directory then: ./script.php.
Instead of 2, you can move/copy/symlink the file to somewhere listed in $PATH or modify the $PATH to include the directory containing the script.
If you script is not located in your /usr/local/bin and is executable, you have to prefix calling your script with php like this:
php myscrip.php
For shebangs, here is what I use:
Like this:
#!/usr/bin/php
or this:
#!/usr/bin/env php
In reply to #NVRM's comment regarding only single use of -d, this is not true.
Start with a chmod +x script as
#!/usr/bin/php
<?php
phpinfo();
and run script | grep -E 'memory_limit|error_reporting', and you'll see
error_reporting => no value => no value
memory_limit => 128M => 128M
Now add some -d entries so you have
#!/usr/bin/php -d memory_limit=2G -d error_reporting=-1
<?php
phpinfo();
and re-run script | grep -E 'memory_limit|error_reporting', and you'll now see
error_reporting => -1 => -1
memory_limit => 2G => 2G
Thus demonstrating you can set multiple options.
In fact, the entire command line is what you are working with here. So you can load extensions, use a different config, etc., everything you can do at the command line.
Leaving here some little notes:
To use a php binary located inside the same folder.
As example a php7.2 executable copied from /usr/bin is in the same path along a hello script.
#!./php7.2
<?php
echo "Hello!";
To run it:
./hello
Which behave just as equal as:
./php7.2 hello
This give portability, but beware of system architectures, the php binary might not match the target platform.
Setting allowed memory from the hashbang:
We can set one INI entry from the hashbang line:
#!/usr/bin/php -d memory_limit=2048M
<?php
phpinfo();
exit;
Then to see if php had understood, using phpinfo():
./myphpProg | grep memory
Correct shell output should contain:
memory_limit => 2048M => 2048M
Doing the above is similar as this command line:
php -d memory_limit=2048M myphpProg.**php**
This is why we can set only one ini value in hashbangs, as php accept only one -d parameter at a time.
find callable shebang for PHP in Linux,
Don't memorize this it, learn how to use it
which php
output
zeus#pop-os:~$ which php
/usr/bin/php
then shebang must be
#!/usr/bin/php
Just noticed something, but I'm not sure why this is true, or why it is different than a standard script, maybe someone can help me solve this riddle. When running a PHP script that contains a json_* function from the command line as an executable I will get a Fatal error: Call to undefined function json_encode() in /var/www/jsontest.php on line 6 with the script below
#!/usr/bin/php -n
<?php
$arr = array('foo', 'bar', 'baz');
print_r($json = json_encode($arr));
print_r(json_decode($bar));
This also happens with json_decode() when trying to decode standard clean json input (validated via jsonlint)
The above script was being run as follows, in a Linux/DEB terminal...
$ chmod +x jsontest.php
$ ./jsontest.php
By running this through my local webserver though, I receive my expect output
#!/usr/bin/php -n ["foo","bar","baz"]Array ( [0] => foo [1] => bar [2] => baz )
What is going on? Why is JSON not available when interpreted as an executable?
PHP version is PHP 5.5.3-1ubuntu2 (cli) (built: Oct 9 2013 14:49:24) and PHPInfo for anyone who might want it is published on my Ubuntu One Account
I'm guessing that the issue here is your php.ini file - PHP uses a different ini file when running as CLI. Have a look at Explosion Pill's answer on this question:
When running php from the command line, you can use the -c or
--php-ini argument to point to the php.ini file to use. This will allow you to use one php.ini file for both. You can also alias php to
php -c/path/to/php.ini if you are running the script yourself.