SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: PHP login

  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP login

    instead of using a mysql database for my user logins for my site i have it make a text file with the usersname and password inside the file is like

    mike
    logmein

    how would i make an authenticate php page to read from taht information instead from a mysql database.
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would use the file system functions to read the file and load it into an array and check it against the entered login information. I have to say though, one of the reasons that a database is used for logins is because of it's security model. In addition, updating that file as opposed to the database is going to become increasingly hard to do unless you only have 3 or 4 users. In that case, an htaccess file would be a much better choice. There is little or no security in doing this with a text file (depending on where in the filesystem it will sit).

  3. #3
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Also i don't know how you would use

    user
    pass
    user
    pass

    Instead you should be using

    user,pass
    user,pass

    Thanks.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Hammer65 View Post
    You would use the file system functions to read the file and load it into an array and check it against the entered login information.
    http://www.php.net/manual/en/ref.filesystem.php


    By the way, don't save the password in plain text, encrypt it using md5. It'll add a little bit of safety to your login system.

    http://fr3.php.net/manual/en/function.md5.php

  5. #5
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I cooked up quick example but have'nt tested:

    create.php to create new user and add to users.txt file.

    PHP Code:
    <?php
    $filename 
    'users.txt';
    $somecontent $_POST["username"] . ", " md5($_POST["password"]);

    if (
    is_writable($filename))
    {
            if (!
    $handle fopen($filename'a'))
            {
                    echo 
    "Cannot open.";
                    exit;
            }

            if (
    fwrite($handle$somecontent) === FALSE)
            {
                    echo 
    "Cannot write.";
                    exit;
            }

            echo 
    "Successfully Created.";
            
    fclose($handle);
    }
    else
    {
            echo 
    "Not writable.";
    }
    ?>

    login.php to login existing users
    PHP Code:
    <?php
    $somecontent 
    $_POST["username"] . ", " md5($_POST["password"]);

    $handle = @fopen("users.txt""r");

    if (
    $handle)
    {
            while (!
    feof($handle))
            {
                    
    $buffer fgets($handle4096);

                    if (
    $buffer == $somecontent)
                    {
                            
    //logged in

                    
    }
            }


            
    fclose($handle);
    }
    ?>
    Hope this helps.

    Thanks.


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
  •