Jump to content


Photo

Password Prompt - something is not working


  • Please log in to reply
2 replies to this topic

#1 Conjurer

Conjurer
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 23 June 2006 - 02:59 PM

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.


<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();
}
?>


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 June 2006 - 03:13 PM

The reason why you get Your Username and Password are not valid! 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:
<?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


#3 Conjurer

Conjurer
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 23 June 2006 - 06:40 PM

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\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users