Page 1 of 6

Undefined variable - why am I getting these?

Posted: Fri Jun 21, 2013 3:13 am
by simonmlewis
[Fri Jun 21 08:46:28 2013] [warn] [client 81.129.13.166] mod_fcgid: stderr: PHP Notice: Undefined index: sn in /var/www/vhosts/domain/httpdocs/includes/product.inc on line 244
We are getting a ton of these kinds of errors.
sn is in the product page as:

Code: Select all

$sn = $_REQUEST['sn'];
Is this wrong? Is it because it's saying $sn is in the URL, but it cannot see it in the URL, so it therefore throws an error in the log?

If so, how do you do it without getting errors - I'm sensing it's something to do with "ISSET"???

Re: Undefined variable - why am I getting these?

Posted: Fri Jun 21, 2013 3:40 am
by pbs
you are getting this "Undefined index" notice errors because variables are not initialized, so you need check each variable with isset or you can disable this notice errors from php.ini file

Re: Undefined variable - why am I getting these?

Posted: Fri Jun 21, 2013 3:46 am
by simonmlewis
Ok - so how do I disable it, but also, how do I "initialize" it with ISSET?
Probably lazy of me, but never used that as I assumed it would just ignore it if not found.

Re: Undefined variable - why am I getting these?

Posted: Fri Jun 21, 2013 3:53 am
by pbs
if you use ISSET then you need to check for every variable, rather you can disable from php.ini file like this

search "error_reporting" in php.ini file and replace it with

error_reporting = E_ALL & ~E_NOTICE

Re: Undefined variable - why am I getting these?

Posted: Fri Jun 21, 2013 12:43 pm
by requinix
Just keep in mind that adjusting your error_reporting settings does not make the problem go away. The code is still wrong. The difference is that now PHP won't tell you it's wrong.

Re: Undefined variable - why am I getting these?

Posted: Fri Jun 21, 2013 12:58 pm
by simonmlewis
Right.... so how do I do it right with each variable?

Re: Undefined variable - why am I getting these?

Posted: Fri Jun 21, 2013 1:16 pm
by AbraCadaver
simonmlewis wrote:Right.... so how do I do it right with each variable?

Code: Select all

$sn = isset($_REQUEST['sn']) ? $_REQUEST['sn'] : '';

Re: Undefined variable - why am I getting these?

Posted: Wed Jul 03, 2013 11:29 am
by simonmlewis
Funny how when using this, most of our scripts wouldn't work. But would if I did:
sn = $_REQUEST['sn'];, tho there were errors in the log. It works.

Re: Undefined variable - why am I getting these?

Posted: Wed Jul 03, 2013 11:10 pm
by Christopher
simonmlewis wrote:Funny how when using this, most of our scripts wouldn't work. But would if I did:
sn = $_REQUEST['sn'];, tho there were errors in the log. It works.
That may be because your scripts don't expect the undefined value to be ''. Perhaps you could try:

Code: Select all

$sn = isset($_REQUEST['sn']) ? $_REQUEST['sn'] : null;
Better would be to improve the checks for variables that get values from system superglobals. Then your code would not be so fragile.

Re: Undefined variable - why am I getting these?

Posted: Thu Jul 04, 2013 7:20 am
by simonmlewis
Great stuff. Just been through the whole site and updated them all (as many as I could find).
Site also now seems to be running faster, possibly as a result.

I've turned error reporting back on, so in a few hours will see what's occurred.

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 08, 2013 3:33 am
by simonmlewis

Code: Select all

function curPageURL() 
{
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$url = curPageURL();
I am getting an error on the second like containing "HTTPS". Any ideas why?
The site isn't using SSL, so maybe I should just change it to HTTPS, but the query looks to be checking IF it is secure.
This is the error:
[text][Mon Jul 08 09:15:04 2013] [warn] [client 173.199.114.235] mod_fcgid: stderr: PHP Notice: Undefined index: HTTPS in /var/www/vhosts/site.co.uk/httpdocs/includes/product.inc on line 231[/text]

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 08, 2013 8:31 am
by Celauran
The array key isn't set if the request isn't HTTPS, hence the notice.

Code: Select all

$pageURL = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 08, 2013 8:34 am
by simonmlewis

Code: Select all

function curPageURL() 
{
 $pageURL = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
This correct?

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 08, 2013 9:43 am
by Christopher

Code: Select all

function curPageURL() 
{
 $pageURL = (isset($_SERVER['HTTPS']) && ($_SERVER["HTTPS"] == "on")) ? 'https://' : 'http://';
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
You should be able to figure this out. ;)

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 08, 2013 9:46 am
by simonmlewis
Ok - so the suggestion before was wrong?