Errors: PHP Notice: Undefined index - php

i have this errors in error log
mod_fcgid: stderr: PHP Notice: Undefined index: url in /var/www/vhosts/mysite.it/httpdocs/wp-content/themes/colormag-pro/js/sharrre/sharrre.php on line
4 lines affected.
Are they dangerous for my site? How can i fix them exactly?
UPDATE: First 4 lines of this code in sharre.php get errors
$json['url'] = $_GET['url'];
$url = urlencode($_GET['url']);
$type = urlencode($_GET['type']);
if(filter_var($_GET['url'], FILTER_VALIDATE_URL)){
if($type == 'googlePlus'){ //source http://www.helmutgranda.com/2011/11/01/get-a-url-google-count-via-php/
$contents = parse('https://plusone.google.com/u/0/_/+1/fastbutton?url=' . $url . '&count=true');

Not too bad, annoying. A 'Notice' is something that PHP can carry on with but may cause something else to break, but on the same hand might not cause anything to break at all. A lot of the time 'Notices' can be ignored and are generally turned off in production environments.
Personally I hate to see 'Notices' in my code but that's just me.
Either $json['url'], $_GET['url'], or both are not set so check both and see which one is not being set, from there you will be able to trace back to what is supposed to be setting the variable and fix it.
Use an isset() to check if each one is set before the first line of the code you pasted to check if either one is not actually set.

This is not dangerous for your site per se. The variable(s) in the GET are not set (variables $_GET['url'] is not set). If you plan to use it later in your PHP script, you should set them before, on the previous page. So, there are two pages, first one and your PHP page - the second one.

Related

Ignore warning and carry on

I am running a script where a url parameter is increasing by 1. Every so often, I get the error message below and the script comes to a halt:
Warning: file_get_contents(https://example.com?id=431): failed to open
stream: HTTP request failed! HTTP/1.1 429 Please retry after few
minutes
$i = 1;
while($file = file_get_contents('https://example.com?id='.$i)) {
echo ''.$i.'</br>';
$i++
}
While I know I can use error_reporting(0); to stop the warnings from appearing, my question is as follows: will the script continue running after the hidden warning?
You can, of course, disable error reporting as others have suggested. However, there are two MUCH better solutions.
First, you could use set_error_handler to create a function which converts errors into exceptions (code in Example 1 here: http://php.net/manual/en/class.errorexception.php). Then, you can simply use a try and catch to check to see if an exception has occurred, and handle it appropriately. See here: http://php.net/manual/en/internals2.opcodes.catch.php
Another solution would be to use PHP's cURL library (http://php.net/manual/en/book.curl.php). cURL will let you check if an error has occurred when you make an HTTP request, and you can respond appropriately.
To be clear, since it seems to be OP's only concern: both of these solutions will allow the script to continue running after an error has occurred. They also have the added benefit of allowing OP to create code that makes a predetermined programmatic response to errors rather than just blindly ignoring any and all errors with unknown results (and no indication that they even happened).
Finally, a note that's applicable to this particular situation: HTTP 429 is "too many requests," which makes sense given that OP is placing these requests one after another with no delay. Sleeping (http://php.net/manual/en/function.sleep.php) between HTTP requests would likely eliminate the problem entirely.
This is not something you should use in prod, but try:
error_reporting(E_ERROR | E_PARSE);
I think it is better to check if the file is reachable before trying to read it.
Replace the condition, e.g.:
while($i < 100)
You can try this:
error_reporting(0);
Put this at the top of script
Put this at the top of the script to avoid the warnings and notices.
error_reporting(E_ERROR);
This will still show Fatal run-time errors. These indicate errors that
can not be recovered from, such as a memory allocation problem.
Execution of the script is halted.
To turn of all the errors and warning,
error_reporting(0);

Session doesn't work on MAMP

When i try to run my site on localhost i get an error:
Undefined index: log in ... on line 137
Within this file there is a line:
if (!$_SESSION['log']) { ...
Everything works on server, but not on localhost. What can I do to fix it?
There is probably a difference between the level of error reporting between the server and your local setup.
If you want to check if the variable is set (assuming that a session has been started...), you should use:
if (!isset($_SESSION['log'])) {
Or if you want to check if it is not set and / or empty or false:
if (empty($_SESSION['log'])) {
Both will not generate any warnings for non-set variables or array indices.
It probably isn't working "on server," but is instead just not showing the error message to the page.
You can fix the warning re: the index by changing your if statement to this:
if (isset($_SESSION['log']) && !$_SESSION['log']) {
Or to whatever condition you need it to be.

Ajax result differs between development and production server

I have a small ajax script that gets some json data and fills some form fields when the user makes a selection.
I noticed this morning that there was an error when I ran the ajax on my development server but when run production side it worked. I am assuming this is due to some difference in error reporting between the servers but I can't figure out why.
Dev PHP version: 5.3.13
Prod PHP version: 5.3.16
I have tracked the error to some variables that were only setting if additional rows cam out of the database. I am retrieving between 1 and 3 rows.
The first row is assigned to $array1 and additional rows go into their own array as $sec_row[0] and $sec_row[1].
$array1 = ('Name'=>'George','Address'=>'52 Smith St',....etc);
$sec_row[0] = ('Alias1'=>'Jorge','Location'=>'SimCity',....etc);
echo json_encode(array('result1'=>$array1,'result2'=>$sec_row[0],'result3'=>$sec_row[1]);
Note $sec_row is only set when additional results are found.
On the live site when $sec_row is undefined the ajax returns result2 and result3 as NULL. But on the development server on my localhost it gives me an "undefined index" (if only $sec_row[0] is set) or "undefined variable" (if neither $sec_row are set) error.
I have fixed the error locally by setting $sec_row manually before encoding the json but I don't understand why I needed to do this locally but not on the production server.
Any suggestions as to what setting might cause this?
Because you probably have display_errors turned on so it will generate the undefined index error in the middle of the JSON making it invalid JSON which can not be parsed on the JS side.
Easiest thing to do other than fix the error is to turn error_reporting off. But you should fix the error, by checking if the indexes are set and then outputting the value, or null.
json_encode(array(
'result1'=>$array1,
'result2'=>isset($sec_row[0]) ? $sec_row[0] : null,
'result3'=> isset($sec_row[1]) ? $sec_row[1] : null
);
Actually, I would try to correct the error instead of simply ignoring it... Try to do something like this:
$result = array('result1' => $array1);
if (isset($sec_row[0]))
$result['result2'] = $sec_row[0];
if (isset($sec_row[1]))
$result['result3'] = $sec_row[1];
echo json_encode($result);
its to do with the error_reporting levels on your servers
so either turn them off - add the following to the top of the script, or directly in the php.ini if you have access to it - look for display_errors, set to 0
error_reporting(0);
#ini_set('display_errors', 0);
or why not build your json_encode beforehand
ie
assuming you always only have 1 $result1
for($x = 0; $x <= count($sec_row); $x++) {
$more_json .= "'result'".$x+2 ."=> ".$sec_row[$x].","
}
# strip the last comma off
$more_json = rtrim($more_json ,',');
echo json_encode(array('result1'=>$array1, $more_json);
I have not tested this, so the syntax may not be 100%, but you get the idea

Php error in closure compiler execution

When I run php-closure i get a PHP error
Undefined index: HTTP_IF_NONE_MATCH in <b>/php-closure.php</b> on line <b>183</b>
Line 184 of php-closure is
trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
This error only happens when closure has already written the compressed javascript file to the directory once, if the directory is emptied the error does not appear. What does this error mean and how can I avoid it?
Thank You So Much!
This is most likely a negligeable message. The code should check for the presence of HTTP_IF_NONE_MATCH in the server array first, before checking its value. What is set in $_SERVER tends to change from server to server (and the headers sent by the client) so probably the original developer had that value set, and didn't think to check for it.
You could prepend the line in question with a check
if (array_key_exists("HTTP_IF_NONE_MATCH", $_SERVER))
(depending whether the program's logic and flow permit this, of course)
Or turn down the error reporting so that notices are not displayed (not recommended but mybe necessary if you can't touch the package) by putting this into the very beginning of your application:
error_reporting(E_ALL ^ E_NOTICE);
#Ivo has a great third alternative in his comment.

PHP turn off errors - in one file only

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") {
....
}

Categories