I'm coding a form in Drupal 7, and I want to 'include' a php file that adds my DB connection variables to my code.
I've tested my code in several ways, and I'm sure that the 'include' makes me get a WSOD when I run Cron in my site.
I've Googled about this and I tried:
include("/sites/all/libraries/php/connection.inc");
include("./sites/all/libraries/php/connection.inc");
include"./sites/all/libraries/php/connection.inc";
I also tried the above with '.php' extension.
And my last try:
define('__ROOT__', dirname(dirname(__FILE__)));
include(__ROOT__.'/sites/all/libraries/php/connection.inc');
Sometimes I get a WSOD when trying to run Cron, and sometimes my page styles get broken when I submit a form.
What is the correct way to include a PHP file manually in Drupal? Note that I don't want to use the 'Drupal way' to do this or to use the webform module. I want to code it manually with PHP.
The libraries directory should only be used to store files shipped as a library through the libraries module (see here https://drupal.org/project/libraries).
For both examples lets assume library.inc is our file and relative_path_to is the relative path based on the module directory to our library.inc file.
To just include a file you can use:
require(drupal_get_path('module', 'module_name') . '/relative_path_to/library.inc');
And to do it the Drupal way (https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7):
module_load_include('inc', 'module_name', '/relative_path_to/library');
Cheers,
j
Try this ways
1) require 'includes/iso.inc';
2) include_once 'includes/iso.inc';
3) include ('includes/iso.inc');
OR Drupal Ways
include_once DRUPAL_ROOT . '/includes/iso.inc';
In my opinion these are correct Drupal (7) ways to include code:
module_load_include(); function - Drupal API documentation - to include any PHP files from within your or other modules where needed,
files[] = ... in your_module.info file to autoload include classes and interfaces within your own module - Writing .info file documentation.
Libraries module provides it's own way to include external PHP files from the libraries.
The second answer is the correct "Drupal way". The one that was accepted as the answer has many potential pitfalls if locations of things change. Using drupal_get_path is the best way. This is also especially true if you are trying to include a file that may not be fully bootstrapped during a module update hook.
Related
I'm trying to bring in an outside PHP library into my WP instance. I'd like to reference it in various plugins I'm creating, but am not sure how to include or instantiate it.
Specifically, I'm looking at the Library here: https://github.com/lobostome/FurryBear/wiki
I've tried a few different ways to include it without success, including using the SPLClassLoader command described here: https://github.com/lobostome/FurryBear/wiki/Installation
I'm not sure where to put the full library and then how to get it recognized by / loaded into WordPress so that it can be used.
Any help would be appreciated. Thanks
if your writing your own plugin you can put the FurryBear code in your plugin folder and then include it manually like
require_once 'SplClassLoader.php';
// Instantiate the SplClassLoader with the location directory of the source files.
$classLoader = new SplClassLoader(__DIR__ . '/src');
$classLoader->register();
Thanks. Basically, the require_once call to SplClassLoader.php they give on the wiki, and you mention above, is how it should be done. The problem was that the files were buried deep within the default build and were meant to be referenced from the same top level directory.
I moved the SplClassLoader.php file out as well as the src folder, putting both in the top level plugins folder and all worked as expected.
Thanks again.
I have installed a security solution in my Joomla website,and it's suggest that to put the configuration.php file above the Public_html Folder,how could it be possible?
how to tell the CMS to recognize the new location?
is the solution would be valid in all versions of the Joomla CMS? ,if it's not,so please
write:
1st:Joomla 2.5 Solution.
2nd:Joomla 3 Solution.
you would need to modify the defines.php file located in the includes folder.
Specifically this line:
define('JPATH_CONFIGURATION', JPATH_ROOT);
And change JPATH_ROOT to the correct path.
But the problem with this is that you are modifying a core file so if an update changes the defines.php file it will overwrite your changes and will break your setup. You will need to reedit the file.
Also the JPATH_CONFIGURATION constant may be used for other things within the CMS that are not specifically trying to get the configuration.php file so make sure to check that it will not adversely affect other parts of the cms before doing this in production.
Alternatively you can change the frameworks.php file (also in the includes folder) directly to change from where the configuration is loaded from
ob_start();
require_once JPATH_CONFIGURATION . '/configuration.php';
ob_end_clean();
Just change the require_once line to the correct path.
Again since this is a core file it could be changed by an update. But this may also affect other parts if the config file is loaded manually in components or other parts of the cms.
Simply answer is don't do it. This would mean you would have to do what #Patrick has suggest which is correct and will work, however it means editing a core Joomla file. This is not a good idea as in your case, if you ever update Joomla, you will have to perform this change every time and it you forget (which is likely), your site will stop working completely.
I would strongly suggest you find a different "security solution" which does not involve having to modify any core Joomla files.
If you could define what you mean by "security solution", then maybe an alternative could be provided for you
I didn't dig for 'since when this has been implemented', But it can be done without changing the core.
Joomla looks for a defines.php in the root and if its present, imports it. And then if it finds a constant named _JDEFINES defined, it doesn't load the original file, effectively overriding it completely.
So, If you wish to override the defines its pretty easy and all you have to do is copy the contents of the defines.php file from under the webroot/includes/ path and paste it inside the one we created in the webroot. And you can change the following constant as per your taste.
define('JPATH_CONFIGURATION', JPATH_ROOT."/my/supersecret/directory");
Now there is one more thing left to be done and then we are good to go :)
You have to prepend the following lines to the top of our override file (the defines.php in the webroot).
define('JPATH_BASE', __DIR__);
define('_JDEFINES', 1);
This constant conveys to the framework that the defines have been overridden and to use the new file accordingly (Last time I checked, this flag/constant is checked at around 10 different places all over the framework eg. here, so its important)
Also I have seen this feature available with Joomla v2.5.0 and v3.8.8 as per your requirements in the question.
Edit: Remember you have to repeat the same procedure for administrator folder too if you want admin panel to work, and remember that administrator has its own /includes/defines.php
Hi this is my first post on here. I am trying to install the Gdata Zend Client library without much success.
I have used these resources + scoured Stack Overflow.
https://developers.google.com/gdata/articles/php_client_lib
http://jeromejaglale.com/doc/php/google_calendar_api
I want to be able to add,edit etc events on google calendar via PHP. My problem/question is i really dont understand what the include_path settings are all about and how to set them in order to make the class work. Of course i checked php manual regarding this but still draw a blank.
I have downloaded the relevant class and uploaded it to my web root. In the past i would just include a class by using php include at the top of the page and this would suffice.
I am of the understanding that i need to change the php.ini file to show php where my class is. Does this mean that i have to put my class somewhere else other than the web root.
I am terribly confused about this step and i know that if i can get it installed, actually using the class should be relatively easy.
Thanks for any help.
Welcome! Your Q is about include files rather than ZF elements.
Understanding where the include setting can be made (and subsequently overridden) is an absolute key bit of information.
You need to find out where it is on the server you are working on.
echo ini_get('include_path');
Then dash off and really, really read the corresponding manual page.
Try out including a very simple file with an echo statement, and you will regain your sanity and confidence.
http://www.php.net/manual/en/function.include.php
Get that working then have a play with this:
http://www.php.net/manual/en/function.ini-set.php
The experience how you can include a file from the same directory (not generally a good idea if that is a public webpage - inside your webroot)
Then if you want to really chase this thing down, look at where you can set this in Apache and per-directory in .htaccess files.
Finally, you can just include a file by telling include/require the exact path from the top of the tree;
include /var/www/includes/libraries/and/so/on.php;
There are SO many places you can set and override this that you are really best off finding out where the server thinks the include directory is and putting your compoenents in there:
Now, when that comes to ZF stuff, I (on Deb and Ubuntu anyhow) put the contents of
Zend Framworks version XYZ ZendFramework/lib/Zend <-that folder into:
/usr/share/php/Zend <-into this place
Then setup your autoloader and Robert is your mothers brother...
Zend/Gdata.php line 124 is like this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’)
Change that to this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’, $useObjectMapping = true)
I have a problem where I would like assistence with.
Currently on working on a new project for myself and I am trying to start with a good foundation.
I have build the following file structure (relavant part):
Class/
Class/class.filename.php
Class/class.etc.php
Func/
Func/func.filename.php
Func/func.etc.php
Config/config.php
index.php
In my config file all classes and functions are included with the require_once function (I loop alle files and directories inside func and class). In the folder class the file for firePHP is located which I include and then setup in the config.php.
In my config.php and index.php I can call this log function perfectly, but when I use it in one of the func.filename.php or class.filename.php it errors. The child (func/class) is not seeing the other included functions within config.php.
Hope somebody can help me out with this.
You func.filename.php file needs to "require_once" the file that contains the log function.
Another solution that may work for you (since you have this structure) is to have all your required files in config.php (included with require_once(<file>)). This seems to work. Then, when other files required any other function for other files, just require_once('config.php').
It is best to only include what you need, when you need it. Why spend the time loading 50 different classes/functions/snippets when you might only need 2 or 3 for the script at hand? Paring out unnecessary include statements can increase the performance of your scripts considerably.
Make use of the __autoload() function for classes. It is just super.
Chain include_once() or require_once() through your various files so that grabbing one file that you need for a given job automatically gets its own dependencies.
I would like to include a PHP program into a Joomla! article, this program calls different PHP files that are used to display what I want, I have tried to install different Plugins such as Jumi, directPHP and others, but I keep getting the following error:
Application raised an exception class EDatabaseError with message 'Cannot connect to database server:mysql error: [0: Connection error to server '' with user ''] in CONNECT(, '', '**', )
'
The program runs fine Standalone, however it does not work when I'm running it on Joomla.
The connection parameters are obtained from an include "config.php" but it seems that they won't get the includes from the included PHP file.
Also when I try to include a menu I have made, which works standalone, redirects me to the index.php of Joomla! root dir.
Thanks.
I've done things similar to this, but have had to install a couple of extensions to get them to work.
First, i use the jce WYSIWYG.
then installed place anywhere (which lets you place modules inside articles)
create a new module, type=Custom HTML
code your php there...
I know this isn't exactly what you're describing, but it's the closest i've come in my experience.
If it doesn't work right away, be sure to check for the settings withing the JCE WYSIWYG so it's not breaking your php.
hope this helps!
Try to use 'Flexi Custom Code' extension is a good one
check it: http://extensions.joomla.org/extensions/core-enhancements/coding-a-scripts-integration/custom-code-in-modules/15251
Its a module extension which you can use with 'module anywhere' to place in the 'content area'(article). For calling different php file you can use the php include or required once code
Please feel free to ask if any doubts are there....implementing this