Valhalla Legends Forums Archive | Web Development | PHP Counter

AuthorMessageTime
CrAz3D
I'd like to create a simple php counter for my bot.  1 that logs the user's name & how many tiems they've connected.

What I need to know how to do is loop through the database (a .txt file) and see if the username is in it.  If the username IS in the file, it adds to the # that follows their name (increasing the amount of times they've logged on).  If they weren't found, I'd like to add them to the .txt file with a 1 following their name.

Can anyone tell me how to go about this?
Much thanks

EDIT: Something close to this
https://davnit.net/bnet/vL/phpbbs/index.php?topic=4780.msg40054#msg40054



This is what I came up with, it seems to work
[code]<?php

$user = $_SERVER['REMOTE_ADDR'];
//$_REQUEST['user'];
$logfile = "list.txt";

$file1 = file($logfile);

$i = 0;
$text="";
$found= "false";
//LINE 10~!

for ($i = 0; $i < count($file1);$i++){
list ($var1, $var2) = explode(" ", $file1[$i]);

if (strstr(strtolower($var2), strtolower($user)))
{
$var1++;
$file1[$i]="$var1 $var2";
$text = $text . $file1[$i];

$found="true";
}
else
{
$text = $text . $file1[$i];
}
$ledit = @fopen($logfile, "r+");
@fwrite($ledit, $text);
@fclose($ledit);
}

if (strstr($found, "false"))
{
$ledit = @fopen($logfile, "a+");
$towrite="
1 $user";
@fwrite($ledit,$towrite);
@fclose($ledit);
}

?>
[/code]
March 9, 2005, 7:32 PM
The-Rabid-Lord
I found that quite hard to follow and the colors didnt help(Probs cos im tired)/ Maybe it would be easier if you add comments. Seems ok from what I understood.
March 11, 2005, 12:08 AM
CrAz3D
The colors won't leave :(
[code]<?php

$user = $_SERVER['REMOTE_ADDR'];
//$_REQUEST['user'];  //I removed this just for now, may add it in later
$logfile = "list.txt";

$file1 = file($logfile);

//A few predeclared variables
$i = 0;
$text="";
$found= "false";
//LINE 10~! --- This is to help me count lines since I am using notepad

for ($i = 0; $i < count($file1);$i++){  //loops through each line of my file
list ($var1, $var2) = explode(" ", $file1[$i]); //seperates the line by a space into 2 variables

if (strstr(strtolower($var2), strtolower($user)))  //checks to see if var2 is the same as user
{
$var1++; //increases count of the user
$file1[$i]="$var1 $var2";
$text = $text . $file1[$i];

$found="true";
}
else
{
$text = $text . $file1[$i];
}
$ledit = @fopen($logfile, "r+"); //if the user was found, it edits the .txt file
@fwrite($ledit, $text);
@fclose($ledit);
}

if (strstr($found, "false")) //if the user wasn't found, it adds them
{
$ledit = @fopen($logfile, "a+");
$towrite="
1 $user";
@fwrite($ledit,$towrite);
@fclose($ledit);
}

?>[/code]
March 11, 2005, 4:29 AM
R.a.B.B.i.T
[code]<?php
$query = mysql_query("UPDATE site_info SET counter=counter+1");
?>[/code]
March 12, 2005, 3:02 PM
CrAz3D
#1 I don't have MySql installed on my computer
#2 I'm too cheap to buy hosting
#3 I'm too lazy to ask someone for hosting & keep uploading stuff
March 12, 2005, 5:06 PM
St0rm.iD
www.freesql.org

#4 You're too cheap and lazy to have a counter?
March 12, 2005, 11:30 PM
Newby
Write one in perl!
March 25, 2005, 2:46 AM
InSeCuRe
Craz I have a webserver that I own if you want I can help you out...
PM me
  or
AIM me

Suicidal SnIp3r
April 14, 2005, 4:03 PM
Quarantine
I wrote one for Networks Craz3d. If you ask him nicely he will like help you :p. Writes to a text file (using mySQL for something as simplistic as this in my opinion is a waste of time) . Stores the file in an two (or three I forget) arrays
file-> name
        channel
        server
        time (or date I forget)

I'm planning on rewriting it (there was a bug causing duplicate entries not to be removed) I think Networks fixed it though.
                                             
April 15, 2005, 4:33 AM

Search