Specific project PHP files returning blank pages - php

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

Related

Why is Require_Once not finding a file that exists on disk

I have the following code
require_once "../pages/appconfig.php";
require_once "../freezer/data_functions.php";
Starting yesterday I'm getting the following error on the second file
failed to open stream: No such file or directory in
for both of these files. It has been working up until today.
echo realpath("../freezer/data_functions.php");
I get the correct full file path of the file that exists on disk.
I made a bunch of changes to other files but none that should affect these calls.
What could be causing this?
It turned out that as part of the appconfig.php includes I was calling a function that called
chdir(__DIR__ . "\\..\\");
and even though I changed it back again, I still got these odd issues. Removed the call to chdir and problem solved.

what's wrong with my require_once path?

I've switched my files over from a local environment to my vps and now my facebook notification isn't working even thought I'm pretty sure I've updated all the paths correctly. I've tried writing the require path numerous ways.
I'm doing a "$.post" from jquery to a php page where the facebook notification is sent and am getting this error:
<b>Fatal error</b>: Class 'Facebook' not found in
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9</b><br />
//THIS IS MY PHP REQUIRE PATH.
require_once('php-sdk/facebook.php') ;
//IN MY LOCAL ENVIRONMENT I WAS USING THIS PATH BECAUSE IT WAS THE ONLY ONE THAT WORKED. THIS DOESN'T WORK ON MY VPS THOUGH.
require_once(dirname(__FILE__).'/php-sdk/facebook.php') ;
You use require_once and have error "Class 'Facebook' not found". If you tried require_once on a file that does not exist, it would cause other error: "Fatal error: require(): Failed opening required 'php-sdk/facebook.php'". So the path is probably OK. Check if you uploaded php-sdk properly. The facebook.php might be empty.
Your error is :
Fatal error</b>: Class 'Facebook' not found in
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9
The valuable information here is :
the error occurred because the Facebook class is unknown which means that require_once did not pop this error
the error occurred on line 9 of accepted.php
Now you have to look why the class Facebook is unknown on line 9.
if you have included the facebook.php before line 9 it is probably not containing the right class. (class names are case sensitive!)
if you include facebook.php after line 9 you just have to call it earlier.
PS: posting the first 10-15 lines of accepted.php might give us enough information to pinpoint the exact problem here.
I have faced issues like this before, and the best way to handle this is to set your true filepath as a variable & prepend that to your includes/requires. Becuase the whole dirname(__FILE__) setup can act oddly in different environments especially those that use symbolic links. Explicitly stating where files are to be set is the best solution.
So let’s assume this is your codebase path; as per your example:
/home/zjkkvcxc/public_html/
Set that as a variable that all of your pages load in some way like this:
$BASE_PATH = '/home/zjkkvcxc/public_html/';
And now when you make calls to the file system for your app, do this:
require_once($BASE_PATH . 'php-sdk/facebook.php');
What is nice about a setup like this is that you can make your app portable between environments by just changing $BASE_PATH to match your local environment. Like this might be a path for a MAMP (Mac OS X LAMP) setup:
$BASE_PATH = '/Application/MAMP/htdocs/';
Regarding how odd __FILE__ can act in symlinked environments, read up here:
Since PHP 4.0.2, _ FILE _ always contains an absolute path with
symlinks resolved whereas in older versions it contained relative path
under some circumstances.
Class not found error not refers to problem loading file. You are using a require , if file not exists a require error will be raised and this is not the case.
Probably the class inside facebook.php is not called Facebook, probably is with another name or with other case like "facebook"
Fatal error</b>: Class 'Facebook' not found in
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9
Is it possible that:
your short_open_tag is enabled on your local environment and
it's disabled on your vps and
you are using <?instead of <?php in your facebook.php file
For some odd reason, i encounter circumstances where php doesn't find the file i need through conventional means. i found some code used for searching a directory and retrieving a list of files, which was advantageous in a url generation script i was writing. overtime, i have used this simple script for many tasks, such as finding files when normal require/include fails. in a pinch, this hack works for me.
Just fill in the file name $somefile and directory name $log_directory if your directory is in the base path just type it with no slashes. if its below the base path type it like this /path/to/folder
hope this helps.
$somefile = '<!--Filename You need to find-->';
$log_directory = '<!--Directory you want to search-->';
$results_array = array();
if (is_dir($log_directory))
{
if ($handle = opendir($log_directory))
{
while(($file = readdir($handle)) !== FALSE)
{
$results_array[] = $file;
}
closedir($handle);
}
}
foreach($results_array as $value)
{
if ($value == $somefile) {
$url_include = $log_directory."/".$value;
}else{
die("error file not found.");
}
}
require_once("$url_include");
require_once JPATH_SITE.'/php-sdk/facebook.php';
The require path you said is wrong:
require_once('php-sdk/facebook.php') ;
This need there has a file got full pathname: /dir/to/your/actual/include/path/php-sdk/facebook.php
And later you require file in include_path wrong, dirname(__FILE__) OR __DIR__ is directory of the file where require_once() is called, so you are looking for facebook.php in php-sdk sub-directory under you current php script.
To fix your problem, you have 2 plan
Better one, find your actual include_path and put php-sdk files in, include_path can find from php.ini, or run php -m from shell, or phpinfo(), in this way, your first require_once() will work.
Make sure php-sdk directory includes facebook.php is in same parent directory with your calling php script, this is not common usage, but will make your second require_once() work.
I suggest you to study more on php include_path configure and __FILE__ magic constant.
define('DOC_ROOT', realpath(dirname(__FILE__) . '/'));
require_once(DOC_ROOT . '/php-sdk/facebook.php');
Assuming the folder php-sdk is at the root of you website directory.
require_once('php-sdk/facebook.php') ;
This assumes that the facebook.php file is in a folder that resides in the current folder of the PHP file beind called.
Is this the case? I suspect not since you have an error!
Are you in a folder up from the root?
---ROOT
---ROOT > FOO --your script here?
---ROOT > php-sdk --facebook in here
If so...
require_once('../php-sdk/facebook.php');
would work.
Try $_SERVER['DOCUMENT_ROOT'].'/path to /php-sdk/facebook.php'
$_SERVER['DOCUMENT_ROOT'] will give absolute path for www or htdocs folder then you can provide path to facebook.php

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

