Results 1 to 4 of 4

Thread: Need help with Login script

  1. #1
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default Need help with Login script

    Hello
    I have on login script (if, else)
    here is my script:
    Code:
    <?php
    require_once 'config.php';
    require_once 'functions.php';
    
    $errMsg = '';
    if (isset($_POST['txtUserid'])) {
    
    	// Check the user login. For now we only check it 
    	// against a hardcoded value
        if ($_POST['txtUserid'] == 'azservice' && $_POST['txtUserpw'] == 'azservice') {
            $_SESSION['isLogin'] = true;
    
    		header('Location: admin.php');
    		exit;
        }
    	
    	
    	 else {
    		$errMsg = "Wrong Id/Password";
        }		
    } 
    
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <p>&nbsp;</p>
    <?php
    if ($errMsg != '') {
    	echo '<p align="center"><font color="#990000">' . $errMsg . '</font></p>';
    } 
    ?>
    <form action="" method="post" name="frmCampaign" id="frmCampaign">
     <table align="center" width="500" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
      <tr> 
       <td width="200" bgcolor="#336699"><font color="#FFFFFF"><strong>User Id</strong></font></td>
       <td bgcolor="#FFFFFF"><input name="txtUserid" type="text" id="txtUserid" value=""></td>
      </tr>
      <tr> 
       <td width="200" bgcolor="#336699"><font color="#FFFFFF"><strong>Password</strong></font></td>
       <td bgcolor="#FFFFFF"><input name="txtUserpw" type="password" id="txtUserpw" value=""></td>
      </tr>
      <tr> 
       <td colspan="2" align="center" bgcolor="#FFFFFF"> <input type="submit" name="Submit" value="Submit"> 
       </td>
      </tr>
     </table>
    </form>
    </body>
    </html>
    there is no problem now buy i need to make 2 users:

    Code:
    if (isset($_POST['txtUserid'])) {
        if ($_POST['txtUserid'] == 'azservice' && $_POST['txtUserpw'] == 'azservice') {
            $_SESSION['isLogin'] = true;
    
    		header('Location: admin.php');
    		exit;
        }
    how can i make that for 2 users?
    help me please

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You might want to try a database backend. Here's one I made earlier.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I made an array with all the usernames:

    PHP Code:
    $users = array('alex','twey','bob','awesome','guest');
    $passwords = array('alexPass','tweyPass','bobPass','awesomePass','guestPass');

    if(isset(
    $_POST['user']) && isset($_POST['pass'])){
         
    $user $_POST['user'];
         
    $pass $_POST['pass'];

         if(
    in_array($user,$users) && in_array($pass,$passwords)){ 
              
    // set the session and stuff here
         
    }

         else{
             
    // error message
         
    }
    }

    else{
    // whatever

    Last edited by alexjewell; 08-13-2007 at 09:52 PM.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This implementation is broken: a user could log in with any username and any password, e.g. alex:bobPass.
    Code:
    if(in_array($user, $users) && array_search($user, $users, true) === array_search($pass, $passwords, true)) {
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •