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
Printable View
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
I'm not sure, but try this:
$Str .= $Newval, wich is a shorter $str = $str.$Newval
str and newval are variables, right?
you mean... the one in php acts differently than the one in asp? hmmm. :(Quote:
Originally Posted by |2eM!x
Turns out somethings wrong & i dont think its that. Here is my Code:
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 returnPHP 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>
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
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. :D
what should i use instead?
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? :(
not really..but i know i could use fwrite in the loop
since as usual i don't have anything to do.. I could try it out here. ;)
I'm sorry... I read the prob again..
so your problem here is that it is not overwriting but appending right?
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
The problem lies in your loop. You are using a foreach loop with a counter variable. :confused:
If you are using a counter variable then you need to use a for loop:
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 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";
}
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.
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.
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. :(
And i dont understand whats wrong! Ive never been so confused. I wish there was a way to walk thru this like other languages
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:
Which will be the IP portion of the line.PHP Code:$entry[$Number]="{$Number2[0]}:$NumVisits";
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. :D
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($handle, 0);
rewind($handle);
fwrite($handle,trim($Str));
}
fclose($handle);
?>
</body>
</form>
</html>
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. :D... but it still retains most of the idea of
your code. :D