Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post

    Post Txt based login with 3 different or more password file to search in or list ?

    Good day to you all,
    I'm working on my login script and I was wondering how can i make the txt file based login to look into 3 txt file for login and password ?

    My problem is I don't have a clue on how to code it.

    Here is my code :

    PHP Code:



    <table border="1" cellpadding="0" cellspacing="0" align="center" width="800"  align="top">
    <tr><td width="800" class="black" valign="top" align="right">
     
     <?php
    session_start
    ();


    $file file("pass.txt");
    $data= array();
    foreach(
    $file as $value){
        list(
    $user$password$url) = explode('=>'$value);
        
    $data[$user] = array("url"=>$url"password"=>$password);
    }



    if(isset(
    $_POST['username']) && isset($_POST['password'])) {
        if(
    $data[$_POST['username']]['password'] == $_POST['password']) {
            
    $_SESSION['username'] = $_POST['username'] . " " $_POST['password'];
            
    $_SESSION['users'] = $_POST['username'];
            
    $_SESSION['present'] = true;
            
    header('Location: ' $data[$_POST['username']]['url']);
        } else {
            
    login('Wrong user name or password. <br>');
        }
    } else {
        
    login();
    }
    ?><?php

    function login($response='Admin login') {

    ?>
        <p><?=$response?></p>
        <form action="" method="post">
            <b>User name</b>
            <input name="username" type="text" />
            <b>Code</b>
            <input name="password" type="password">
            <input type="submit" value="Login" />
        </form>

    <?php ?> 
     
     </td></tr>
     </table>
    Thank for any help !

  • #2
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    Use a for loop.
    PHP Code:
    $file['1'] = "p1.txt";
    $file['2'] = "p2.txt";
    $file['3'] = "p3.txt";
    $i;
    for( 
    $i=0;$i<count($file);$i++ ) {
      
    // process for checking each file

    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • Users who have thanked BWiz for this post:

    Peuplarchie (07-27-2008)

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post
    Solved

    PHP Code:

    <?php
    //sessions must be initialized prior to any output if output buffering if off
    session_start();

    //the list of files containing passwords
    $files = array(
        
    "file1.txt"
        
    "file2.txt"
        
    "file3.txt"
    );

    //if list of users not set create a new array
    if(!isset($_SESSION['users']))
        
    $_SESSION['users'] = array();
        
    if(isset(
    $_POST['username']) && isset($_POST['password'])){
        
        
    //need to remove slashes from POST if magic_quotes are on 
        
    if(get_magic_quotes_gpc()){
            
    $_POST['username'] = stripslashes($_POST['username']);
            
    $_POST['password'] = stripslashes($_POST['password']);
        }            
        
        
    $userFound false//we need this to exit the loops
        
    foreach($files as $file){ //loop every file in the $files array
            
    if($fh fopen($file"r")){
                while(!
    feof($fh) && !$userFound){ //while not the end of the current file or the user was not found
                    
    list($username$password$url) = explode(","fgets($fh,1024));
                    
                    if((
    $username == $_POST['username']) && ($password $_POST['password'])){
                        
    $_SESSION['username'] = $username;
                        
    $_SESSION['present'] = true;
                        
    array_push($_SESSION['users'], $username); //add the current user to the list of users
                        
    header("Location: ".$url);
                        
    $userFound true//confirm that the user was found
                    
    }    
                }
                
                
    fclose($fh);
                
    //we need to use break to exit the foreach loop if the user is found in one of the files
                
    if($userFound)
                    break;
            } else
                echo 
    "Unable to open a required password file: $file";
        }
        if(!
    $userFound)
            
    login('Wrong username or password.<br />');
    } else {
        
    login();
    }
    ?>
    <?php

    function login($response='Admin login') {
    ?>
        <p><?=$response?></p>
        <form action="" method="post">
            <b>User name</b>
            <input name="username" type="text" />
            <b>Code</b>
            <input name="password" type="password">
            <input type="submit" value="Login" />
        </form>

    <?php ?>


  •  

    Posting Permissions

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