How to debug PHP codes in DirectPHP

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
    

  • Using .htaccess - If you do not want to touch your php.ini, you can also turn on error display in .htaccess:
  • php_flag display_errors on
    php_value error_reporting 32767
    
    The value 32767 is the value for E_ALL.

  • In your PHP script - If you have no access to php.ini or .htaccess, you can turn on error display right in your PHP script. Include the following at the top of your PHP script:
  • <?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:

  1. Copy and paste your entire code and place them into a standalone .php file, say test1.php
  2. Then in your article, use DirectPHP with only one statement:
  3. <?php
    require("fullpath/test1.php");
    ?>
    

    Note that you must use the full path to your .php file.

  4. First try if test1.php works standalone. Any error message?
  5. 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.

  6. Now load the page in Joomla. Does it work? Did you see any error message?

  7. Once you have debugged your .php file, you can then copy and paste the code in test1.php back into your article.

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   

0 # MattB 2012-11-28 15:27
:-) Does the json_encode work? Does DirectPHP work with the JSON PHP extension? I only get the coded information printed in the form when I use an echo command.
Reply | Reply with quote | Quote
0 # kksou 2012-11-28 17:27
Hi,

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
Reply | Reply with quote | Quote
0 # lhooq 2013-02-27 03:41
Hi.

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:
Reply | Reply with quote | Quote
0 # labiere 2013-05-22 09:49
Installed DirectPHP version 2.5 as I run Joomla 2.5 with K2 and Gantry framework. Activated the plugin, changed the filter setting. I see that php code works in the body of an article. When I create a custom HTML module and use the same code, It does not parse as php - I simply see the literal code displaying on the frontend. Any way to change a setting so I can have this working in modules too? :sad:
Reply | Reply with quote | Quote
0 # kksou 2013-05-24 02:00
Hi,

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
Reply | Reply with quote | Quote
0 # Gilles 2014-04-22 08:08
Thank you very much for this great plugin and all the good tipps on your website!!!

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
Reply | Reply with quote | Quote
0 # kksou 2014-04-23 07:32
Hi,

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
Reply | Reply with quote | Quote
0 # Gilles 2014-04-23 08:48
Thnx. I must admit, I'm not entirely sure what you mean by "full path"

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
Reply | Reply with quote | Quote
0 # kksou 2014-04-23 16:45
Hi Gilles,

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
Reply | Reply with quote | Quote
0 # NetVicious 2016-01-22 13:41
IT's there any way to use something linke

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.
Reply | Reply with quote | Quote
0 # Chris 2014-11-23 06:16
Two things
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?
Reply | Reply with quote | Quote
-1 # Roy Lester Santos 2015-02-07 05:10
Hello Kksou i have problem how to connect my database in mysql to my article using this plug in? can you put the source code in it? im having so much trouble to connect my tables iny mysql to joomla article :(( thankss
Reply | Reply with quote | Quote
0 # kksou 2015-02-08 07:13
Hi Roy,

>>> 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
Reply | Reply with quote | Quote
0 # Filip Serbruyns 2015-03-09 02:34
Hello 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
Reply | Reply with quote | Quote
0 # kksou 2015-03-09 07:38
Hi 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

8) Now you can slowly adjust the max_execution_time, say to 250 then 200 - until you find one that is just long enough to run your script.

Please give the above a try and let me know if it works.

Regards,
/kksou
Reply | Reply with quote | Quote

Add comment


Security code
Refresh