what's wrong with my require_once path? - php

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

Related

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

Unable to get correct path in PHP due to server settings

I am not an expert on servers and one of my friends wanted to use some of my PHP scripts I wrote, but has a problem with his server and I was unable to find how to fix this.
Basically the problem is in generating paths to files.
In my script, I use the following variables to get the path:
$path = "/template/"; // path to the template folder
$baseURL = $_SERVER['DOCUMENT_ROOT'].$path;
Then I include my pages for example like this:
include($baseURL."scripts/functions.php");
On my server, this works fine and when I tried to echo the baseURL parameter I get this:
/data/web/virtuals/104571/virtual/www/template/
However, on my friend´s server, it just throws "PHP Fatal error: Call to undefined function" - because I checked and the functions.php is not being loaded,
When I tried echoing baseURL on my friend´s server, I got nothing.
So I tried viewing phpinfo() and interestingly, I did't find the DOCUMENT_ROOT there at all and the paths are just weird, the PHP error shows:
PHP Fatal error: Call to undefined function convertT() in H:\webspace\hostings\kocher.es\hosting\www\meteotemplate\index.php on line 14
As I dont really understand servers so I dont know what this means, why it has that H:/ drive there and more importantly, how to fix this....
Here is the phpinfo() displayed:
http://kocher.es/meteotemplate/
If anyone knew how to fix this I would very much appreciate it, thanks.
try __DIR__
example:
$basePath = __DIR__;
$templatePath = $basePath."/template/"; // path to the template folder
include($templatePath."/scripts/functions.php");
__DIR__ will return file base directory and its different in different files which mean you must be careful from when will call it
You can try with dirname(), pathinfo() and realpath() to determine the base path instead using $_SERVER['DOCUMENT_ROOT']
Please add document root path in httpd.conf file of apache
Or try _SERVER["CONTEXT_DOCUMENT_ROOT"]

include an php file with included files

Here is directory structure
/global.php
/includes/class_bootstrap.php
/includes/init.php
/plugins/myplugin.php
Here is codes in these files
/start.php
require('./includes/class_bootstrap.php');
/includes/class_bootstrap.php
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once(CWD . '/includes/init.php');
/plugins/myplugin.php
require_once(dirname(__FILE__).'../global.php');
And as far as I am understanding the problem is in class_bootstrap.php file coz it's generating wrong path for CWD
here is error:
Warning: require_once(C:/wamp/www/vb4/plugins/includes/init.php) [function.require-once]:
failed to open stream: No such file or directory in C:/wamp/www/vb4/global.php on line 35
As you can see "C:/wamp/www/vb4/plugins/includes/init.php" is wrong path.
The MAIN PROBLEM is that I can edit only myplugin.php file other files are CMS core files and should not be changed.
How can I fix this issue?
If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.
Instead, base it on a known path.
/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:
define('CWD', realpath(dirname(__FILE__) . '/../') );
dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins
Phew.
From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php
Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.

PHP is including relative to the URL path and not the file path, how do you change this?

I have a PHP file /a/b/file.php with the line
require("../connect.php");
In connect.php there is a line
require("../config.inc.php");
This all works fine on my local server. There are a bunch of files using connect.php and they all work fine too.
However on the hosting site /a/b/file.php throws an error:
Fatal error: require() [function.require]: Failed opening required
'../config.inc.php' (include_path='.:/usr/local/php5/lib/php') in
/******/connect.php on line 3
I suspect that even though connect.php is in another folder, it's looking for it relative to /a/b. Is there a php.ini setting to change this?
Why dont you use something like:
require( dirname(__FILE__).DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."connect.php");
This way you will avoid problems like when you develop an app on a Unix-Like system and deploy it on a Windows systems, or viceversa.
I don't know if there's a php.ini setting, but usually I use $_SERVER['DOCUMENT_ROOT'] for my included files.
require $_SERVER['DOCUMENT_ROOT']."/path/relative/to/document_root/file.php";
There are some cases where a relative path is better, but most of the time the files you want to include will stay in the same dir.
Usually I personally make a file called global.php (which is in the root directory of the project) where I define() a constant, include libs and so on.
<?php
// ...
define('APP_INCLUDE_DIR', dirname(__FILE__) .'/');
// ...
?>
Afterwards I include that file in all other files located in the same directory (e.g. index.php with require('global.php'). Now that everything is executed at that directory level you can use the constant APP_INCLUDE_DIR in every file which gets included.
<?php
require('global.php');
// ...
require_once(APP_INCLUDE_DIR .'a/b/c/connect.php');
?>
And in a/b/c/connect.php you could write for example
<?php
// ...
require_once(APP_INCLUDE_DIR .'a/b/config.inc.php');
?>
Check your include_path in your PHP.ini file.
http://php.net/manual/en/function.set-include-path.php
For example, on my local server (XAMPP) it looks like this: `include_path .;C:\xampp\php\PEAR
You can do a phpinfo() to find out also.

spl_autoload fails when script ran from command line

I know this has to do with the path not being quite right but it has me baffled. I can run my script with no problems at all from the browser but when I do to the exact same spot from a shell, spl_autoload complains and dies:
Fatal error: spl_autoload(): Class db
could not be loaded in...
I am using the absolute path from the root directory, echoed to screen and pasted it into a shell and verified that it is good. Please... what am I missing??
Try using the __DIR__ constant to locate the files, CLI PHP doesn't uses the same working dir.
Use something like this:
function __autoload($class)
{
require_once(dirname(__FILE__) . '/path/to/libraries/' . $class . '.php');
}
you can usually grab your root directory for the project with something along the lines of :
// The file that defines this is 2 directories below root, hence the ../ changes.
define('PATH_ROOT', realpath(dirname(__FILE__) . '/../../'));
Once you have your root path you can modify your include path, using set_include_path. (remember to include get_include_path when you set it otherwise you'll lose the defaults)
once thats sorted, just setup your autoloader assuming against the root dir and you should be fine, since its a bit more concrete than relying on relative paths which can change according to the working dir.

Categories