jeffg Posted March 26, 2008 Share Posted March 26, 2008 I was looking through my Apache error log and came across lines such as the following: [error] [client 192.168.1.2] PHP Notice: Undefined index: cancel in C:\\Apache\\htdocs\\login.php on line 83, referer: http://vcwb/index.html This is the line that causes it: if ($_POST['cancel']){ do_cancel();}elseif ($_POST['login']).... I do this sort of thing all the time to check whether something has been clicked (or whatever). Is there a better way of writing this code so it doesn't post the warning? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 26, 2008 Share Posted March 26, 2008 if (isset($_POST['cancel']))If you also want to check the value:if (isset($_POST['cancel']) && $_POST['cancel'] == true) Link to comment Share on other sites More sharing options...
jeffg Posted March 26, 2008 Author Share Posted March 26, 2008 if (isset($_POST['cancel']))Is this going to fix the error log entry, or is it just complaining that the key 'cancel' does not exist in the $_POST array? I assumed the latter. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 26, 2008 Share Posted March 26, 2008 Yeah, it's complaining that you are trying to access a key that doesn't exist. If you check using isset first then you won't get that notice. Link to comment Share on other sites More sharing options...
jeffg Posted March 27, 2008 Author Share Posted March 27, 2008 if (isset($_POST['cancel']))If you also want to check the value:if (isset($_POST['cancel']) && $_POST['cancel'] == true)I have sooo many references to POST and SESSION variables, that rather than inflate my code by doing this everywhere, I have created a file varcheck.inc with the following:<?php// varcheck.inc// Checks variables are set before accessfunction post($index){ $ret = false; if (isset($_POST["$index"])) $ret = $_POST["$index"]; return $ret;}function session($index){ $ret = false; if (isset($_SESSION["$index"])) $ret = $_SESSION["$index"]; return $ret;}?> and have replaced all my references to $_POST['index'] with post('index'), and the same for $_SESSION. Maybe takes a bit more time to process, but at least it was quick to change, and avoids all those errors in Apache's error log. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.