Jump to content

Password Prompt - something is not working


Conjurer

Recommended Posts

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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\" /]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.