Jump to content


Photo

PHP Notice: Undefined variable: id in index.php


  • Please log in to reply
3 replies to this topic

#1 CHLee

CHLee
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 May 2006 - 12:59 PM

Hello there im new to PHP and i'm having trouble with loading content within a page. I get this error [error] PHP Notice: Undefined variable: id in Apache Group\\Apache2\\htdocs\\fluid-web.com\\public_html\\index.php on line 111
PHP Notice: Undefined variable: id in Apache Group\\Apache2\\htdocs\\fluid-web.com\\public_html\\index.php on line 112

My the content swith works but this error comes up each time you access the page.

The switch code i use is:

<?php switch($id) { default: include('main.txt');
break; case "privacy": include('privacy.txt');
break; case "terms": include('terms.txt');
}?>

And they are called with these href links:

<a href="index.php?id=terms">Terms &amp; Conditions</a>
<a href="index.php?id=privacy">Privacy Statement</a>

I now i can disable these notices within php.ini but I would prefer there to be no error notices at all.
Could some on please help me on this, I used to use a php snippet like this when my old host used php 3 or 4 but i had no access to the error logs:

<?php if($id == "") {include "main.txt";} else {include ($id.".txt");}?>

Then i would call them with the href link as above.

I'm now hosting my own site which is working but i'm not sure if my php5 is correctly configured but it works apart from those error.


My server info: Apache/2.2.2 (Win32) PHP/5.1.4

Many thanks in advance Chris
My pain will be the pain my computer feels when it's a door stop....

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 May 2006 - 03:09 PM

You should check whether id isset in the url first before useing the switch statement like so:
<?php

//check that id isset
if(isset($id))
{
    switch($id)
    {
        case "privacy":
            include('privacy.txt');
        break;

        case "terms":
            include('terms.txt');
        break;
    }
}
else // if it isn't include main.txt (defualt)
{
    include('main.txt');
}

?>


#3 CHLee

CHLee
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 May 2006 - 03:35 PM

Thanks it seems to work without the notice of the undefined variable in index.php, so does the isset function set the variable or wot as i'm new to php i would like to understand how it works so i can learn php correctly and have error free code.

Many Thanks Chris
My pain will be the pain my computer feels when it's a door stop....

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 May 2006 - 08:02 PM

You should be checking whether the value $_GET['id'] is set, not the variable $id, so you code should read:
<?php
//check that id isset
if(isset($_GET['id']))
{
    switch($_GET['id'])
    {
        case "privacy":
            include('privacy.txt');
        break;
        case "terms":
            include('terms.txt');
        break;
    }
}
else // if it isn't include main.txt (defualt)
{
    include('main.txt');
}

?>
Another way to do this would be something like this:
<?php
$inc_file = 'main';
if(isset($_GET['id']))
    switch($_GET['id'])
    {
        case "privacy":
        case 'terms':
            $inc_file = $_GET['id'];
        break;
    }
include($inc_file . '.txt');
?>
This way you will always include one of your files, since a default value is set first. If "$_GET['id']" is one of the expected values, the script will use that. If someone tries to use your URL maliciously and enters an unexpected value for the ID, it doesn't matter and the script will include the default file, just as it does when $_GET['id'] is not set.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users