Jump to content


Photo

Help with Error


  • Please log in to reply
2 replies to this topic

#1 stobbo

stobbo
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 29 October 2006 - 10:07 PM

Hello.

I am fairly new to PHP, and am struggling over a error. Any help would be kindly appriciated.

Error:

Notice: Undefined index: id in c:\home\********\public_html\index.php on line 3

Code

<?php 
include("/home/********/public_html/modules/template-top.php");
$page= $_GET['id'];
if(file_exists("/home/********/public_html/content/".$page.".php"))
{
include("/home/********/public_html/content/".$page.".php");
}
else
{
include("/home/********/public_html/modules/news.php");
}
include("/home/********/public_html/modules/template-bottom.php");
?>

If you can help, I would be most pleased.

Thanks, Stobbo.

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 29 October 2006 - 10:43 PM

This will happen when $_GET['id'] hasn't been defined. Try...
<?php 
include("/home/********/public_html/modules/template-top.php");
if(isset($_GET['id']) && file_exists("/home/********/public_html/content/".$_GET['id'].".php"))
{
include("/home/********/public_html/content/".$_GET['id'].".php");
}
else
{
include("/home/********/public_html/modules/news.php");
}
include("/home/********/public_html/modules/template-bottom.php");
?>


#3 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 29 October 2006 - 10:45 PM

It means that $_GET['id'] isn't set

If someone goes to pagename.php there is no $_GET['id'] as averse to pagename.php?id=1 in which case it is 1.

You need to check if it exists first of all...

<?php
$page = isset($_GET['id']) ? $_GET['id'] : defaultvalue;
?>

This will set it to $_GET['id'] if it exists or to a defaultvalue (you'll need to set that) if it doesn't.  Also, make sure you're checking the contents of $_GET['id'] before using it, otherwise a user could potentially request any file.

HTH

Dest




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users