Page 4 of 4 FirstFirst ... 234
Results 31 to 39 of 39

Thread: PHP+file handling+confused???

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

    Default

    Not tested thoroughly enough, obviously How about this?
    Code:
    <?php
    define('DATABASE_FILE', 'database_fake.php');
    define('SEPARATOR', ',');
    define('LINEBREAK', "\n");
    
    define('USERNAME', 0);
    define('PASSWORD', 1);
    define('EMAIL', 2);
    define('COUNTRY', 3);
    define('ZIP', 4);
    
    function updateUser() {
      $ret = 0;
      $lines = array_unique(file(DATABASE_FILE));
      $argv = func_get_args();
      $conditions = array();
      $changes = array();
      for($i = 0; $i < count($argv) && $argv[$i] !== SEPARATOR; $i += 2)
        $conditions[$argv[$i]] = $argv[$i + 1];
      for(++$i; $i < count($argv); $i += 2)
        $changes[$argv[$i]] = $argv[$i + 1];
      for($i = 0; $i < count($lines); ++$i) {
        $cline = explode(SEPARATOR, $lines[$i]);
        $changeThisLine = true;
        foreach($conditions as $k => $v)
          if($cline[$k] != $v)
            $changeThisLine = false;
        if(!$changeThisLine) continue;
        foreach($changes as $k => $v)
          $cline[$k] = $v;
        $lines[$i] = implode(SEPARATOR, $cline);
        ++$ret;
      }
      $f = fopen(DATABASE_FILE, 'w');
      fwrite($f, str_replace(LINEBREAK . LINEBREAK, LINEBREAK, implode("\n", $lines)));
      fclose($f);
      return $ret;
    }
    ?>
    And by the way if I remove the <?php exit()?> lines then anyone would be able to view the database(sure I could encrypt the password with md5 hash but what about the email addresses??They'd be a great source for spambots).
    There are many different ways to accomplish this. For a start, that file is not referenced anywhere that's visible to the user, so it'd have to be one lucky spammer to stumble across the file (especially if you give it a slightly less obvious name).
    If that's not enough for you (and it shouldn't be, really) a simple solution is to move the file outside the web root.
    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!

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

    Default

    Twey, this script has the same problem, it doesn't update the file.

    Currently my page looks like this:

    Code:
    <?php
    define('DATABASE_FILE', 'database.php');
    define('SEPARATOR', ',');
    define('LINEBREAK', "\n");
    
    define('USERNAME', 0);
    define('PASSWORD', 1);
    define('EMAIL', 2);
    define('COUNTRY', 3);
    define('ZIP', 4);
    
    function updateUser() {
      $ret = 0;
      $lines = array_unique(file(DATABASE_FILE));
      $argv = func_get_args();
      $conditions = array();
      $changes = array();
      for($i = 0; $i < count($argv) && $argv[$i] !== SEPARATOR; $i += 2)
        $conditions[$argv[$i]] = $argv[$i + 1];
      for(++$i; $i < count($argv); $i += 2)
        $changes[$argv[$i]] = $argv[$i + 1];
      for($i = 0; $i < count($lines); ++$i) {
        $cline = explode(SEPARATOR, $lines[$i]);
        $changeThisLine = true;
        foreach($conditions as $k => $v)
          if($cline[$k] != $v)
            $changeThisLine = false;
        if(!$changeThisLine) continue;
        foreach($changes as $k => $v)
          $cline[$k] = $v;
        $lines[$i] = implode(SEPARATOR, $cline);
        ++$ret;
      }
      $f = fopen(DATABASE_FILE, 'w');
      fwrite($f, str_replace(LINEBREAK . LINEBREAK, LINEBREAK, implode("\n", $lines)));
      fclose($f);
      return $ret;
    }
    updateUser(USERNAME, 'Shachi', COUNTRY, 'nepal', SEPARATOR, COUNTRY, 'Nepal', EMAIL, 'some.new@email-address.com');
    ?>
    and my db file looks like this:

    Code:
    Test,test,test@test.com
    testuser,test,test@user.com
    Shachi,test,shachibista@gmail.com,nepal
    :'(

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

    Default

    Aha, seems we had a few extraneous linebreaks hanging around:
    Code:
    <?php
    define('DATABASE_FILE', 'database.php');
    define('SEPARATOR', ',');
    define('LINEBREAK', "\n");
    
    define('USERNAME', 0);
    define('PASSWORD', 1);
    define('EMAIL', 2);
    define('COUNTRY', 3);
    define('ZIP', 4);
    
    function updateUser() {
      $ret = 0;
      $lines = array_unique(file(DATABASE_FILE));
      $argv = func_get_args();
      $conditions = array();
      $changes = array();
      for($i = 0; $i < count($argv) && $argv[$i] !== SEPARATOR; $i += 2)
        $conditions[$argv[$i]] = $argv[$i + 1];
      for(++$i; $i < count($argv); $i += 2)
        $changes[$argv[$i]] = $argv[$i + 1];
      for($i = 0; $i < count($lines); ++$i) {
        $cline = explode(SEPARATOR, str_replace(LINEBREAK, '', $lines[$i]));
        if(count($cline) < 2) continue;
        $changeThisLine = true;
        foreach($conditions as $k => $v)
          if($cline[$k] != $v)
            $changeThisLine = false;
        if(!$changeThisLine) continue;
        foreach($changes as $k => $v)
          $cline[$k] = $v;
        $lines[$i] = implode(SEPARATOR, $cline);
        ++$ret;
      }
      $f = fopen(DATABASE_FILE, 'w');
      fwrite($f, str_replace(LINEBREAK . LINEBREAK, LINEBREAK, implode("\n", $lines)));
      fclose($f);
      return $ret;
    }
    updateUser(USERNAME, 'Shachi', COUNTRY, 'nepal', SEPARATOR, COUNTRY, 'Nepal', EMAIL, 'some.new@email-address.com');
    ?>
    The syntax is, for example:
    Code:
    updateUser(condition_column_1, condition_value_1, condition_column_2, condition_value_2, SEPARATOR, column_1, new_value_1, column_2, new_value_2)
    ... which would be similar to the SQL:
    Code:
    UPDATE table SET column_1=new_value_1, column_2=new_value_2 WHERE condition_column_1=condition_value_1 AND condition_column_2=condition_value_2;
    Last edited by Twey; 07-17-2006 at 10:08 PM.
    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!

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

    Default

    Twey it does what I want. Can I add more arguements?? like zip, 'somevalue' ??Thanks.

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

    Default

    Yes, you can add as many column/value pairs as you like.
    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!

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

    Default

    Then it works just fine, but I have another question, Can the function have only one arguement instead of two and will it also work fine??

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

    Default

    For each column to be checked or altered, it needs two arguments: a column number (provided by the defines) and a value. You should be able to miss out all the arguments on either side of the SEPARATOR without problems, though.
    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!

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

    Default

    Thank you very much Twey, for all your support and help and time. It worked like a dream.

    Thank you again!!!

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

    Default

    No problem -- I even learnt a bit in the process.
    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
  •