Jump to content

Need help with a simple code !


vxdesigns

Recommended Posts

Hey people how's life treating ya, i'm hoping everyone's having a good weekend.
I have a very simple PHP stats script which counts " Page hits, Unique hits, Todays Page hits, Todays unique hits" Here's the coding for it

[code]<body>
<font face="Verdana" size="1">
<?php
// Our log file;
$counter = "stats.txt";

// Date logging;
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$current_date = $mday . $month . $year;


// Log visit;
$fp = fopen($counter, "a");
$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

// Read log file into array;
$contents = file($counter);

// Total hits;
$total_hits = sizeof($contents);

// Total hosts;
$total_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
array_push($total_hosts, $entry[0]);
}
$total_hosts_size = sizeof(array_unique($total_hosts));

// Daily hits;
$daily_hits = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hits, $entry[0]);
}
}
$daily_hits_size = sizeof($daily_hits);

// Daily hosts;
$daily_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hosts, $entry[0]);
}
}
$daily_hosts_size = sizeof(array_unique($daily_hosts));

?>
<? echo "
Page hits:<b> " . $total_hits . "</b><br><br>
Unique hits: <b> " . $total_hosts_size . "</b><br><br>
Todays Page hits: <b> " . $daily_hits_size . "</b><br><br>
Todays unique hits: <b>" . $daily_hosts_size;
?>[/code]

It counts the page hits properly but it does not count the unique page hits properly meaning it doesn't count the unique page hits after the first unique hit although i;ve asked people in different areas with different ips to visit the page so i can be sure that something is wrong with the code.

Page hits: 10

Unique hits: 1

Todays Page hits: 10

Todays unique hits: 1

You see it keeps counting page hits but doesn't count uniques after the first one. I'm assuming somethings wrong with the coding, so please can someone take a look at the following coding and provide a fix ? I will greatly greatly appreciate it !
Link to comment
https://forums.phpfreaks.com/topic/35177-need-help-with-a-simple-code/
Share on other sites

[quote author=jesirose link=topic=123455.msg510296#msg510296 date=1169441281]
Can you post the contents of the files?
[/quote]

That's all the content, i will repost them.

There are two files

1)index1.php
2)stats.txt

Code for index1.php

[code]<body>
<font face="Verdana" size="1">
<?php
// Our log file;
$counter = "stats.txt";

// Date logging;
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$current_date = $mday . $month . $year;


// Log visit;
$fp = fopen($counter, "a");
$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

// Read log file into array;
$contents = file($counter);

// Total hits;
$total_hits = sizeof($contents);

// Total hosts;
$total_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
array_push($total_hosts, $entry[0]);
}
$total_hosts_size = sizeof(array_unique($total_hosts));

// Daily hits;
$daily_hits = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hits, $entry[0]);
}
}
$daily_hits_size = sizeof($daily_hits);

// Daily hosts;
$daily_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hosts, $entry[0]);
}
}
$daily_hosts_size = sizeof(array_unique($daily_hosts));

?>
<? echo "
Page hits:<b> " . $total_hits . "</b><br><br>
Unique hits: <b> " . $total_hosts_size . "</b><br><br>
Todays Page hits: <b> " . $daily_hits_size . "</b><br><br>
Todays unique hits: <b>" . $daily_hosts_size;
?>
[/code]

The stats.txt file is empty. I took this script from www.zymic.com and you can download it by following this link
http://zymic.com/download_download.php?id=23

You can see this script in action here

http://www.pwnagetime.net/test/index1.php

Now the issue as i've mentioned before is that the script keeps counting regular hits/refreshes but doesn't count a unique hit after the first one. I've asked my friends to visit the test page and also used different proxy services to view the page with a new ip and still it doesn't count the unique hit. Zymic forums are down and their irc channel is good as dead, so i seek help from the professionals. Please help me out and i will love you forever !
[quote author=jesirose link=topic=123455.msg511139#msg511139 date=1169530609]
It can't be empty if it's showing anything. It may have been empty when you uploaded it, but it has been written to and isn't empty anymore. I wanted to see that file.
[/quote]

This is what's inside the stats.txt file

[code]|23January2007
|23January2007
|23January2007
|23January2007
|23January2007
|23January2007
|23January2007
|23January2007
|23January2007
|23January2007
[/code]

Hope this can help you figure out what's wrong :/
It's not storing any hosts, or there would be something before the | <- Pipe.

Here is the code I am talking about
[code]
$total_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
array_push($total_hosts, $entry[0]);
}
$total_hosts_size = sizeof(array_unique($total_hosts));
[/code]


This explodes the info based on the | - whatever is before the | is the host. None of yours have anything before the | so it is all the same (or none?) host.

$REMOTE_ADDR is not working. Where does that var come from? Here is the script I use to get an IP address:
[code]if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip=$_SERVER["REMOTE_ADDR"];
}else{
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
}[/code]
I think it's getting the host/ip info by the following method

// Log visit;
$fp = fopen($counter, "a");
[b]$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";[/b]
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

Do you see any problem with that ? How can i implement your method of getting ip/host information into this script ?
One more thing i'd like to know is that can you download this script from here

http://zymic.com/download_download.php?id=23

And test it on your host to see if it works because lately alot of simple scripts have started to give me error on my new host, so i want to make sure that if it's my host who has disabled something or not because of which i'm running into issues.Thanks in advance
So where exactly do i put this code ???

[code]if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip=$_SERVER["REMOTE_ADDR"];
}else{
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
}[/code]

Shall i do it like this

// Log visit;
$fp = fopen($counter, "a");
$line = if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip=$_SERVER["REMOTE_ADDR"];
}else{
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
} "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);
No. You might want to read some basics tutorials, you can't assign an if statement to a var. (AFAIK!)

[code]
if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip=$_SERVER["REMOTE_ADDR"];
}else{
$ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
}

// Log visit;
$fp = fopen($counter, "a");
$line = $ip . '|' . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);
[/code]
Okie i think i found another bug/issue with the script. The problem is that it stops counting unique hits after the first 5 unique hits whereas it still keeps counting page refreshes/hits. What could be causing this ??? Any fix will be greatly appreciated !

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.