i tried the following code to import two files
<?php echo "php";
require('../globalvasr.php') or die("error");
require('../newcosn.php') or die("error2");
$config = new GlobalConfigs();
?>
It does not shows error and it just simply displays a blank page.Also i am unable to use the variable defined in those two files.
Like $config->DBNAME.
I dont know whats wrong in this.
Please help me find it.
Thank you.
require, in contrast to include, automatically dies and does not have a return value.
This means the or die() is bad. Better:
<?php echo "php";
require('./globalvasr.php');
require('./newcosn.php');
$config = new GlobalConfigs();
?>
Require generates a fatal error when require fails, causing the script execution to stop immediatly.
As you seem to be running in a web env, your output (all echo or print statements) is buffered until the end of the script.
So here the require fails, causing a fatal error (that should be available in the error log) before the output buffer is emptied, preventing your first "echo" to be sent to the browser. that's why you get a blank page.
Try replacing the require with an include, you will get a warning instead of the fatal error.
if you have blank page, set error_reporting: E_ALL and Display_errors: On in php.ini or put on start of your script these lines
error_reporting(E_ALL);
ini_set('Display_errors','On');
then you will see errors
Related
I have a php script in root of my site. I have added a cronjob in my cpanel.
It is working with basic database operations like shown below:
<?php
require_once "classes/class.database.php";
$db = new database;
$db->connectToDB();
$data = date("Y/m/d H:i");
$res = $db->insertRow("cron",array("datetime"),array($data));
echo $res;
?>
In same file I have replaced these codes with codes below which are real codes that I want to schedule but it is not working. If I enter manually, it works but by this way it doesnt working.
Real Codes:
<?php
require_once "/home/domain/subdomain.domain.com/share/share.php";
$share = new share;
$share->sharePosts();
?>
I dont think there is an error in my code because it works manually however I want to be sure about that. Can I log output of this file?
Thanks in advance.
I have finally solved the issue.
I have enabled error logging with codes below.
error_reporting(E_ALL);
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
I have tried to mail myself (it is cpanels property) and just echo something.
Mail came succesfully. Then I wrote wrong code (I wrote wrong path to "required_once"), when I execute manually, gives fatal error. Fatal errors not mailed. After that I replaced "require_once" with "include" to avoid fatal errors, then it noticed an error but this time it mailed to me. Error was "No such file." Then I tried path like that "/home/domain.com/yourcron.php" and no errors. In conclusion, all paths must be like "/home/domain.com/yourcron.php".
I have a problem when testing my php code file. Despite having error handling in test mode (I mean, 8191 E_ALL), the errors of the code that is added within an included file are not browsed, and the execution of the script is stopped. If the included file is large, I spend many time until I find the error, what can I do to display the error even if it is inside an included file?
Example:file code.php
echo '<p>beginning of the script</p>';
include_once('code2.php');
echo '<p>end of the script</p>';
The file code2.php has a parse error:
echo 'hello'
The script is stopped before browsing "end of the script", but no error messages are browsed.
Many thanks,
This code does not execute bcoz php thinks code2.php is a variable, to fix that surround it with single quotes 'code2.php' .
In order to see errors messages make sure you have <?php not just <? and error_reporting(E_ALL);
Here is the code I have been struggling for several hours:
if ((require $_SESSION['ROOT_PATH'] . '/templates/core/menu_js.php') == 'OK') {
echo 'OK';
} else {
echo 'KO';
}
If I understand the PHP documentation on the "require" directive correctly, the "KO" should never be written because, if the require doesn't work, an error is raised.
In my case, the "KO" is always displayed even with error tunning :
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
Note that the same require (or include) works perfectly in other pages of the site.
EDIT
The if structure has been added after watching the documentation.
At first, I had a single line :
require $_SESSION['ROOT_PATH'] . '/templates/core/menu_js.php';
As I checked that this line was not working, I added the if.
By the way. Required page (when it works) adds a script tag to calling page that I never see on this unique page. On any other page where this require is used, the script appears on output.
So my question should be "if the output of the required php file is not displayed, why is there no error raised ?"
According to the documentation
Successful includes, unless overridden by the included file, return 1... Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function.
So your file isn't returning 'OK'. It's returning either 1 (for success) or a custom value.
require is a language construct, not a standard function. By using require you're already indicating the code should fail if the file isn't found. Most likely you do not need to check the return value.
This only works the way you expect if menu_js.php contains return 'OK';. Otherwise the require works just fine, but the returned value is not "OK", which is why your "KO" condition is triggered. require doesn't return 'OK'. If the require does not work, the program is halted immediately, your false condition will never be hit the way you think it will.
There are some mistakes here:
According to the PHP documentation:
require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error.
A fatal error will stop your script execution.
require returns what the included file returns:
// a.php
return 'test';
// b.php
$result = require 'a.php';
echo $result; // will display 'test'
So, don't test the return value of require! Use is_file(), then if true, require that file!
You cannot use require in if
Use this
if(file_exists($_SESSION['ROOT_PATH'] . '/templates/core/menu_js.php'))
{
echo 'OK';
} else {
echo 'KO';
}
While it's true that the require will not return anything, making the conditional always return 'KO' since the left hand value in the parenthesis will never equal 'OK', you can check to see if the file has failed by setting the display_errors ini value in this script temporarily On.
Since you have your error reporting set to catch this, your display_errors is probably off, therefore not showing you this. Set it for this script like this:
ini_set('display_errors', 'on');
I am running a PHP page and as soon as I introduce calls like this: $_GET('') then everything goes wrong and I get an error 500.
This code goes not work:
echo $_GET('username');
echo $_GET('password');
?>
This code does:
<?php
phpinfo();
?>
The above code has syntax errors - you need to use square brackets.
The web server's error logs will show you those errors if you have access to them.
Use this:
echo "Username: ".$_GET['username']."<br />Password: ".$_GET['password'];
Since $_GET is a array and not a function, you need to use [square brackets] instead of (normal brackets) to retrieve the data out of a array.
To figure out what the problem is you need to turn on php error reporting. You do this by running this the first thing you do in your php-file:
ini_set('display_errors',1);
error_reporting(E_ALL);
E_ALL means the interpreter will show you errors, warnings and notices. After that, everything will be pretty obvious since php will tell you what went wrong.
Is there any difference between them? Is using them a matter of preference? Does using one over the other produce any advantages? Which is better for security?
require will throw a PHP Fatal Error if the file cannot be loaded. (Execution stops)
include produces a Warning if the file cannot be loaded. (Execution continues)
Here is a nice illustration of include and require difference:
From: Difference require vs. include php (by Robert; Nov 2012)
You find the differences explained in the detailed PHP manual on the page of require:
require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
See #efritz's answer for an example
Use include if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed.
Using require means your script will halt if it can't load the specified file, and throw a Fatal error.
The difference between include() and require() arises when the file being included cannot be found: include() will release a warning (E_WARNING) and the script will continue, whereas require() will release a fatal error (E_COMPILE_ERROR) and terminate the script. If the file being included is critical to the rest of the script running correctly then you need to use require().
For more details : Difference between Include and Require in PHP
As others pointed out, the only difference is that require throws a fatal error, and include - a catchable warning. As for which one to use, my advice is to stick to include. Why? because you can catch a warning and produce a meaningful feedback to end users. Consider
// Example 1.
// users see a standard php error message or a blank screen
// depending on your display_errors setting
require 'not_there';
// Example 2.
// users see a meaningful error message
try {
include 'not_there';
} catch(Exception $e) {
echo "something strange happened!";
}
NB: for example 2 to work you need to install an errors-to-exceptions handler, as described here http://www.php.net/manual/en/class.errorexception.php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
<?PHP
echo "Firstline";
include('classes/connection.php');
echo "I will run if include but not on Require";
?>
A very simple Practical example with code.
The first echo will be displayed. No matter you use include or require because its runs before include or required.
To check the result, In second line of a code intentionally provide the wrong path to the file or make error in file name. Thus the second echo to be displayed or not will be totally dependent on whether you use require or include.
If you use require the second echo will not execute but if you use include not matter what error comes you will see the result of second echo too.
In case of Include Program will not terminate and display warning on browser,On the other hand Require program will terminate and display fatal error in case of file not found.