PHP include file behaves strange after a JQuery call - php

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';

Related

Proper way to include header and functions files in PHP

I have my all files in public_html directory like this
global_assets
assets/layouts/header.php
assets/setup/env.php
order/index.php
order/info/index.php
Now all is working fine in order/index.php in which I am including header like
include '../assets/layouts/header.php';
Now I have tried include header in my order/info/index.php like below
include '../../assets/layouts/header.php';
Header getting included properly in this too, but its giving me error called
PHP Fatal error: require(): Failed opening required '../assets/setup/env.php' in /home/myusername/public_html/assets/layouts/header.php on line 7
I will have many similar errors because of directory structures. I think I am not following proper way to include files so I can call it properly from any directory, sub directories without any issues. Let me know if anyone here can help me for it.
Thanks a lot!
You can define and retrieve the root of your project in several ways, then you can always access php files from this root.
environment variable
include getenv('MY_PROJ_ROOT').'/assets/...';
auto-prepend-file
//php.ini
auto_prepend_file="/path/to/prepend-file.php"
//prepend-file.php
define('MY_PROJ_ROOT', '/path/to/project-root');
//any other files
include MY_PROJ_ROOT.'/assets/...';
include-path
//php.ini
include-path=".:/path/to/project-root"
//other files
include 'assets/...';

require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0

I know this error is dicussed often already, but I just can't use require or include in my function.php(xampp) without this error on the top and I don't have a php.ini file. I don't even want one, if not necessary!
I just want to include a second php file in the fuction.php. Come on that can't be so hard...
That's what I used.
require_once (get_stylesheet_directory_uri().'/index.php');
(result: http://localhost/werkstatt/wp-content/themes/twentysixteen/index.php)
I mean in this case the index.php would be inluded anyway which leads me to the second question. Why or how are some standard files like index.php already inluded?
index.php is not included at all, this is the entry point to PHP code execution and it is defined in your webserver config (not really precise, but should give you an idea).
The PHP file you want to include, is it on the same server? If so, you need to include it by its filename, and not by its URL. I understand you want to include another PHP file which is part of your theme.
I don't know much about wordpress and what helper functions it defines, but this seems to be the one you need to use: https://developer.wordpress.org/reference/functions/get_template_directory/.
If it is not placed an the same server, then you just should not do this.

Specific project PHP files returning blank pages

So, I have this project uploaded at webwire.in/alpha , but it's returning blank pages.
PHP Fatal error: require(): Failed opening required './classes/Cookie.php' (include_path='.:/usr/share/php:/usr/share/pear in /var/www/html/alpha/core/init.php on line 22
and one on line 25.
Line 21-23 code = spl_autoload_register(function($class) {
require_once ('./classes/' . $class . '.php'); //line 22
});
Line 25 = require_once ('./functions/sanitize.php');
Tricky thing about this error is that this works only for this alpha folder. It was occurring yesterday also but somehow got fixed. Now when I deleted the files and uploaded, the error is back. Yesterday, at least the alpha/admin was working, but today nothing is working in this alpha folder.
One more thing, I've uploaded the same file to a different server, and it works flawlessly. See here - crowdsourced.in/rationshop (shared hosting)
Current server is a VPS if that matters.
I am not expert here, still tried solutions as I searched, but none worked for me till now.
Any help is highly appreciated.
Pages included are included relative to the page that was loaded, not the script doing the include.
So, if you have a script in the folder: /var/www/html/alpha/admin/
And that includes the script: /var/www/html/alpha/core/init.php
And init.php includes the file ./functions/sanitize.php
Then init.php is looking relative to the folder /var/www/html/alpha/admin/ which is where the initial script was loaded. Not looking relative to the init.php script. It is looking for a file at: /var/www/html/alpha/admin/functions/sanitize.php which doesn't exist. I'm assuming it exists under /var/www/html/alpha/core/functions/sanitize.php.
The easy fix is to use __DIR__ which is a built in php magic constant that has the value of the current script (the currently included file). Inside of /var/www/html/alpha/core/init.php, __DIR__ == /var/www/html/alpha/core/. So you can include/require __DIR__."/functions/sanitize.php

How do I properly reference files in subdirectories (in xampp) for PHP, CSS, etc. on localhost?

I'm running XAMPP on win7 to test fairly simple sites using PHP/MySQL/AJAX, and have run into the problem of not being able to reference files correctly.
I know that C:\xampp\htdocs is "equivalent" to http://localhost/, and I have a project subdirectory in C:\xampp\htdocs\lorem_ipsum (which is http://localhost/lorem_ipsum/). Within that are some more folders (one for php scripts, one for css stylesheets, etc).
I'm having problems referencing files in these subdirectories.
For example, in lorem_ipsum/index.php I use the line:
require_once 'http://localhost/lorem_ipsum/php/login.php'
Which creates the error:
Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\lorem_ipsum\index.php on line 13
Warning: require_once(http://localhost/lorem_ipsum/php/login.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\lorem_ipsum\index.php on line 13
Fatal error: require_once(): Failed opening required 'http://localhost/lorem_ipsum/php/login.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\lorem_ipsum\index.php on line 13
By googling around I've gathered that it's some sort of "filesharing/security" issue, but I have absolutely no idea what that means or how to fix it (I'm still very much a beginner).
When I use the following CSS, for example:
background-image:url('http://localhost/lorem_ipsum/images/bg.png')
The background is white, not 'bg.png'. Same if I use './images/bg.png'.
I've tried all sorts of different ways to reference the files, and none have worked. I also tried taking down any firewalls and that didn't work, either.
My ultimate question is: Why all the problems having to do with file references?
Secondary questions: Are the PHP and CSS errors related, or have I screwed them both up that badly? How can I stop this from happening? Is there a setting in apache/php that needs to change, or am I just coding it wrong?
In PHP, you should reference files from the Document_Root that is specified (C:\xampp\htdocs in your case). So try
require_once "/lorem_ipsum/phplogin.php";
The url in the css should work if the file is being hosted properly. But you can also try a relative path like:
background-image:url('/lorem_ipsum/images/bg.png');
give defines of Variable to require_once command, because HTTP:// protocol is not allow on much Server on PHP ini to reduce some conflict and Trouble .
<pre>
<?
require_once(dirname(__FILE__) . "php/login.php")
//next script here.....
?>
</pre>
or maybe you want make include command :
<pre>
<?
include "php/login.php";
//do something here..
?>
</pre>
ref : http://www.php.net/manual/en/function.require-once.php / http://www.php.net/manual/en/function.include.php
and for css is :
<pre>
background-image:url('images/bg.png')
</pre>

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

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.

Categories