I have placed my own php file in /sites/all/modules/<myfolder> and call it via $.post(<myphppage>) from a static html page (javascript function) also in /sites/all/modules/<myfolder>.
However, the php does not appear to be executing as expected, but is getting called (access logs in Apache HTTPD show it is post'ed to).
If I try to manually request the same php page, I receive this error:
Fatal error: Call to undefined function db_insert() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
The echo statement I have in the php page is outputted properly above this error, and simply uses $_GET['paramname']. (And _POST, changed for testing direct request) to print a few query string parameters for debugging).
Also, when I added a call to watchdog, I receive:
Fatal error: Call to undefined function watchdog() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
Is it not possible to access the PHP page directly? Or is there a Drupal library I need to import? Or am I just plain missing something else with how Drupal works?
You are getting those errors about undefined functions because your PHP file is independent from Drupal and it is not bootstrapping it.
The Drupal way of doing what you are trying to do is to create a module (see Creating Drupal 7.x modules, and Creating Drupal 6.x modules) that defines a URL it handles with hook_menu(); that URL is then used with $.post().
As example of the way of bootstrapping Drupal, see the content of the index.php file that comes with every Drupal installation. (The following code is the one used from Drupal 7.)
/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
The code changes from Drupal 6 to Drupal 7, but drupal_bootstrap() is defined in both Drupal versions.
The correct way is to create a module, anyway. For a list of reasons why a module should use a PHP file that bootstraps Drupal, see Are there cases where a third-party module would need to use its own file similar to xmlrp.php, cron.php, or authenticate.php?
Related
Im doing a direct .php call (ajax). Due to my classes, loading system, I would need to load wp-load later, not at the start.
So, if my file has only this:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
it runs ok. But if its wrapped inside a class:
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
}
}
(new Paff())->x();
it dies with a fatal error:
PHP Fatal error: Call to undefined method stdClass::escape() in /var/www/wp-content/sunrise.php on line 11
I cant see any reason why!
It seems like you're trying to load a WordPress multisite outside of the normal parameters (judging by sunrise.php being the issue here). Try loading this file instead:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
This skips index.php which defines the WP_USE_THEMES constant and assumes you're not loading the front-end of the site (at least not traditionally, which you're not).
That should load everything and in the right order for you now.
I also could not see anything wrong with your code. So I tested it with one change just for confirmation that it does work.
<?php
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
echo 'wp-load.php included. ABSPATH=' . ABSPATH;
}
}
(new Paff())->x();
?>
It runs without error! and displays the following in the browser.
wp-load.php included. ABSPATH=D:\bronce/
I displayed the ABSPATH just to confirm that wp-load.php was included as it defines ABSPATH.
So this works as it should on my installation which is very standard as I just installed PHP a few weeks ago and I know I didn't change the defaults very much. So why does it not work on your system? It must be configured different. Is your system running any pre-execution handlers?
I just did a search for 'sunrise.php' which I originally thought was the name of your code. It may actually be an infection (or not) see https://premium.wpmudev.org/blog/removing-backdoor-exploits/. I think given its name it is configured to run as a pre-execution handler. Some plugins legitimately install pre-execution handlers, e.g. WordFence. Unfortunately, I did not install WordFence this time and I don't recall the name of its pre-execution handler.
I just read the source code to WordFence. It does install a pre-execution handler but its name is ABSPATH . 'wordfence-waf.php'. However, WordFence also has a reference to '.../wp-content/sunrise.php' apparently executed before multisite loading. Are you using WordFence? Is your site a multisite?
The important point is: The problem is not in your code but in a pre-execution handler. A pre-execution handler can be a huge amount of PHP code that can significantly change the PHP execution environment, which is why plugins such as WordFence use it to monitor your code execution.
i created a script in order to insert some articles via a php script. This script is working on my local machine (xampp), but when i deploy the whole joomla project to my web server i get the following error message:
Error displaying the error page: Application Instantiation Error: Application Instantiation Error
By adding some echo calls, i was able to find the line which causes the error:
$app = JFactory::getApplication('site');
Now i am wondering how to fix this behaviour and make my function also run on the web server.
Below i will provide my system informations and the beginning of my php function until the line, which causes the error message:
Systeminformations
joomla version: 3.6.5 Stable
db version: 5.6.34-79.1-log
php version: 5.6.30-he.0
web server: Apache
PHP snippet
<?php
echo "STARTING; ";
// get db connection
include('../includes/mysql.inc.php');
// get all sql querys
include('./autoNewsQuerys.inc.php');
/**
* Prepare joomla framework to insert article correctly
*/
if (!defined('_JEXEC')) {
define('_JEXEC', 1);
define('JPATH_BASE','/is/htdocs/wp1088688_4E1H7PYJFK/www');
require_once(JPATH_BASE . '/includes/defines.php');
require_once(JPATH_BASE . '/includes/framework.php');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}
echo "searching app;";
$app = JFactory::getApplication('site');
echo "Found app;";
Full browser output
STARTING; searching app;Error displaying the error page: Application Instantiation Error: Failed to start the session because headers have already been sent by "/is/htdocs/wp1088688_4E1H7PYJFK/www/1500AutoNews/autoNews.php" at line 3.
So as you can see the error is caused by the JFactory call. 'Found app' is never printed out.
Just copy the Joomla index.php file to another file and then replace the $app->execute code with your code.
Note: You don't need to include any MySQL library - since you are loading the Joomla environment then you have access to the JDatabase class, which is a database abstraction layer.
Another note: this file autoNewsQuerys.inc.php likely contains some issues (maybe you are trying to instantiate the app there, but the Joomla environment hasn't loaded yet). You should move it to the end.
if it was working on localhost and it is not working on Web server then the biggest issue is with defining the base path. Give a hardcoded base path of your joomla installation. I suggest you to check your base path using a php script. Create a php file and name it path.php. Put it in the Joomla main folder. Content of the file
<?php
echo "The Base path is ".getcwd();
?>
Once you get the path just change the Base_Path. For ex:
define('JPATH_BASE', '\var\www\joomla');
I have a drupal site and inside i have a wordpress blog like that drupal_site/wordpress_blog.
When i did the implantation all seemed to work fine but now i am getting the following error
Fatal error: Cannot redeclare timer_start() (previously declared in (path_of_mysite)/includes/bootstrap.inc:456) in (path_of_mysite)/blog/wp-includes/load.php on line 197
I have renamed the timer_start() but then i got another same error for another function.
So the problem is that drupal's functions "overrides" wp functions and renaming every wp core function not working.
Also i have tried at least the errors to not shown at my page, i have disable them from my drupal dashboard, i have tried via .htaccess,via index.php,also via phpmyadmin but this error keeps showing.
Update:I found the solution for the errors to not be shown,i just have added
error_reporting(0);
to wp-config.php.So at least there is something.
Any clue for the solution?
Drupal and Wordpress are trying to declare the same function names. To solve this type of problems PHP had introduced namespaces in PHP 5.3, more info: http://www.php.net/manual/en/language.namespaces.php, but saddly Drupal and Wordpress did not use namespaces.
My recomendation to avoid this problem is to install Drupal and Wordpress in different subdomains, so if your domain is drupal_site.com, use blog.drupal_site.com for Wordpress installation.
I'm rewriting a component, and I've been following
a tutorial here: http://docs.joomla.org/Managing_Component_Updates_with_Joomla!1.6_-_Part_3
In the install file code they compare the existing installed component with the new
install file's params. The code to get the installed version is this:
$oldRelease = $this->getParam('version');
When I run this, it dies with the following:
Fatal error: Call to undefined method
com_mycomponentInstallerScript::getParam()
I thought that in Joomla 1.6+ the params were accessible
automatically via the getParam?
Thanks for help.
If you look at the full script you linked to, they added a function getParam to the class. since com_mycomponentInstallerScript isn't extending an existing Joomla class, that function doesn't exist unless you define it.
I am learning how to use SWIG, and I am writing a php wrapper for a C library. The extension successfully compiles, but when I try to call the function I get this error:
php: symbol lookup error: /usr/lib/php5/20090626+lfs/fact.so: undefined symbol: fact
Your problem is probably due to a mismatch in the name of the module (see %module, or passed on the command line) and the name of the .so file you are generating.
PHP, or any system that accepts loadable binary modules, is going to make certain assumptions about the name of the entry point into the library it is trying to load. PHP seems to be assuming that the file name (fact.so) is going to contain a function called "fact".
When you run SWIG, explicitly setting the module name to "fact" will probably solve your problem. If not, posting the generated SWIG source file could help us debug your problem.