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.
Related
I have a PHP script, util.php, that I call from jQuery:
$("#galleryContent").load("util.php",
{op : 'get_thumbs'
},
function() {
$('.galleryThumb').draggable(thumb_dragOps);
}
);
Anything the script echoes shows up in #galleryContent.
But the script has acquired a bug such that nothing happens now when it's called. Even if I put an
echo ("In util.php");
at the very top, this doesn't show up in #galleryContent. If I delete all of the code after the echo down to ?> then the echo does show up. So something is keeping util.php from parsing and running. There's a missing ";" a missing "}", or something like that.
My question is, why isn't something telling me were that bug is? I have
ini_set('display_errors', 1);
error_reporting(E_ALL);
at the top of the script but these seem to only help with run time errors, not parse errors. What can I use to find a parse error?
Thanks.
Parsing errors cannot be configured in run-time via ini_set or error_reporting, you need to use php.ini set display-startup-errors to 1 and restart apache
second - you can try run php -l <filename> to get syntax errors
I need to test some functionality on live server and it's obvious that they turned off errors at some global level.
Problem is that I can't work when I don't know what's throwing the error. How can I make sure that exceptions are being shown and not just 500 code.
I have tried putting these two lines at the top of my script but it's still empty.
<?php
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
throw new Exception('Fatal Error'); // nothing is outputed
The two lines are pretty much identical, so remove one of them.
You also need ini_set("display_errors",1); to actually display errors.
Note however that this will not work for syntax errors in the current file, as these occur in the parsing phase, before any statements are actually run.
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'm trying to make errors hidden but it seems I'm doing something wrong.
In my hosting configuration display_errors is set to off and I don't have .htaccess file. I tried to write follownng script
<?php
echo ord(ini_get("display_errors")) . " ";
die("error");
?>
And I'm getting following output:
0 error
So, display_errors is set to off, but die() function still shows error on the screen. How to avoid this?
By doing die("error"), you're are commanding the code that it should output the string "error" and stop the code.
You are seeing the "error" message does not mean that there is an error, it is just another string.
die() is a function (commonly used to handle errors), but it is not deactivated when you set display errors off. It will still work and to whatever it is meant to do.
How to avoid this?
Do not use die() to handle errors.
use trigger_error() instead, which will follow the behavior you expected
There is a option to display error to stderr instead of stdout so you won't see them on you webpage, but only in the logs. (Manual)
I found error_log() function. Now I can use
function quit($error)
{
echo "Request failed";
error_log($error);
die();
}
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") {
....
}