cactus Posted March 8, 2011 Share Posted March 8, 2011 Hi, I keep get this error message: Undefined index: username in C:\wamp\www\admin.php on line 29 I have tried everything to resolve it and am now just frustrated. Is there anyone who can help me out please? My code where its having the problem is: if (isset($_POST['Submit']) || ($_POST['username'] == 'admin') && //THIS IS LINE 29 ($_POST['password'] == 'xyz')) { $_SESSION['username'] = 'login'; } else { echo "<b>You login details is not correct. Pls login again</b>"; } if ($_SESSION['username']=='login'){ //IT'S ALSO GIVING THE SAME ERROR FOR THIS LINE if (isset($_REQUEST['file'])) { $fc = file_get_contents($_REQUEST['file']); $text = explode("<!-- EDITABLE -->",$fc); echo "<form method='post' action=''><textarea name='content'>$text[1]</textarea>"; echo "<p><input type='hidden' name='file' value='".$_REQUEST['file']."' /><input name='submitUpdate' type='submit' value='Update Page'></form>"; } Quote Link to comment https://forums.phpfreaks.com/topic/229997-undefined-index-problem/ Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 Your HTML form doesn't contain a field called username then. Quote Link to comment https://forums.phpfreaks.com/topic/229997-undefined-index-problem/#findComment-1184566 Share on other sites More sharing options...
Pikachu2000 Posted March 8, 2011 Share Posted March 8, 2011 Look at your logic in this line: if (isset($_POST['Submit']) || ($_POST['username'] == 'admin') && ($_POST['password'] == 'xyz')) { It reads: "If the form is submitted, OR username == 'admin' . . ." If the form hasn't been submitted, how can 'username' equal anything at all? Quote Link to comment https://forums.phpfreaks.com/topic/229997-undefined-index-problem/#findComment-1184661 Share on other sites More sharing options...
cactus Posted March 9, 2011 Author Share Posted March 9, 2011 Thanks, I have included a username variable in my form. I hadn't thought about what the code meant stupid me. How would I solve this error to make it work properly then? Thanks again, the help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/229997-undefined-index-problem/#findComment-1184936 Share on other sites More sharing options...
HuggieBear Posted March 9, 2011 Share Posted March 9, 2011 Make sure your form has a username and password field and then use something like this: <?php session_start(); if (!isset($_SESSION['logged_in'])){ // You're not logged in, have you submitted the form if (isset($_POST['Submit']) { // You have submitted the form if ($_POST['username'] == 'admin' && $_POST['password'] == 'xyz'){ // You're authenticated $_SESSION['logged_in'] = $_POST['username']; echo "Thanks for logging in"; } else { // You entered invalid details echo "Sorry, the username and password you entered are invalid"; } } } else { // You're already logged in echo "You're logged in as " . $_SESSION['logged_in']; } if (!isset($_SESSION['logged_in'])){ // Display the form echo "This is where your form code goes"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229997-undefined-index-problem/#findComment-1184960 Share on other sites More sharing options...
cactus Posted March 9, 2011 Author Share Posted March 9, 2011 Thanks Huggie Bear that looks fantastic and i'll try that out. However I'm looking at my code again and am not sure the php tags and html tags are in the correct places. Would someone mind looking for me as i'm not sure whether this is also causing a problem? Thanks a lot its much appreciated. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/229997-undefined-index-problem/#findComment-1185021 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.