Help me in PHP Im quite new

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Help me in PHP Im quite new

Post 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 :P i just resetted it 6 mins ago

[google][php_man][/php_man]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Last edited by feyd on Wed Aug 25, 2004 12:39 am, edited 1 time in total.
PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Post by PHPScripter »

Man... To hard soo much functions
Last edited by PHPScripter on Wed Aug 25, 2004 12:48 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you didn't see my edit.. look up :D
PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Post by PHPScripter »

Ive seen :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 />";

?>
PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Post 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);
?>
?>
Last edited by PHPScripter on Wed Aug 25, 2004 1:07 am, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why don't you try for yousefl :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll have to fiddle with it..
PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Post by PHPScripter »

hey hey im new..


o hmm i just add this after fwrite thats how i see it
Last edited by PHPScripter on Wed Aug 25, 2004 1:15 am, edited 1 time in total.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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'];
PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Post by PHPScripter »

okay
PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Post 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 :evil: Hehe but ok i guess it was fun
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're probably still always appending to the file.. instead of overwriting it..
PHPScripter
Forum Newbie
Posts: 9
Joined: Wed Aug 25, 2004 12:25 am

Post 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
Last edited by PHPScripter on Wed Aug 25, 2004 1:48 am, edited 3 times in total.
Post Reply