Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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') ;
like image 549
Spilot Avatar asked Nov 06 '13 21:11

Spilot


People also ask

What is the difference between include_once and require_once in PHP?

include_once will throw a warning, but will not stop PHP from executing the rest of the script. require_once will throw an error and will stop PHP from executing the rest of the script.

How does require_once work in PHP?

The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.

What is the main difference between require () and require_once ()?

The require() function is used to include a PHP file into another irrespective of whether the file is included before or not. The require_once() will first check whether a file is already included or not and if it is already included then it will not include it again.


7 Answers

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.

like image 133
Veelkoov Avatar answered Oct 09 '22 03:10

Veelkoov


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.

like image 29
Daniel P Avatar answered Oct 09 '22 02:10

Daniel P


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.

like image 21
Giacomo1968 Avatar answered Oct 09 '22 03:10

Giacomo1968


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
like image 24
Dubas Avatar answered Oct 09 '22 04:10

Dubas


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
like image 25
Pierre Avatar answered Oct 09 '22 02:10

Pierre


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");
like image 37
r3wt Avatar answered Oct 09 '22 03:10

r3wt


require_once JPATH_SITE.'/php-sdk/facebook.php';
like image 33
saloni Avatar answered Oct 09 '22 02:10

saloni