Undefined variable - why am I getting these?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Undefined variable - why am I getting these?

Post 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"???
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Undefined variable - why am I getting these?

Post 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Undefined variable - why am I getting these?

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Undefined variable - why am I getting these?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post by simonmlewis »

Right.... so how do I do it right with each variable?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Undefined variable - why am I getting these?

Post by AbraCadaver »

simonmlewis wrote:Right.... so how do I do it right with each variable?

Code: Select all

$sn = isset($_REQUEST['sn']) ? $_REQUEST['sn'] : '';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable - why am I getting these?

Post 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.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined variable - why am I getting these?

Post 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://';
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable - why am I getting these?

Post 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. ;)
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Undefined variable - why am I getting these?

Post by simonmlewis »

Ok - so the suggestion before was wrong?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply