Log in

View Full Version : Another Simple thing



bluewalrus
01-18-2009, 10:18 PM
I'm trying to make another simple thing like the simple mailer but a simple page counter. I want the counter to write the number with the users ip address and computer information. I have this code but for some reason it has "Resource ID #3." I dont know where it's getting the Resource ID # from or the 3 the file was set to 0. This is my code thanks for any help you can offer here.



<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
$count = "hit.txt";
$value = fopen($count, "r+");
$entervalue = fread($value, 999999);
$entervalue = entervalue + 1;
fwrite($value, $value);
fclose($value);
$hitsfile = "log.txt";
$gethits = fopen($hitsfile, "r+");
$page = $value . ". " . $user . " " . $computer . "\n";
echo $page;
fwrite($gethits, $page);
fclose($gethits);
?>

Master_script_maker
01-18-2009, 10:43 PM
try changing this: fwrite($value, $value); to this:fwrite($value, $entervalue); and this:$page = $value . ". " . $user . " " . $computer . "\n"; to this:$page = $entervalue . ". " . $user . " " . $computer . "\n";

Nile
01-18-2009, 11:28 PM
If this helps you can base if off these:
http://www.dynamicdrive.com/forums/showpost.php?p=146859&postcount=2
http://www.dynamicdrive.com/forums/showpost.php?p=145816&postcount=7

bluewalrus
01-19-2009, 12:48 AM
Okay I got it working now but decided to try and make it work better again hah. So I added in an attempt to see if a user has been to the site before but now it thinks the user has been there every time. Thanks for any further help you can offer with this

<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
//To Change the date formate from MM/DD/YY HH:MM go here http://www.php.net/date
$date = date('m/j/y H:i');
$count = "hit.txt";
$value2 = fopen($count, "r+");
$entervalue = fread($value2, 999999999);
$entervalue = $entervalue + 1;
fclose($value2);
$value = fopen($count, "w+");
fwrite($value, $entervalue);
fclose($value);
$hitsfile = "log.txt";
$gethits = fopen($hitsfile, "a+");
$check = fread($gethits, 9999999999);
$page = $entervalue . ". " . $date . " " . $user . " " . $computer . "\n";
//REMOVE ALL OF THIS IF YOU DON'T CARE IF A USER HAS BEEN HERE MORE THAN ONCE IN A DAY... REMOVE THAT . " " IF YOU WANT TO BE MORE SPECIFIC TO IF THEY HAVE EVER BEEN THERE.
$beenhere = str_replace($check, "", $date . " " . $user);
if ($date . " " . $user === $beenhere) {
$entervalue = $entervalue - 1;
die ("You've been here today.");
}
else {
//REMOVE UNTIL HERE
fwrite($gethits, $page);
fclose($gethits);
}
?>

Master_script_maker
01-19-2009, 02:26 AM
try:

<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
//To Change the date formate from MM/DD/YY HH:MM go here http://www.php.net/date
$date = date('m/j/y H:i');
$count = "hit.txt";
$value2 = fopen($count, "r+");
$entervalue = fread($value2, 999999999);
$entervalue = $entervalue + 1;
fclose($value2);
$value = fopen($count, "w+");
fwrite($value, $entervalue);
fclose($value);
$hitsfile = "log.txt";
$gethits = fopen($hitsfile, "a+");
$check = fread($gethits, 9999999999);
$page = $entervalue . ". " . $date . " " . $user . " " . $computer . "\n";
//REMOVE ALL OF THIS IF YOU DON'T CARE IF A USER HAS BEEN HERE MORE THAN ONCE IN A DAY... REMOVE THAT . " " IF YOU WANT TO BE MORE SPECIFIC TO IF THEY HAVE EVER BEEN THERE.
$beenhere = preg_match("/".$user."/", $check);
if ($beenhere) {
$entervalue = $entervalue - 1;
die ("You've been here today.");
}
else {
//REMOVE UNTIL HERE
fwrite($gethits, $page);
fclose($gethits);
}
?>

bluewalrus
01-19-2009, 05:29 PM
Yup that did it thanks.

If anyone comes here looking for this the whole code is below. I had to add in the rewriting to the text file the count for the if the user had been there before. So it is as follows.



