Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    fwrite and BOM problem with mode (r+b)


    Hi, you help with this?

    http://paste.ofcode.org/v8M4DwrH7hQzkJMEDjaRpt

    PHP Code:
    <?php
    include("scan_ips_class.php");
    $scanIPs = new Scan_ips;
    ?>
    Go to line 413 - 417, I place there a test block // TEST
    which causes some odd behaviour.

    Test this like this:
    1) make sure file 4.ip was not generated yet or delete it
    2) add die(); before the TEST block.
    3) reload index.php
    Then you see that the file 4.ip is successfully created and the binary content is all right. Mostlikely it will contain many of binary 0000 and one value 0E on first byte.
    4) But if you remove the die command, and repeat then the test block is executed and in the hexadecimal editor it looks like there are only two values: 0ECD . The rest of the file is not visible. But when I check the file it has size 187kB which looks ok. If you dont find error, you can remove the test block and test what it does. You can tell me if you see the same thing as me.

    My goal is to be able to write to the file without any kind of corruption. How to solve the problem?

    I cannot find out why there is unicode BOM when I created the file in x or xb mode. Also when I did first write no problem was visible, but on the second try the BOM dismiss and file looks like coruppted in hexa editor.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    1) Please use [ PHP ] tags around your code. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    2) Do not use pastebin for your code. Post the relevant code here as that is what this forum is for. The built-in formatter also makes the code easier to ready.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0
    This is how it looks before it is changed. It contains unicode BOM - have no idea why. I want to write to binary and it dismiss when I write on second try. Then it looks like corrupted in the hexa editor.



    And this is how it looks when it is written on the second try:


    This is the relevant part:
    PHP Code:
    if ( !$this->FRWBHandler )
        
    $this->FRWBHandler fopen($this->file,"r+b");
    $fh =& $this->FRWBHandler;
    for(
    $i=$segmentBegin$i<=$segmentBegin+$this->segmentLength$i++):
        
    fseek$fh  $i );
    //    echo $i . " ";
        
    $byte fread($fh,1);
    //    echo (" <b>$i:$byte</b><br>");
        
    if ( $byte === "\x00" ){
            
    fseek($fh$i);
        
    // echo "<h2>".$arr[1]." $i</h2>";
        
    fwrite($fhchr($arr[1])); // THIS WAS OK

        // TEST
        
    $result fseek($fh$i+);
        
    fwrite($fhchr($arr[2])); // THIS FAILS or CORRUPTS FILE
        
    fclose($fh);
        die(
    $arr[1]." ".$arr[2]." ".$arr[3]. " result $result");
    ..... 
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    I don't see the '+' format in the PHP documentation. Did you try this?
    PHP Code:
    $this->FRWBHandler fopen($this->file,"rb"); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0
    Originally Posted by gw1500se
    I don't see the '+' format in the PHP documentation. Did you try this?
    PHP Code:
    $this->FRWBHandler fopen($this->file,"rb"); 
    See r+
    r+ .. it opens file for read and write without truncate the file
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0
    Yet I show you working version which is simplified. This uses no class:
    simplified version

    so you can see how it works. The written data do not corrupt the file. But why I cannot use the class to do that?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    This only thing I can see off-hand is that your offset must be wrong.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0
    Offset? Do you mean fseek argument 2? The offset in the class is 0 (first) and the second is 2 . And it should make no problem. The working example (test.php) has the same offset because I did not change there anything. I only exchanged $this->segmentLength for 250 (which is exactly the same) because $segmentBegin starts on 0. The for loop is not completed there is die(). So we have first value 0 and second value 2. ... I thought if there could be some problem with $fh (file handler), but this is what I thought about 20 times. I tried $fh = @fopen($this->file,"r+b"); instead of
    PHP Code:
    $this->FRWBHandler = @fopen($this->file,"r+b"); 
    but the result was the same...
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    May I ask why on earth you store the IPs as binary strings? That's the most cumbersome, slow and error-prone way I could think of.

    Unless you need to feed that data into some kind of low-level microelectronic device, you're definitely doing it wrong.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0
    I think you just don't understand the concept. It is not used to save IPs of visitors but IPs of proxies. Why do you think it is slow? It is supposed to read IPs from calculated location. So when user's IP is eg. 124.64.28.12 I will look to record number 124 (the position is calculared: 250*3*124) and then I (will) read first byte. If the number is equal to 64 I will compare with next byte which is 250*3*124+250 , if the byte is equal to 28 I will read the position 250*3*124+250*2 and compare the byte if it is equal to 12. If all this comparation is true then I will block him because he uses a proxy. I don't think that this is slow. But you did not explain the problem itself. I really would like to know the cause of the problem.
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by h4ever
    Why do you think it is slow?
    Because modern database systems have sophisticated optimization techniques much better than anything you or I will ever come up.



    Originally Posted by h4ever
    It is supposed to read IPs from calculated location. So when user's IP is eg. 124.64.28.12 I will look to record number 124 (the position is calculared: 250*3*124) and then I (will) read first byte. If the number is equal to 64 I will compare with next byte which is 250*3*124+250 , if the byte is equal to 28 I will read the position 250*3*124+250*2 and compare the byte if it is equal to 12.
    Oh lord. Why would you do this? How did you even come up with this stuff?

    No offense, but this looks like someone trying to write assembly code in PHP. If you love to fumble with bits and bytes, use C or something.



    Originally Posted by h4ever
    But you did not explain the problem itself.
    Because I question the whole approach.

    In the world outside of microelectronics, you would simply create a database table for the IP addresses. Some database systems like PostgreSQL even have a special data type for this. Adding and checking addresses is then trivial. It doesn't take more than 5 minutes of programming and 10 lines of code.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0
    But I cannot use it. I don't work on commercial project. This is just for fun. The free hosting which I would like to use has limited use of mysql. Hence I must limit the number of requests to mysql. But you don't solve my problem. This is the approach I choosed. I don't mind to work with bytes. I better make my own methods than playing with this modern approches. I absolutely don't need database. You forget that using database means some delay in connection. Hence it is faster to get what I need from the binary file than to connect and query db. It would still need filter results. This approch is simply immedate to find what I am looking for because I calculate the position. You did not convinced me. However I would be glad if you can devote the time to answer my question than to argue which approch is better. I prefer to gain experience. It's just playing. Don't take it too much seriously.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    You want to gain experience writing inappropriate code rather than best practices? Where would you find that kind of experience useful?

    P.S. You have one of the best here helping you. Pay attention to what he says.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0
    You wanna talk, not help. I already told you that I am limited with the service where I plan to run my app. Hence what are you trying? You try to convince me to use service, which will make my app cause not to be usable.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    You said you were doing this to gain expereince. If that is indeed your goal, move to another provider that gives you standard services, rather than limited and use best practices.

    Comments on this post

    • Jacques1 agrees
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo