Results 1 to 19 of 19

Thread: Add to string

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Add to string

    Okay, in vb we use

    Str = Str & Newval

    But if i use that in php, it duplicates the document and adds it repeatedly. I hope someone understands & thanks

  2. #2
    Member Matias's Avatar
    Join Date
    Jun 2005
    Location
    Buenos Aires, Argentina.
    Posts
    43

    Re: Add to string

    I'm not sure, but try this:

    $Str .= $Newval, wich is a shorter $str = $str.$Newval

    str and newval are variables, right?
    ... and the rivers shall open for the righteous, and the rivers shall open for the righteous, and rivers shall open for the righteous, someday ....

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    Quote Originally Posted by |2eM!x
    Okay, in vb we use

    Str = Str & Newval

    But if i use that in php, it duplicates the document and adds it repeatedly. I hope someone understands & thanks
    you mean... the one in php acts differently than the one in asp? hmmm.

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Add to string

    Turns out somethings wrong & i dont think its that. Here is my Code:
    PHP Code:
    <html>
    <form>
    <body>
    <?php
        $ip 
    =  $_SERVER['REMOTE_ADDR'];
       
    $filename "IPTimes.txt";
        
    $handle fopen($filename"r+") or die("Err1");
        
    $contents = @fread($handle,filesize($filename));
            If(
    Strpos($contents,$ip)===False){
            echo 
    "New user..Validating..";
              
    fwrite($handle,$contents."\r\n".$ip.":1");
                    
    $NumVisits 1;
            }Else{
                 
    $entry explode("\r\n",$contents);
                    
    $Number 0;
                    foreach(
    $entry as $x){
                                If(
    strpos($entry[$Number],$ip)===0){
                           
    $Number2 explode(":",$entry[$Number]);
                           
    $Number2['1']++;
                           
    $NumVisits $Number2['1'];
                           
    $entry[$Number]="$ip:$NumVisits";
                                }
                                
    $Str $Str.$entry[$Number]."\r\n";
            Echo 
    $Str."\r\n";
                           
    $Number++;
                    }
            }
        
    //echo $NumVisits;
            
    fwrite($handle,$Str);
            
    fclose($handle);
    ?>
    </body>
    </form>
    </html>
    Now, NumVisits is returning the right number - which is perfect. But when writing, $Str takes on values it shouldnt. For example, in "IPTimes.txt", i have my ip (127.0.0.1:1) followed by times ive visited(:1). But after running the file once, it will return
    127.0.0.1:1127.0.0.1:2

    instead of overwriting that number, it just adds to it?? i dont think the /r/n is working. But sometimes it does add enters?? i got no idea whats wrong

  5. #5
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    perhaps its.. '\r\n'

    that's what I use to go new line..

    but I remember visualAd saying about a function getting the newline of certain pc's.

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Add to string

    what should i use instead?

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    Hmmm.... we'd have to wait for visualAd for that.. I was looking through the dox. and I can't seem to find it. ...

    don't you want to just write it... line per line?

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Add to string

    not really..but i know i could use fwrite in the loop

  9. #9
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    since as usual i don't have anything to do.. I could try it out here.

  10. #10
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    I'm sorry... I read the prob again..

    so your problem here is that it is not overwriting but appending right?

  11. #11

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Add to string

    Im not sure what its doing at all. It returns the right values to me with an echo, but when writing its doing something crazy.

    I think /r/n isnt valid for writing

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Add to string

    The problem lies in your loop. You are using a foreach loop with a counter variable.

    If you are using a counter variable then you need to use a for loop:
    PHP Code:
                    for($number 0$number count($entry); $number++)
                           If(
    strpos($entry[$Number],$ip) === 0){
                               
    $Number2 explode(":",$entry[$Number]);
                               
    $Number2['1']++;
                               
    $NumVisits $Number2['1'];
                               
    $entry[$Number]="$ip:$NumVisits";
                           }

                           
    $Str $Str.$entry[$Number]."\r\n";
                    
                Echo 
    $Str."\r\n";

                    } 
    Also, the file() function returns an array , one for each line, independant of the plat form - so it is better to use this than manually explode the files contents.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  13. #13

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Add to string

    thanks VAd, but whats the difference? Each time the foreach loop is run it adds one to the counter. It seems like it should do the same thing?

    Ill try your code and post results.

  14. #14

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Add to string

    Okay, i dont understand whats wrong at all.

    The first time its run, IPTimes.txt has this in it:127.0.0.1:1
    Second time:127.0.0.1:1127.0.0.1:2
    Third time:
    127.0.0.1:1127.0.0.1:2
    127.0.0.1:1127.0.0.2
    127.0.0.1:1127.0.0.3

    I really dont understand this.

  15. #15
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    this is what is printed on the txt file?

    when I tried ur code, the buggy one above... i didn't get entries that is appended on the same line.

    but still the code is indeed buggy.

  16. #16

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Add to string

    And i dont understand whats wrong! Ive never been so confused. I wish there was a way to walk thru this like other languages

  17. #17
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Add to string

    Its because you are appending the new line to the original line. If you just want the IP address you need to change this line:
    PHP Code:
    $entry[$Number]="{$Number2[0]}:$NumVisits"
    Which will be the IP portion of the line.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  18. #18
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    DONE!!! lol
    Let's just say I got frustrated with the problem and i did edited it.

    also.. remix.. i renamed $x as $ipdata... i guess it got mixed there.

    THIS code is working.. ehehehe.. i'm giving myself a pat in the back.

    PHP Code:
    <html> 
    <form> 
    <body> 
    <?php 
        $ip 
    =  $_SERVER['REMOTE_ADDR']; 
        
    $filename "IPTimes.txt"
        
    $handle fopen($filename"r+") or die("Err1"); 
        
    $contents = @fread($handle,filesize($filename)); 
        If(
    $contents===False){ 
            echo 
    "New user..Validating.."
            
    fwrite($handle,$ip.":1"."\r\n"); 
        }Else{ 
            
    $entry explode("\r\n",$contents); 
            
    $Str "";        
            echo 
    "<BR>Entries=".count($entry);
            foreach(
    $entry as $ipdata){ 
                if ( 
    trim($ipdata) != ""){
                    If(
    strpos(trim($ipdata),$ip)===0){ 
                        
    $ipparts explode(":",$ipdata); 
                        
    $ipparts[1]++; 
                        
    $ipdata implode(":"$ipparts); 
                        echo 
    "<BR>NEW".$ipdata;
                    }
                    
    $Str $Str.$ipdata."\r\n"
                    echo 
    "<BR>".$Str
                }    
            } 
            
    ftruncate($handle0);
            
    rewind($handle);
            
    fwrite($handle,trim($Str)); 
        }     
        
    fclose($handle); 
    ?> 
    </body> 
    </form> 
    </html>

  19. #19
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Add to string

    i even tried placing some dummy ip addresses.. before and after... my local ip... and
    it still worked. .

    What do you think?

    I also removed those variables that I think is not needed.. and renamed
    a few so as to make it clearer. ... but it still retains most of the idea of
    your code.

Posting Permissions

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



Click Here to Expand Forum to Full Width