Jump to content

PHP Notice: Undefined variable: id in index.php


CHLee

Recommended Posts

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

You should check whether id isset in the url first before useing the switch statement like so:
[code]<?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');
}

?>[/code]
Link to comment
Share on other sites

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

You should be checking whether the value $_GET['id'] is set, not the variable $id, so you code should read:
[code]<?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');
}

?>[/code]
Another way to do this would be something like this:
[code]<?php
$inc_file = 'main';
if(isset($_GET['id']))
    switch($_GET['id'])
    {
        case "privacy":
        case 'terms':
            $inc_file = $_GET['id'];
        break;
    }
include($inc_file . '.txt');
?>[/code]
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
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.