PHP require_once error when including file in parent folder

I have the following file structure
-- Plugins
-- inParent.php
---- Uploadify
------ inSame.php
------ Uploadify.php
This function was working smoothly till yesterday. But now, If I try to include the inParent.php in uploadify.php using require_once('../inparent.php), It giving me a silly error..saying...
Warning: require_once(../inParent.php) [function.require-once]: failed to open stream: No such file or directory in /home/user/public_html/plugins/uploadify/uploadify.php on line 3
But the file definitely exists...
The same code when used to include inSame.php shows no error and works perfectly..
I guessed that there could be file permission problem.. But everything is fine..all the related files have 755/644 permissions.
Also, the move_uploaded_file function has also stopped working.
All the code was working fine till yesterday. I hav'nt changed anything.
I have tried everything and dont know what exactly happened..
Please help me friends.. :( :(
I've googled and found this :
require_once __DIR__.'/../inParent.php';
In my case I had to write:
require_once dirname( __FILE__ ) . '/' . '../../inParent.php';
And that worked!
This question is old, but for people like me who run across it, here is an answer from another part of Stack Overflow.
require_once($_SERVER['DOCUMENT_ROOT'] . '/inParent.php');
PHP require file from top directory
If it was working yesterday and you are certain you did not change anything, then try a server restart first.
Second, on require once try using the full path instead of .. I normally create a file with my root paths and referance those instead of using ./ and ../
In my case it is really weird:
This Works perfect:
require('../../the_folder/the_file.php');
But PHP gives an error if i write that as this:
require('../the_file.php');
Please see that in case 2 i do not go up two steps (../../) and then down to the folder, i directly go up one step (../).
Why is PHP giving the error Failed to open stream: No such file or directory in ... ? It is really weird.
This also works:
require(__DIR__.'/../the_file.php');
Thanks for the TIP.

Failed to load Zend/Loader.php. Trying to work out why?

I have inherited a client site which crashes every 3 or 4 days. It is built using the zend-framework with which I have no knowledge.
The following code:
<?php
// Make sure classes are in the include path.
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . 'lib' . PATH_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'classes');
// Use autoload so include or require statements are not needed.
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
// Run the application.
App_Main::run('production');
Is causing the following error:
[Tue Sep 02 12:58:45 2008] [error] [client 78.***.***.32] PHP Warning: require_once(Zend/Loader.php) [function.require-once]: failed to open stream: No such file or directory in /srv/www/vhosts/example.co.uk/httpdocs/bootstrap.php on line 6
[Tue Sep 02 12:58:45 2008] [error] [client 78.***.***.32] PHP Fatal error: require_once() [function.require]: Failed opening required 'Zend/Loader.php' (include_path='.:.:/usr/share/php5:/usr/share/php5/PEAR') in /srv/www/vhosts/example.co.uk/httpdocs/bootstrap.php on line 6
I don't even know where to begin trying to fix this. My level of knowledge of PHP is intermediate but like I said, I have no experience with Zend. Also, contacting the original developer is not an option.
The interesting thing is that even though the code is run every time a page of the site is hit the error is only happening every now and then.
I believe it must be something to do with the include_path but I am not sure.
for a start I think your include path should maybe have a trailing slash. Here is an example of mine :
set_include_path('../library/ZendFramework-1.5.2/library/:../application/classes/:../application/classes/excpetions/:../application/forms/');
You bootstrap file will be included by another file (probably an index.php file). This means that if your include path is relative (as mine is) instead of absolute, then the path at which Loader.php is looked for changes if the file including bootstrap.php changes.
For example, I have two index.php files in my Zend app, one for the front end, and one for the admin area. These index files each need there own bootstrap.php with different relative paths in because they are included by different index files, which means they have to be relative to the original requested index file, not the bootstrap file they are defined within.
This could explain why your problem is intermittent, there could be another file including the bootstrap somewhere that is only used occasionally. I'd search through all the sites files for 'bootstrap.php' and see all the places which are including / requiring this file.
The fact that it only happens sporadically makes me think this is less of a programming issue, and more of a sysadmin issue - if it were a defect in the implementation, you'd expect it to fail consistently considering the error is "No such file or directory". Two guesses
There are multiple front-end web servers, and one of them is mis-configured (missing the Zend Framework).
The PEAR include directory is network mounted, and occasionally vanishes for short periods of time.
It could be a more insidious file system problem, but one would think this would effect more than just one file.
I had the same problem, but problem was in permissons to files. I gave chmod for all RWX and now is everything fine.
So maybe someone else will have same problem as me, then this was solution.
Regards
It works sometimes so there isn't anything inherently wrong on the PHP end of things (if the path was wrong it would never work... but it does, yes?). So what is causing Loader.php to be periodically inaccessible? I would suspect a permissions problem. Something that is making Loader.php or the directory that it is in inaccessible. Maybe a cron job is setting/reseting permissions? Check that first. See what permissions are when it is working and what they are when it is not.
In my case the Zend/Loader.php was not in the PEAR-directory. It should be there, but my webserver was a little raw. But you can insert it in the library/Zend directory as well.
But indeed this does not answer why your problem occurs only sometimes.
I had this error as well when I was working with PHPUnit 3.5.5. My main application script loaded the zend framework fine, however the test class ran into errors.
My solution was to add the following to the test class
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . 'lib' . PATH_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'classes');
require_once 'ThemeWidgets.php';
require_once 'PHPUnit/Framework.php';
require_once '../../library/Zend/Loader/AutoLoader.php';
class ThemeWidgetsTest extends PHPUnit_Framework_TestCase
{
public function setUp() {
Zend_Loader_Autoloader::getInstance();
}
...

Categories