<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
//To Change the date formate from MM/DD/YY HH:MM go here http://www.php.net/date
$date = date('m/j/y H:i');
$count = "hit.txt";
$value2 = fopen($count, "r+");
$entervalue = fread($value2, 999999999);
$entervalue = $entervalue + 1;
fclose($value2);
$value = fopen($count, "w+");
fwrite($value, $entervalue);
fclose($value);
$hitsfile = "log.txt";
$gethits = fopen($hitsfile, "a+");
$check = fread($gethits, 9999999999);
$page = $entervalue . ". " . $date . " " . $user . " " . $computer . "\n";
//REMOVE ALL OF THIS IF YOU DON'T CARE IF A USER HAS BEEN HERE MORE THAN ONCE IN A DAY... REMOVE THAT . " " IF YOU WANT TO BE MORE SPECIFIC TO IF THEY HAVE EVER BEEN THERE.
$beenhere = preg_match("/".$user."/", $check);
if ($beenhere) {
$entervalue = $entervalue - 1;
$value = fopen($count, "w+");
fwrite($value, $entervalue);
fclose($value);
die ("You've been here today.");
}
else {
//REMOVE UNTIL HERE
fwrite($gethits, $page);
fclose($gethits);
}
?>

Master_script_maker
01-19-2009, 07:45 PM
glad i could help :)

Twey
01-19-2009, 08:09 PM
Nice how you can disable the IP addresses of people you don't like by putting them in your user-agent string and then accessing the page :) And that IP addresses that contain other IP addresses will be considered the same address (e.g. 123.123.123.123 and 3.123.123.1). Also note how the script tries to read from the hits file 999999990 bytes more than are ever likely to be used, how if two users access the page at the same time it's likely that only one will be recorded, how regular expressions are used for a simple substring match, and how instead of just checking whether the user has been there before updating the hit counter, the script always updates the hit counter but then goes back and decreases it again...

Also, don't use locale-specific date formats in code, especially not the crazy American one — use ISO-8601 standard date format, DD-MM-YYYY (or 'N-m-Y').

bluewalrus
01-19-2009, 11:47 PM
Yea I was considering going back through and reverse it so that it checked first to see if the user had been there then functioned the idea for that came one I first got it working. What do you mean by disabling the ip addresses with the users-agent string?

Also I noticed the checker I had isn't checking the date and user it's just checking the user so I just tried chaning to to this


$beenhere = preg_match("/".$date." ".$user."/", $check);

but I got this:
Warning: preg_match(): Unknown modifier '1' in /hsphere/local/home/crazychr/bluewalrus.net/hitter/hit.php on line 19 Is it because of the " "?

Is there an infinite function so it will read the whole file no matter the size?

Master_script_maker
01-20-2009, 02:27 AM
I've rewritten your code using the function file_get_contents() which retrieves the whole file.
<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
$date = date('N-m-Y');
$counter = "hit.txt";
$cData = file_get_contents($counter);

$logfile = "log.txt";
$log = file_get_contents($logfile);
$page = $cData . ". " . $date . " " . $user . " " . $computer . "\n";
$beenhere = str_replace($date." ".$user, '', $log);
if ($beenhere===$log) {
$cData = $cData + 1;
file_put_contents($logfile, $log.$page);
file_put_contents($counter, $cData);
} else {
file_put_contents($counter, $cData);
die ("You've been here today.");
}
?>

bluewalrus
01-20-2009, 02:45 AM
oo that does look a lot more efficient thanks, but i'm getting this error message when I try it.



Fatal error: Call to undefined function: file_put_contents() in /hsphere/local/home/crazychr/bluewalrus.net/hitter/hit.php on line 14

Twey
01-20-2009, 01:41 PM
That means that you're still running PHP4. PHP5 has been out for five years now: upgrade, tell your host to upgrade, or else switch hosts.

In the meantime, here's a replacement (but it only supports the first two arguments):
if (!function_exists('file_put_contents')) {
function file_put_contents($fn, $data) {
$f = fopen($fn, 'w');
fwrite($f, $data);
fclose($f);
}
}

