If you are using DirectPHP, you'll know that it's very difficult to debug codes in DirectPHP.
This is because DirectPHP uses the standard PHP eval() function
to process your PHP codes. If you have used the eval() function
before, you'll know that it doesn't give you any meaningful error messages. The most you see is something like:
Warning: Invalid argument supplied for foreach()
in /JoomlaRoot/plugins/content/DirectPHP.php(54):
eval()'d code on line 9
Turn on Error Display
To debug any code, you need to know what is the error or warning message first. So the first thing is to get PHP Errors to display.
- Using PHP.ini - By default, most of the php.ini setting is to turn off error display. To turn it on, go to php.ini and make sure that the error display is turned on.
display_errors = on error_reporting = E_ALL
php_flag display_errors on php_value error_reporting 32767The value 32767 is the value for
E_ALL
.
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); . . . ?>
However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:
display_errors = on
How to Debug
To debug, here's what I usually do:
- Copy and paste your entire code and place them into a standalone .php file, say
test1.php
- Then in your article, use DirectPHP with only one statement:
- First try if test1.php works standalone. Any error message? Very Important: You might need to change the setting in php.ini to display error and warning messages. If you do not see any error messages, please see the above to see how to turn on error display.
- Now load the page in Joomla. Does it work? Did you see any error message?
- Once you have debugged your .php file, you can then copy and paste the code in
test1.php
back into your article.
<?php require("fullpath/test1.php"); ?>
Note that you must use the full path to your .php file.
Can DiectPHP runs all PHP codes
Please note that DirectPHP is a Joomla plugin. As the name suggests, a plugin is run within a Joomla article.
What this means is that NOT ALL PHP scripts can be run within the Joomla framework.
Below are some cases in which PHP might not run properly:
- HTML header. DirectPHP runs within a Joomla article. By the time DirectPHP processes your PHP codes, the HTML header for your Joomla article has already been displayed. If your PHP also outputs another HTML header (which is very common for a standard standalone .php file), it might conflict with the Joomla framework.
- Special PHP library. Some .php codes require special PHP library which might not be present in the PHP running on your host. In this case, your .php file might not work.
- Mysql database. Each Joomla installation has its own underlying mysql database. If your .php file uses its own mysql database, you must MAKE SURE that after running your codes, your last statement must connect the mysql database back to the mysql database that your Joomla is using. Otherwise you will find that your Joomla site will stop working at the end of your code because it has no way to connect back to the Joomla environment.
Comments
DirectPHP uses the standard PHP eval() function. Any standard PHP function should work with eval().
If it works with echo(), there's no reason why it didn't work elsewhere.
To test, you can try:
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$a = json_encode($arr);
echo $a;
Regards,
/kksou
I have a really weird problem...
I have two modules with the almost exactly same PHP code on it (only the path changes, but on Joomla if i call module 1, everything goes fine, but if i call module 2, i have a (by 'call' i mean 'loadposition') :
PHP Fatal error: Cannot redeclare curPageURL() (previously declared in /plugins/content/DirectPHP/DirectPHP.php(56) : eval()'d code:2) in /plugins/content/DirectPHP/DirectPHP.php(56) : eval()'d code on line 11
I can't understand why the code is correctly parsed on module 1 but not on module 2...
Code module 1:
DirectPHP is a content plugin. As the name suggests, it's for use ONLY within the Joomla article. It DOES NOT work when it's in module (because it's not an artilce).
What you can do is to install some of the modules that I've written, such as the googleMaps module: http://www.kksou.com/php-gtk2/Joomla-Gadgets/googleMaps-module.php
Although you will not be using the googleMaps module or plugin, what that module does is that it will make a trigger to the content plugin so that DirectPHP will run.
Please give it a try and let me know if it works, ok?
Regards,
/kksou
PHPDirect works on my joomla 3.2.x site for normal commands. I want to use a blocked command (e.g."fopen"). Since there is a security issue with my provider, I could not modify the block list to allow fopen.
Since you recommend anyway the "require" method to include this kind of functions from a library file, I tried this one.
But: it's not working:
my code is:
** php echo 'hello world';
require("/phpscripts/test.php");
echo 'gugus';
**
If I open the page, I get just a blank page "hello world".
If I comment out the "require" statement, "hello worldgugus" is displayed as expected.
==> Do you have any idea why "require" does not work? Might this be connected to the security issue with my provider, which prevents my from saving the updated blocking list??
Thanks for all, nounours
Try fullpath for your require files i.e.
require("/full/path/to/your/php/folder/phpscripts/test.php");
If you know how to set error_reporting in php.ini, you can also display all warning messages and see what is the error message given by php.
Let me know if the fullpath works, ok?
Regards,
/kksou
a) the full path from the start of the website "/myphpscripts/
b) the full path from my shared hosting account "/html/domainA/myphpscripts/" (this is also the root of my ftp to the server)
or
c) the full hardcoded path from the server (which is normally transparent for me as a shared hosting user): "/home/www/hosting7564/html/domainA/myphpscripts/"
=> I tested a) and b), and both do not work. Now, after you insisting, I had the idea to test also c).
=> the path c) works!!!
Sorry for bothering and thank you very much for your support!!! I will buy you a pizza.
nounours
Yes, fullpath refers to (c). It usually starts with /home or /usr. I should have been clearer earlier.
Glad it's working now!
Warm Regards,
/kksou
require(getcwd() . "/phpscripts/test.php");
I'm getting eval error :-(
I tried to using a simple echo getcwd() but I got the same eval error.
1. Directphp is great, could not do without it
2. I have added a Try Catch statements in a class called from the PHP in the article and Joomla ignores it. When there is an eroor the code jumps into some Joomla classes.
Anyone any ideas why this should be? Or how to get it to work?
>>> im having so much trouble to connect my tables iny mysql to joomla article
You may want to let people know what have you tried? And what are the error messages you see on the screen?
It will be much easier for other people to help you...
Regards,
/kksou
I have a very strange behaviour on a 1.5.26 website.
In a certain article, I use directphp to load in an external page (trough a curl function). Everything works fine, but ... when the page that is called exceeds about 100k, I get a complete blank page in Joomla. No errors, not even in the error.log on the server. No clue whatever, just a blank empty page. Do you know of any limitations on the size of content that can be displayed trough the echo command and directphp ?
After hours of testing and debugging, my hope to solve the problem lies in your hands
Regards,
Filip
One of the key thing in debugging PHP code is to be able replicate the error consistently.
So I'm not sure if you can reproduce the error you described consistently.
If you can, then you can try the following:
1) As described above in this article, try to copy the entire PHP code that you've put inside DirectPHP and save them in a standalone .php file.
2) I suspect he error is due to your script running beyond the max_execution_time as specified in your php.ini file.
3) Try to run the standalone .php file you have created in (1). Did you get a blank page?
4) Now go to your php.ini file. Check what is the value of max_execution_time in your php.ini
e.g. if max_execution_time = 30
Try changing it to max_execution_time = 300
5) Rerun the standalone .php file again. Did you still get a blnak page?
6) If (5) is ok now, you can transfer the code back to DirectPHP. Now reload the page. Does it run ok now?
7) If (6) is ok now, it shows that the problem statementn is indeed the max_execution_time in php.ini
Please give the above a try and let me know if it works.
Regards,
/kksou
RSS feed for comments to this post