Jump to content

Need help with simple login.


env3rt

Recommended Posts

Hi i'm new to php and I'm just wondering if instead of putting:

 

if(($_POST['password'] == PASSWORD) AND ($_POST['account'] == ACCOUNT) OR ($_POST['password'] == PASSWORD2) AND ($_POST['account'] == ACCOUNT2) OR ($_POST['password'] == PASSWORD3) AND ($_POST['account'] == ACCOUNT3))

 

Could I just put something more simple like one if statement that works for all the accounts?

 

<?php
session_start();
define('ACCOUNT', 'name1');
define('PASSWORD', 'password1');
define('ACCOUNT2', 'name2');
define('PASSWORD2', 'password2');
define('ACCOUNT3', 'name3');
define('PASSWORD3', 'password2');

        if($_POST['check'])
        {
                if(($_POST['password'] == PASSWORD) AND ($_POST['account'] == ACCOUNT) OR ($_POST['password'] == PASSWORD2) AND ($_POST['account'] == ACCOUNT2) OR ($_POST['password'] == PASSWORD3) AND ($_POST['account'] == ACCOUNT3))
                {
                        echo "
<body bgcolor='#757575'>
Thanks $account, you're now logged in.
";
                }
                else
                {
                        echo "Authencation failed.";
                                                                 die("
<body bgcolor='#757575'>
<center>
<form method='post' action='?'>
                                Account: <input name='account' size='17' />
<br>
                                Password: <input type='password' name='password' size='17' />
                                <input type='submit' value='Login!' name='check'>
                        </form>
</center>
</body>");
                }
        }
        else
        {
                      die("
<body bgcolor='#757575'>
<center>
<form method='post' action='?'>
                                Account: <input name='account' size='17' />
<br>
                                Password: <input type='password' name='password' size='17' />
                                <input type='submit' value='Login!' name='check'>
                        </form>
</center>
</body>"); 
        }

 

Link to comment
https://forums.phpfreaks.com/topic/71266-need-help-with-simple-login/
Share on other sites

that IS one if statement.

 

by the way, the way you're grouping ands/ors will not work as you expect it to. you need to rearrange your parentheses:

 

if(

    ($_POST['password'] == PASSWORD AND $_POST['account'] == ACCOUNT) OR ($_POST['password'] == PASSWORD2 AND $_POST['account'] == ACCOUNT2) OR ($_POST['password'] == PASSWORD3 AND $_POST['account'] == ACCOUNT3)

)

yes, but that is very insecure. you essentially give the user 3 x 3! different login possibilities. and you certainly don't save any white space. here is one way to do it:

 

$good_passes = array($PASSWORD, $PASSWORD2, $PASSWORD3);

$good_accounts = array($ACCOUNT, $ACCOUNT2, $ACCOUNT3);

 

if (in_array($_POST['password'], $good_passes) && in_array($_POST['account'], $good_accounts)) {

  // User has correctly guessed one of the many possible accounts and passwords combinations.

 

}

$good_passes = array($PASSWORD, $PASSWORD2, $PASSWORD3);

$good_accounts = array($ACCOUNT, $ACCOUNT2, $ACCOUNT3);

 

if (in_array($_POST['password'], $good_passes) && in_array($_POST['account'], $good_accounts)) {

  // User has correctly guessed one of the many possible accounts and passwords combinations.

 

}

Is a way of doing it but I don't want the 9 possible combinations I only want password2 to work with account2 etc.. Anybody have an idea?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.