How to debug PHP codes in DirectPHP
Written by kksou   
Sunday, 14 October 2012

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

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, try to check your php.ini and change the setting for error_reporting to:
    error_reporting = E_ALL | E_NOTICE

  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  

 
# 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.
 
 
# 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($ar r);
echo $a;

Regards,
/kksou
 
 
# 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:
 
 
# 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:
 
 
# 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
 
 
# 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
 
 
# 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
 
 
# 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
 
 
# 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
 
Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved