Jump to content

Help with Error


stobbo

Recommended Posts

Hello.

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

Error:

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

Code

[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");
?>[/code]

If you can help, I would be most pleased.

Thanks, Stobbo.
Link to comment
Share on other sites

This will happen when $_GET['id'] hasn't been defined. Try...
[code=php:0]
<?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");
?>
[/code]
Link to comment
Share on other sites

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...

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

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.