However, note that this doesn't help much against most of the problems there (and will still cause the script to break if the log file gets particularly big). For something like this you really want to use perhaps a small SQLite database.

bluewalrus
01-21-2009, 04:49 AM
Okay I have it working now but a terrible way and with the date formatting even sloppier than before. I can't switch to php 5 because my server will make me cancel my other accounts and resign up. They said to could take 2 weeks to do this. So I'm trying it my moderately? poorly coded way.
This works

$date1 = date('Hi');
$date2 = date('mjy');


This brings up
Warning: preg_match(): Unknown modifier 'j' in /hsphere/local/home/crazychr/bluewalrus.net/hitter/hit.php on line 20


date1 = date('H\:i');
$date2 = date('m\/j\/y');

And this bring up this

Warning: preg_match(): Unknown modifier '2' in /hsphere/local/home/crazychr/bluewalrus.net/hitter/hit.php on line 20


$date1 = date('H/:i');
$date2 = date('m\/j\/y');

I was thinking I needed to clear out the : and / so I tried those other two ways but with no luck. I didn't change the code to the formatting you suggested twey because I and the users of this only use the crazy american date formating and for the first 12 days of the month it could be confusing to look at.

The whole code as I have it working....


<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
//To Change the date formate from MM/DD/YY HH:MM go here http://www.php.net/date
$date1 = date('Hi');
$date2 = date('mjy');
$count = "hit.txt";
$value2 = fopen($count, "r+");
$entervalue = fread($value2, 999999999);
$entervalue = $entervalue + 1;
fclose($value2);
$value = fopen($count, "w+");
fwrite($value, $entervalue);
fclose($value);
$hitsfile = "log.txt";
$gethits = fopen($hitsfile, "a+");
$check = fread($gethits, 9999999999);
$page = $entervalue . ". " . $date1 . $date2 . " " . $user . " " . $computer . "\n";
//REMOVE ALL OF THIS IF YOU DON'T CARE IF A USER HAS BEEN HERE MORE THAN ONCE IN A DAY. IF YOU WANT TO BE MORE SPECIFIC TO IF THEY HAVE EVER BEEN THERE. REMOVE "$date . " " ." If you
$beenhere = preg_match("/".$date2."/", $check);
$beenhere2 = preg_match("/".$user."/", $check);
if ($beenhere) {
if ($beenhere2) {
$entervalue = $entervalue - 1;
$value = fopen($count, "w+");
fwrite($value, $entervalue);
fclose($value);
die ("You've been here today.");
}
}
else {
//REMOVE UNTIL HERE
fwrite($gethits, $page);
fclose($gethits);
}
?>


Thanks for anymore help you can offer.

bluewalrus
01-23-2009, 01:39 AM
Okay I'm trying to redo it again and I think this new way is more efficient except for the text file size problem anyway. Thanks for any more help you can offer here. I tried making the viewer check if the visitor has been there in the past 10 minutes rather than the day also.


<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
//To Change the date formate from MM/DD/YY HH:MM go here http://www.php.net/date
$day = date('z');
$year = date('Y');
$hour = date('H');
$seconds = date('s');
$minutes = date('i');
$minutes = $minutes * 60;
$hour = $hour * 3600;
$time = $hour + $minutes + $seconds;
$date1 = date('H:i');
$date2 = date('m.j.y');
$hitsfile = "log.txt";
$gethits = fopen($hitsfile, "a+");
$check = fread($gethits, 9999999999);
fclose($gethits);
if ($check =< $time + 600 . " " . $user ) {
$count = "hit.txt";
$value2 = fopen($count, "r+");
$entervalue = fread($value2, 999999999);
fclose($value2);
echo "<div id=\"counter\">" . $entervalue . "</div>\n</div></div></html>";
die ;
}
else {
$hitsfile = "log.txt";
$entervalue = $entervalue + 1;
$count = "hit.txt";
$value = fopen($count, "w+");
fwrite($value, $entervalue);
fclose($value);
$page = $entervalue . ". " . $date1 ." " . $date2 . " " . $computer . " " . $time . " " . $user . "\n";
$gethits = fopen($hitsfile, "a+");
fwrite($gethits, $page);
fclose($gethits);
}
?>