PHP include() for WordPress plugin- Error/Warning Message - php

This is my first post on stack overflow, so I apologize if I have violated etiquette or otherwise did something dumb. I'm also a novice programmer as well, so I apologize in advance if this question seems dumb.
Now on to the main point: I'm trying to create a WordPress plugin for a website while our main programmer is on vacation. I've had some success, and now what I need to do is call simplemodal-login from the plugin after the user does a certain action on the webpage. Currently, I am trying this in order to include the files, which are in a separate directory one level up from the plugin in file(I'm going to block out files names to preserve anonymity):
include ("../simplemodal-login/simplemodal-login.php");
I've also tried prefixing the simplemodal-login directory part with /../, ../../, and /../../ . I've also tried
include ("{$_SERVER['DOCUMENT_ROOT']}/[project name]/wp-content/plugins/simplemodal-login");
and some variations of it. Nothing has worked. I keep on getting variations of this error message:
Warning: include(../simplemodal-login/simplemodal-login.php) [function.include]: failed to open stream: No such file or directory in /home/[user name]/public_html/[website name]/[project name]/wp-content/plugins/[my plugin]/[plugin file] on line 29
Warning: include() [function.include]: Failed opening '../simplemodal-login/simplemodal-login.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/[user name]/public_html/[website name]/[project name]/wp-content/plugins/[plugin name]/[plugin file].php on line 29
What am I doing wrong? I simply want to run the simplemodal-login plugin after the user sees the webpage a certain amount of times. How can I include the file properly? I apologize for my inexperience - I've taken an intro course in Java at college (which I did well in) and I've learned a little (I mean LITTLE) bit of PHP this summer as an intern. If anyone could guide me so I don't get that error message when I try to include the files, it would be very helpful.
Thanks!

If the file you are trying to include is one directory level up from the plugin file you are writing the code in, a portable solution would be to use:
include dirname(__FILE__) . '/../file.php';
dirname(__FILE__) returns the path of the file that that statement is executed in (regardless of whether or not it has been included itself). I use this in Wordpress plugins without any issues.
Hope that helps.

Related

Warning: include(../includes/db.php): failed to open stream:

This is the problem I am facing now Can you please suggest me to get rid of this problem. Before I have not get this problem It was working nicely now I don't know what happened the path is correct only. Anyone please solve it...
Warning: include(../includes/db.php): failed to open stream: No such
file or directory in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 3
Warning: include(): Failed opening '../includes/db.php' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 3
Warning: include(functions.php): failed to open stream: No such file
or directory in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 4
Warning: include(): Failed opening 'functions.php' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\portfolio\admin\includes\admin_header.php on line 4
Based on the error posted, it seems to be that the include_path is referring to the path (include_path='C:\xampp\php\PEAR') while the PHP files are included in some other folder.
You need to update the include_path variable to point to the correct location. You can find this in the php.ini file.
Computers don't lie, if it says it doesn't see it, then it is not there
Make sure you are making it look at the right place, and if I will advise you do it manually also to confirm
I also suspect it is an issue arising from using relative paths. Please make sure the path is right considering where you are currently calling the file from
e.g calling include(../includes/db.php) from index.php is different from calling it from admin/index.php
EDIT
If your are calling from C:\xampp\htdocs\portfolio\admin\includes\admin_header.php to C:\xampp\htdocs\portfolio\includes
then
include(../../includes/db.php);
is the right syntax to use
Hope this Helps
I have had this the same issue before and it ended up being something dumb on my part i forgot to start the MySQL on the XAMPP control panel. I'm sure i'm late the the party on the answer but hope it can help someone later down the road.
a bit late but i just reply for those who might get the same error .
assuming you are storing header.php in your includes directory and its not beside your index.php,so when you include bd.php into your header.php and then including your header.php into your index.php pay attention that apache knows index.php direcotroy as your pwd so if you have set that relative path for db.php based on header.php directory its not gonna work.you can use absolute path to avoid this or just set you relative path based on relative location of db.php from index.php
It look likes, the files you are trying to include in your php codes, are not on that place. Try to save all your include files in a particular folder and then call it wherever you want to call it.
suppose your index.php file is in xampp/htdocs/your_folder_name/index.php , then simply create a folder name whatever you want to name it inside your folder name your_folder_name/your_new_folder and save all your files which you want to include, as if you are saving db.php then call it on the top of your page as:
<?php include("your_new_folder/db.php");?>
This has to do with how you are coding your relative path.
I am assuming you got this error when your app made reference to admin_header.php, which has an include() to try and call {document_root}/porfolio/admin/includes/db.php. My educated guess is it contains your implementation of the connection string to the database.
The correct relative path should be: include('../../includes/db.php');
This link should give you a better understanding of relative paths (and paths in general).
https://phpdelusions.net/articles/paths
In case this helps anyone else, I had this problem and then realised WAMP had crashed (or maybe I didn't start it). Either way, WAMP wasn't running on my local machine, so... doh.
This should be the very first thing you check (in line with, "is it plugged in?").
Try removing relative paths ("../")
e.g. change include(../includes/db.php) to include(includes/db.php)

Cannot include php files from root using subdomain

I have recently made an API now for that api i have made a subdomain however it broke my php file includes
include $_SERVER['DOCUMENT_ROOT'].'/core/init.php';
now my subdomain i am using is api.website.com and i need to include something from website.com when i try to include its giving the root of the subdomain,
Summary
When i try to include init.php i get
Warning: include(/home/website/public_html/API/core/init.php): failed to open stream: No such file or directory in /home/website/public_html/api/apex/getusers.php on line 2
When i actually need
Warning: include(/home/website/public_html/core/init.php): failed to open stream: No such file or directory in /home/website/public_html/api/apex/getusers.php on line 2
I apologize for my grammar if need be.
Changing your include path to
include ("../../core/init.php");
will fix your problem. However, this is not a recommended approach.
You should think about using a dependency manager like composer. ( https://getcomposer.org/ ). Then your included files will always be under a directory like
/home/website/public_html/vendor/core
and there will be less of a chance of your application breaking if the files in /home/website/public_html/core change.
This should work.
include("../core/init.php");
Use following code
include_once $_SERVER['DOCUMENT_ROOT'] . '/../core/init.php';
visit https://developer.hyvor.com/php/including-files-in-root-with-subdomain.php to learn more.

PHP include file behaves strange after a JQuery call

It's been a while since I came across with this site. So far I've been viewing questions asked by different users and using the tips handed by the commenters in order to solve my issues and code problems. Sadly I couldn't manage to find a relative post to my current issue, hence I'm posting my very first help thread.
I'm using and 'include.php' file in my application which includes all the relevant files to my project. The file is located in the sub directory 'includes', therefore its path is 'includes/include.php'. Everything worked fine since I have started codding until my last decision to start using JQuery in my codes. Since I'm pretty new to both of these languages, I came across an annoying problem when trying to load a certain PHP file into one of my HTML divs. Instead of spending my time by verbally explaining to you what the problem is, I'll simply go ahead and show it to you.
In the top of my PHP files I use the next code in order to include my main include file:
require_once "/includes/include.php";
The content of my 'include' file is simply including the rest if the files, it goes like this:
require_once 'user.php';
require_once 'db.php';
Everything works fine with these files. Now I have a different include file, a script file, that I use in the middle of my code to post a certain HTML script. I use the next code in order to load it in my main PHP file:
<?php include '/includes/script_files/loadAdminMessageClass.php'; ?>
Everything works just fine so far as well. The problem starts when I try to refresh a certain div using JQuery and re-load the same 'loadAdminMessageClass' file to that div. This is the code line I use in order to reload the div:
$('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
Here starts the problem. From this point onwards, the line 'require_once '/includes/include.php' in the first 'loadAdminMessageClass' doesn't work. The next error comes up in my div after the reload process:
Warning: require_once(/includes/include.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\includes\script_files\loadAdminMessageClass.php on line 8
Fatal error: require_once() [function.require]: Failed opening required '/includes/include.php' (include_path='.;C:\php5\pear') in C:\wamp\www\includes\script_files\loadAdminMessageClass.php on line 8
I've been searching this problem for ages and nothing was like the answer I was seeking for. Seems like when I call the 'loadAdminMessageClass.php' file from JQuery, it won't treat the 'require_once' call the way it should.
--
I'd be glad to have a response to this issue of mine. Thanks.
I'm actually surprised this line works:
require_once "/includes/include.php";
This refers to the root of your server's file system. You're mistakenly assuming PHP respects the document root of your website when constructing include paths.
You have to explicitly retrieve $_SERVER['DOCUMENT_ROOT'] to find out where the website is running (something like /var/www/mywebsite or /home/user/public_html usually), and then construct the paths from there:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
require_once ROOT_PATH.'/includes/include.php';
You should try this one
require_once dirname(__FILE__) . '/includes/include.php';

Web application not finding file in what appears to be the correct path

I tried to reorganise my facebook application files into better folders and now when I try to load my application on facebook it throws the following error:
Warning: require_once(../scripts/mysql_connect.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
Fatal error: require_once() [function.require]: Failed opening required '../scripts/mysql_connect.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
The second error is a result of the failure of the first one I think. The weird thing is that my program works perfectly fine when I'm accessing through the localhost url in the browser but when I run it through facebook it throws this error.
In my root folder I have a "Model" folder where the Database.php file is kept and a "scripts" folder where the mysql_connect.php file is kept. I have tried clearing my cache on the browser and using private browsing but it is still throwing this error.
Does anyone know what could be wrong?
EDIT: Sorry this does not seem to be working in localhost either now! One thing I think is weird is that it says "failed to open stream : NO SUCH FILE OR DIRECTORY IN" and then lists the database.php path. Why is it looking in there?
I use the line require_once('../scripts/mysql_connect.php'); at the top of my Database.php file to include it.
It seems that on the server there is a different working directory. Something like the following owuld be a quick fix
dirname(__FILE__) . '/../scripts/mysql_connect.php'
Ok I've fixed this, but I'm still unsure why it was a problem. In my root folder I have two folders: scripts, and Model. I have a database.php file in my Model folder which has the following line of code in:
require_once('../scripts/mysql_connect.php');
For some reason this did not work, but by replacing the line with:
require_once('/scripts/mysql_connect.php');
This started working again! Just thought I would post the solution. Does anyone know why the other way failed? I thought the ".." just makes it go up one level in the folder hierarchy i.e. to the root folder in this case, and then follow the path which would have taken it to the file...
EDIT: This appears to only have solved it for localhost and not through Facebook...
Do you have permission to edit your php.ini file? One solution would be to find the line with the include path (you'll notice it has C:\xampp\php\PEAR in there already). Include the full path to the "scripts" folder you're referencing, then you can change the php code to read:
require_once('mysql_connect.php');
PHP will be able to find this file since it's in the include path. Just make sure that if you start adding several include paths that you shouldn't have a file named mysql_connect.php in two or more of the different include paths because you'll most likely get errors. Hope this helps. It definitely makes for some cleaner code.

require wp-load.php 3 directories back

I'm trying to include a file (/wp-load.php) at the beginning of the /html/ directory. I'm trying to include it from /wp-content/themes/pw-steel-orange/index-load.php, but I always get the error message
Warning: require_once(../wp-load.php) [function.require-once]: failed to open stream: No such file or directory in /nfs/c07/h01/mnt/102799/domains/platyworld.com/html/wp-content/themes/pw-steel-orange/index-load.php on line 1
Fatal error: require_once() [function.require]: Failed opening required '../wp-load.php' (include_path='.:/usr/local/php-5.2.6-1/share/pear') in /nfs/c07/h01/mnt/102799/domains/platyworld.com/html/wp-content/themes/pw-steel-orange/index-load.php on line 1
Am I doing something wrong? I though ../ brings the includes to the beginning directory
Sorry if this is a duplicate, I couldn't find something related to this in my searches...
You can issue the following command to see where you are pulling the file from (where you are at):
// get your current working directory
echo getcwd();
Then include the file accordingly.
// file is 3 dirs back
include '../../../wp-load.php';
If you are using a framework like CodeIgniter, for instance, there will be an index.php file at the very start of your application that will be calling all other code. So you would have to include that file according to that file.
Most frameworks use something they call a BASEPATH that is the current actual full server path to the site. This may prove very useful when migrating your site to another destination.
Here is a semi-automated way to do this:
$incPath = str_replace("/wp-content/plugins/PLUGIN_NAME","",getcwd());
ini_set('include_path', $incPath);
include('wp-load.php');
Regardless though, it's still a bad idea to include wp-load.php. ( if this link is ever deleted, See that page here )

Categories