Question

So, problem issue is:

Error reporting is not enabled in php.ini file, but when i developing something i use simple if and else checks. But as i was unable completely to even setup some session, i come down to basics, and wrote:

<?php

if (require_once('classes/sessionHandler.class.php')) {
    echo "file found";
}else{
    echo "file not found";
}

and i got message:

file found

than i wrote:

<?php

if (require_once('abc')) {
    echo "file found";
}else{
    echo "file not found";
}

and i did not get anything, even file not found message.

What should be wrong in php setup?

PHP version:

PHP Version 5.4.4-14+deb7u5

OS:

Kali linux 1.0.5

Was it helpful?

Solution 5

It's because require_once throws a fatal error when executing the script, which stops the rest of the execution. You need to use include_once or file_exists functions instead.

$file = 'abc';

if(file_exists($file)) {
    require_once($file);
} else {
    echo 'File not found';
}

It's better to have error_reporting set to on when developing. Thus setting ini_set('display_errors', true)

OTHER TIPS

require(_once) generates a fatal error if the file is not found. If you always want output, try using include(_once) instead.

require_once() always returns 1 if file is found, and halts execution with a fatal error if file is not found. Hence you don´t see error output - your script died.

Use include_once instead.

require or require_once usually falling with fatal error when file not found or not accessible for reading. In your case just simply turn on errors output and you will see your problem.

If you want to allow missing file, you have to use include_once('file'); , that will generate a warning saying the file is missing, but this will not stop the script.

Or, you can just check if the file exists as follow:

<?php

$file = 'abc';
if (file_exists($file)) {
  echo "file found";
  require_once($file);
}else{
  echo "file not found";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top