CHLee Posted May 26, 2006 Share Posted May 26, 2006 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 111PHP Notice: Undefined variable: id in Apache Group\\Apache2\\htdocs\\fluid-web.com\\public_html\\index.php on line 112My 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 & 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.4Many thanks in advance Chris Quote Link to comment https://forums.phpfreaks.com/topic/10503-php-notice-undefined-variable-id-in-indexphp/ Share on other sites More sharing options...
wildteen88 Posted May 26, 2006 Share Posted May 26, 2006 You should check whether id isset in the url first before useing the switch statement like so:[code]<?php//check that id issetif(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] Quote Link to comment https://forums.phpfreaks.com/topic/10503-php-notice-undefined-variable-id-in-indexphp/#findComment-39207 Share on other sites More sharing options...
CHLee Posted May 26, 2006 Author Share Posted May 26, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/10503-php-notice-undefined-variable-id-in-indexphp/#findComment-39212 Share on other sites More sharing options...
kenrbnsn Posted May 26, 2006 Share Posted May 26, 2006 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 issetif(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 Quote Link to comment https://forums.phpfreaks.com/topic/10503-php-notice-undefined-variable-id-in-indexphp/#findComment-39275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.