Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Unique file() function -- PHP

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to create a login system with Twey's unique file function.

    Well, all the credits from this piece of snippet goes to Twey(it's his function with some minor changes).

    Code:
    <?php
    function newfile($filename, $sep){
    $lines = file($filename);
    $config = array();
    for($i = 0; $i < count($lines); ++$i)
    $config[substr($lines[$i], 0, strpos($lines[$i], $sep))] = substr($lines[$i], strpos($lines[$i], $sep) + 1, -1); // -2 for a Windows host
    return $config;
    }
    print_r(newfile('newfile.txt'));
    ?>
    Code:
    test1|test2
    test3|test4
    test5|test6
    This is an extremely useful function in PHP file handling(it makes things a heck lot easier). Hope you find it useful. Kudos to Twey.
    Last edited by shachi; 01-25-2007 at 03:17 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Erm... what does it do? Posting a description with either comments or some sort of example, etc. would be helpful for people who might find this script useful.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    drj33: It reads the file like this(in this case the newfile.txt)

    Code:
    Array("test1" => "test2", "test3" => "test4", "test5" => "test6");
    It is so extremely useful in creating authentication systems. I am sure there is a version I developed somewhere around. I have also created a more complex version.

    Sorry for the lack of any examples or anything.

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Could you show the original txt file, and by what parameters the function is called?
    - Mike

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Nevermind, I posted when you posted, answering my question
    - Mike

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

    Default

    It should be noted that this was created for a special situation (what exactly, I can't remember now), and in most cases parse_ini_file() is both easier and more flexible.

    I'm not sure this thread is in the right forum.
    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!

  7. #7
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey: How come, it's not in the right forum?

    Everyone who sees this post: If you want to know how to use it of why to use it, let me show you a demonstration:

    Suppose, you're creating a login system and you need a flat-file text database in the registration you simply append the data to the file users.txt, something like this:

    Code:
    <?php exit();?>
    //userid generated from uniqid() or some other techniques
    someuser:userid,password
    someotheruser:hisid,password
    the userdata.txt file:

    Code:
    <?php exit();?>
    userid:email,someotherdata
    hisid:email,someotherdata
    You've the following code to read the user data/login the user:

    PHP Code:
    <?php
    session_start
    ();
    $userlist newfile("users.txt"":");
    $userdata newfile("userdata.txt"":");
    list(
    $id$password) = explode(",",$userlist[$_POST["username"]);
    if(isset(
    $userlist[$_POST["username"]]) && $password == $_POST["password"]){
    echo 
    "Hello ".$_POST["username"]."<br>";
    echo 
    "Your email is $userdata[$id]";
    $_SESSION["user"] = $id;
    }
    ?>
    In this way you can use only the id to find out anything about the user(something similar to relational databases) but it also gives you more power to update fields, I'll write about updating fields later(I am in school now and I even live coded the above scripts so they might not work, I will post a working version as soon as I get home(hopefully)).

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

    Default

    Twey: How come, it's not in the right forum?
    Well, it's just a function, not a tip or tutorial particularly.
    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!

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey: So, how is a tip supposed to be? I am sorry but I am a newbie in this forum.

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

    Default

    A method as to how to accomplish something, usually -- such as "how to use opacity in scripts cross-browser" or something along those lines.
    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
  •