supanoob Posted March 14, 2009 Share Posted March 14, 2009 When i use $_GET['step'] to find out what the user is doing and include the page required (using the folowing code) i get errors even though the $_GET['step'] is defined. Anyway to stop this happening? PHP Code: <?php if (!$_GET['step']) { include_once('homepage.php'); } if ($_GET['step'] == 'register') { include_once('register.php'); } ?> Errors: Notice: Undefined index: step in C:\wamp\www\index.php on line 75 Hello World! Notice: Undefined index: step in C:\wamp\www\index.php on line 80 The hello world is what i want echoing when on the hompage (as a test obv) the errors disappear when i click register and my resgistration page shows up. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted March 14, 2009 Share Posted March 14, 2009 Notices aren't important are they? You could just display errors... <?php error_reporting(E_ALL ^ E_NOTICE); //error reporting is everything but notices. ?> Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 14, 2009 Share Posted March 14, 2009 I got this error/warning once... Can't remember how I fixed it though. I'm interested in what its cause is though... Quote Link to comment Share on other sites More sharing options...
supanoob Posted March 14, 2009 Author Share Posted March 14, 2009 Notices aren't important are they? You could just display errors... <?php error_reporting(E_ALL ^ E_NOTICE); //error reporting is everything but notices. ?> ive tried that and it doesnt change anything, just been reading up on it and apparently i cant use global variables in the php version im using? 5.2.9? however when when put $step = $_GET['step']; in the top of my code it throws out the same error, and i was mistaken my register doesn't show up at all. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 14, 2009 Share Posted March 14, 2009 Notices aren't important are they? You could just display errors... Err. Yes. They're pretty important. They're generally the single most helpful thing in finding out why a script's not running the way you expected it to. It's damn difficult finding a typo without them. With regard to the error, you should check to make sure that $_GET['step'] is set before trying to use it: if(isset($_GET['step']) && $_GET['step'] == 'register'){ include_once('register.php'); }else{ include_once('homepage.php'); } You might wonder why you don't still have problems with this if statement. After all, you're still making a test against $_GET['step'] so if it doesn't exist you might think you'd still get a warning. But no. PHP (like most languages) uses short-cut evaluation of logical expressions. That is, whilst evaluating the expression, if it has already worked out the result, it won't check the rest of the comparisons. So, in this case, the isset() test will be evaluated first. If it is false, then the second part of the statement wont be tested since false & expression is false regardless of the result of expression. Quote Link to comment Share on other sites More sharing options...
supanoob Posted March 14, 2009 Author Share Posted March 14, 2009 Notices aren't important are they? You could just display errors... Err. Yes. They're pretty important. They're generally the single most helpful thing in finding out why a script's not running the way you expected it to. It's damn difficult finding a typo without them. With regard to the error, you should check to make sure that $_GET['step'] is set before trying to use it: if(isset($_GET['step']) && $_GET['step'] == 'register'){ include_once('register.php'); }else{ include_once('homepage.php'); } You might wonder why you don't still have problems with this if statement. After all, you're still making a test against $_GET['step'] so if it doesn't exist you might think you'd still get a warning. But no. PHP (like most languages) uses short-cut evaluation of logical expressions. That is, whilst evaluating the expression, if it has already worked out the result, it won't check the rest of the comparisons. So, in this case, the isset() test will be evaluated first. If it is false, then the second part of the statement wont be tested since false & expression is false regardless of the result of expression. so using that method (which fixed it, thanks alot) how would i go about adding more to that list? would something like: if(isset($_GET['step']) && $_GET['step'] == 'register'){ include_once('register.php'); }else if(isset($_GET['step']) && $_GET['step'] == 'register_done'){ include_once('register_done.php'); } else if(isset($_GET['step']) && $_GET['step'] == 'newsr'){ include_once('news.php'); } else { include_once('homepage.php'); } do the trick? Quote Link to comment Share on other sites More sharing options...
hastishah Posted March 14, 2009 Share Posted March 14, 2009 Hi Just try this code once. your problem will be solved. <?php if(in_array('step',$_GET)) { if (!$_GET['step']) { include_once('homepage.php'); } if ($_GET['step'] == 'register') { include_once('register.php'); } } ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 14, 2009 Share Posted March 14, 2009 I think i'd build an array of pages which you might want to include. Something like this: $pages = array('register','register_done','news'); if(isset($_GET['step']) && in_array($_GET['step'],$pages)){ include($_GET['step'].'.php'); }else{ include('homepage.php'); } Quote Link to comment 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.