Page 1 of 2
Help me in PHP Im quite new
Posted: Wed Aug 25, 2004 12:25 am
by PHPScripter
hi my problem is making making a counter if the the name is already used
The script looks works like this
check.php?user=HISNAME
Code: Select all
<?php
$username = $_GET['user'];
// Logs all the ?user=name's
$file = "log.html";
$open = fopen($file, "a");
fwrite($open, "$username<br>");
fclose($open);
?>
how do i make the same ones add up?
http://runescapesig.clawz.com/log.html thats the log see how big it gets

i just resetted it 6 mins ago
[google][php_man][/php_man]
Posted: Wed Aug 25, 2004 12:35 am
by feyd
it'd be better to use a database, esp with that many people hitting the page that often.. otherwise you risk having LOTS of collisions with adding names..
anyway, so here's the basics...
I'd use [php_man]file_get_contents[/php_man] to load in the file. Then I'd combo the username into [php_man]preg_match[/php_man] with [php_man]preg_quote[/php_man]. Afterwhich, if the user isn't in the "database", I'd add them on the end. If they are in the "database", I'd [php_man]preg_replace[/php_man] them adding/fixing the number up one..
Posted: Wed Aug 25, 2004 12:37 am
by PHPScripter
Man... To hard soo much functions
Posted: Wed Aug 25, 2004 12:46 am
by feyd
if you didn't see my edit.. look up 
Posted: Wed Aug 25, 2004 12:50 am
by PHPScripter
Ive seen

Posted: Wed Aug 25, 2004 1:00 am
by feyd
it's not all that bad... something like this:
Code: Select all
<?php
$data = file_get_contents('log.html');
if(preg_match('#\b(' . preg_quote($user,'#') . "\t)(\d+)<br />#i", $data,$matches))
$data = str_replace($matches[0],$matches[1] . (++$matches[2]), $data);
else
$data .= "$user\t1<br />";
?>
Posted: Wed Aug 25, 2004 1:02 am
by PHPScripter
than the $data i need to call in fwrite ?
Code: Select all
<?php
<?php
$username = $_GET['user'];
// Logs all the ?user=name's
$file = "log.html";
$open = fopen($file, "a");
$data = file_get_contents('log.html');
if(preg_match('#\b(' . preg_quote($user,'#') . "\t)(\d+)<br />#i", $data,$matches))
$data = str_replace($matches[0],$matches[1] . (++$matches[2]), $data);
else
$data .= "$user\t1<br />";
fwrite($open, $data .);
fclose($open);
?>
?>
Posted: Wed Aug 25, 2004 1:03 am
by John Cartwright
Why don't you try for yousefl

Posted: Wed Aug 25, 2004 1:04 am
by feyd
you'll have to fiddle with it..
Posted: Wed Aug 25, 2004 1:06 am
by PHPScripter
hey hey im new..
o hmm i just add this after fwrite thats how i see it
Posted: Wed Aug 25, 2004 1:14 am
by d3ad1ysp0rk
actually, since he used $user a few times in the code, I assume he meant to have this line instead:
Code: Select all
$user = $_GET['user'];
//instead of
$username = $_GET['user'];
Posted: Wed Aug 25, 2004 1:16 am
by PHPScripter
okay
Posted: Wed Aug 25, 2004 1:29 am
by PHPScripter
lol sorry for double posting but wow it messed up BIG TIME i got a 783 MB file in my ftp LOL in a matter of seconds after resetting
http://runescapesig.clawz.com/log.html
O well i guess i dont need a log page i just wanted to see spy on some people to see if they still play

Hehe but ok i guess it was fun
Posted: Wed Aug 25, 2004 1:36 am
by feyd
you're probably still always appending to the file.. instead of overwriting it..
Posted: Wed Aug 25, 2004 1:37 am
by PHPScripter
I have no clue i just took ur code under the fwrite code and saved the script
and i renamed everything that is $username to $user
what should i change "a" to so it will overwrite? "ow" ?
lol i thought that a was useless
Ok Thanks i found a little guide
r - open for reading
w - open for writing (file need not exist)
a - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)
Meh now it shows only 1 name per log