Conjurer Posted June 23, 2006 Share Posted June 23, 2006 Having a problem here! [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] Someone once helped me get some password code to set up a page to control access to another page.Basically want to have a page that can only be accessed by authorized users. So I put the code below into the body of a php page to test. I am testing it on my localhost on my own machine.It works sort of, the problem I have is that the failed text shows up when the page opens. I would like it instead to open the page saying "Please Enter a Valid User Name and Password"One thought occured to me [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] -- is it showing the failed message because it has the variables set from the last time I previewed the page? Do I just need to have something make sure they are cleared or what? And if so how do I?Thanks for some help.[code]<form action="" method="post" name="login"><p>Username: <input name="username" type="text"></p> <p>Password: <input name="password" type="password"> <input type="submit" name="Submit" value="Submit"> </p></form> <?php$username = $_POST['username'];$password = $_POST['password'];$set_username = "user";$set_password = "pass";if($username == $set_username && $password == $set_password){ echo "It worked!";}else{echo "Your Username and Password are not valid!"; die();}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12735-password-prompt-something-is-not-working/ Share on other sites More sharing options...
wildteen88 Posted June 23, 2006 Share Posted June 23, 2006 The reason why you get [i]Your Username and Password are not valid![/i] straight away is because you are not checking whether the form has been submitted yet, so PHP is going to parse your PHP whether the form has been submitted or not!To check whether the form has been submitted use this code:[code]<?php// check whether form has been submitted:if(isset($_POST['Submit'])){ // check whether username and password match here}else // form hasnt been submitted so ask them to login{ echo "Please login<br />";}?>// form code here[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12735-password-prompt-something-is-not-working/#findComment-48813 Share on other sites More sharing options...
Conjurer Posted June 23, 2006 Author Share Posted June 23, 2006 Hey thanks! I will try that. Sounds right.I have gotten very rusty at php, forgotten almost all I knew and that was limited to start with.Appreciate your help!Conjurer [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/12735-password-prompt-something-is-not-working/#findComment-48891 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.