I have the following test code:
<html>
<body>
<p>
Hi
</p>
<?php
if (function_exists('date_default_timezone_get')) {
echo "J";
$timezone = #date_default_timezone_get();
echo "K";
date_default_timezone_set($timezone);
echo "L";
}
phpinfo();
?>
</body>
</html>
When I retrieve it through my apparently working apache I get just:
<html>
<body>
<p>
Hi
</p>
J
If I comment out the '$timezone = #date_default_timezone_get();' then I get a response with an error about '$timezone' not being defined and then the standard phpinfo() output.
All of the debugging options I've found thus far don't show me anything of interest in any log.
Running the script with php on the command line it gives no error either.
FYI: This is me attempting to figure out why my websvn suddenly stopped working, I've narrowed down the failure to this line and it fails in the sample code, so I'm feeling reasonably good about this other than why it just wont work.
Why are you keeping the # before the function call? It suppresses the error output which may be of help.
Also, what PHP version are you using? DateTime functions are only enabled by default since 5.2.0.
I wouldn't also exclude the possibility that even though date_default_timezone_get() function exists, it is not a standard one that comes with PHP, but something custom-made instead.
After commenting out the call entirely in websvn I got a new error from another line:
Timezone database is corrupt - this should never happen!
This error led me to investigate what was wrong with my timezone info and apparently all of files in /usr/share/zoneinfo/ had 640 permissions, changing them to 644 and the errors have gone.
Related
I'm completely lost with an "500 internal server error".
Yesterday I updated to PHP 7.1.2 and then there were a lot of errors.
I solved them almost all except for this strange coding error.
When trying to find the problem I echoed some output to the screen.
And this is what happens:
for($iRecordNumber=0;$iRecordNumber==$iTotalAssignments;$iRecordNumber++){
$iNextRecordNumber = $iRecordNumber+1;
echo $iNextRecordNumber;
echo $aAssignments[$iRecordNumber][2];
echo $aAssignments[$iNextRecordNumber][2];
}
With this code I get the "500-error".
When I comment out the line with echo $aAssignments[$iNextRecordNumber][2]; the error is gone and it does what it should do. Showing me a record from the nested array $aAssignments(PDO query).
for($iRecordNumber=0;$iRecordNumber==$iTotalAssignments;$iRecordNumber++){
$iNextRecordNumber = $iRecordNumber+1;
echo $iNextRecordNumber;
echo $aAssignments[$iRecordNumber][2];
// echo $aAssignments[$iNextRecordNumber][2];
}
I looked through the Backward compatabilty list but did not find anything.
And the tool codechecker says it's good too.
I dont know how this is possible.
What can I do to find an answer? Are there any programs or code checkers to check for more php 7 errors in my code. I have several php sites running and I do not want to check all code manually for errors.
I think $aAssignments[$iNextRecordNumber][2] is not always set.
You could replace:
echo $aAssignments[$iNextRecordNumber][2];
By:
echo $aAssignments[$iNextRecordNumber][2]?? '';
I hope it could help.
In PHP 7 is the error handling completly revised. A fatal error other errors no longer stops the script. To find these kind of errors, use the new error() method in the try and catch class. For example:
try{
code to test on errors
} catch(error $eCatchedError) {
echo get_class($eCatchedError)."<br>".$eCatchedError->getLine()."<br>".$eCatchedError->getFile()."<br>".$eCatchedError->getMessage();
} // End try and catch
By this way you get an more elegant way of showing an error (with css and everything)
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
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.
I am well aware about error_reporting(0); & ini_set('display_errors', "Off"); to make error messages go away.
What would be an appropriate way to do this - for a specific file or part of code only?
Surpressing errors with #'s seems like a bad idea since it apparently slows the code down...
The reason? We have a number of memcached servers in a development LAN that is really unreliable due to the network settings, thereby we are recieving errors multiple times every hour and there's nothing we can do about it except stop using memcache or turning off errors for the whole application, which would be giving us a headache - in the middle of the development stage :)
<?php
// normal code
// error_reporting returns the old error code
$old_error_reporting = error_reporting(0);
// your errorful code
// reset error_reporting to its old value
error_reporting($old_error_reporting);
// normal code
Although it would be a good idea to fix what is actually causing the errors.
You've kind of answered your own question. To do it for a specific file, error_reporting(0); will turn off errors. You can also call it multiple times in a script, I think.
You can also use php exceptions to 'catch' errors over a block of code. For example:
try {
// code to ignore errors for here
} catch {
// you can output a custom error here, but putting nothing here will effectively mean errors are ignored for the try block
}
The script will continue running past the try block, even if there is an error within it. See the PHP Manual Entry for more information.
You can change the error reporting level during runtime:
<?
error_reporting(E_ALL);
... some code ....
error_reporting(0);
... some more code ....
error_reporting(E_ALL);
I know of no other way but I can't think of a case where this wouldn't be sufficient. Can you?
That's really a long time ago but someone like me would maybe use my answer.
When i need to do this kind of stuff, i just put # before the variable in order to NOT display the errors coming from this variable.
example:
switch(#$var!="e") {
....
}
Ok, so a little while back I had some help writing some PHP voting code, it worked just fine after I upgraded my server to use the latest version of PHP. However now I have switched servers, and the PHP isn't as up to date as the other one. Anyways here's my code:
<?php
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}
if($_GET['click'] == 'up1'){
file_put_contents('vote/1u.txt', ((int) file_get_contents('vote/1u.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
Execute and display:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('vote/up1.txt'); ?>
Now when on my other server (PHP version 5.2.5) this code worked great! However on my new server the PHP version is 5.2.11, and because of this the code won't work. My question is, is there any way to make this more compatible with an earlier version of PHP, or to write completely new code that will work just like this one? Or is there a way to tell my servers to use PHP 5.2.5+? I'm using cPanel X admin panel.
I have set the text file permissions to 777 and still nothing!
you are checking for variable "click" but executing the code only if it equals "up1".
But your link tells click to equals "yes" so that part of the code is never true, hence never executed.
Change your executor to this:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('counteru.txt'); ?>
But more logically, your processing code should be rationalized a bit to this:
if the link is clicked :
First, if the data file (lu.txt) does not exist, create it and write '+1' inside of it, else, add 1 to its existing value.
Then, redirects to the initial page.
if($_GET['click'] == 'up1'){
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}else{
$content = file_get_contents('vote/1u.txt');
if(!$content){
die("Error! file_get_content failed !");
}
file_put_contents('vote/1u.txt', ((int)$content) + 1);
}
header('Location: ' . $_SERVER['SCRIPT_NAME']);
}
exit;
Not a bad idea to add a trim() around file_get_contents(). Or to check if $_GET['click'] isset() prior to checking if it's equal to 'up1'.
It's conventional to exit() instead of die() after a header redirect--well, from what I've seen at least.
Basically, during development, turn on error reporting and set your error flag to E_ALL to see everything, including warnings and notices--neither of which halt your code, but should still be known and addressed.
You might discover the reason your code produces different outcomes under different minor versions of PHP by turning on full error reporting.