
-----------------------------------
Amailer
Fri Mar 12, 2004 8:21 pm

Unique hits
-----------------------------------

// Here we check if the user is asking to view
// the forum or not.

define('IN_PHPBB', true);
define('IN_JOR', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.' . $phpEx);

// Here we are going to get/set the site's unique hits (also delete them if they are older then a week).

$page = $_SERVER["REQUEST_URI"]; // get the page the user is at

if ($page == '/') {
	$page = 'index.php'; // The page
} //
$page = str_replace("/", "", $page); // ^

$hits = array();
$sql = "SELECT * FROM " . UNIQUE_HITS_TABLE;

if (!$result = $db->sql_query($sql)) {
	error_message(GENERAL_ERROR, "Couldn't get Forum/Category information", "", __LINE__, __FILE__, $sql);
} 

if ($total_hits = $db->sql_numrows($result)) {
	$hits = $db->sql_fetchrowset($result);
} 

$week_date = 60 * 60 * 24 * 7; // The seconds for a week!

for($j = 0; $j < $total_hits; $j++) {
	
	if (($hits[$j]['hits_time'] * (60 * 60 * 24 * 7)) sql_query($sql)) {
			error_message(GENERAL_ERROR, "Couldn't delete unique hit", "", __LINE__, __FILE__, $sql);
		} 
	} 

	if ($user_ip == $hits[$j]['hits_ip']) {
		$set_2 = '0';
	} else {
		$set_2 = '1';
	}

} 


Okay, i got a problem, all the work im checking in the for() loop get's messed up because each and every variables value get's repeated....

can anyone help me?

-----------------------------------
wtd
Fri Mar 12, 2004 9:21 pm


-----------------------------------
First I'd replace that for loop with something like:

foreach ($hits as $hit)
{
   if (($hit['hits_time'] * (60 * 60 * 24 * 7)) sql_query($sql)) 
      {
         error_message(GENERAL_ERROR, "Couldn't delete unique hit", "", __LINE__, __FILE__, $sql);
      }

      // it's possible something like this
      // could be replaced with:
      // $result = $db->sql_query($sql) or error_message(GENERAL_ERROR, "Couldn't delete unique hit", "", __LINE__, __FILE__, $sql);
   }

   $set_2 = $user_ip == $hit['hits_ip'] ? 0 : 1;
}

-----------------------------------
Amailer
Fri Mar 12, 2004 10:20 pm


-----------------------------------
see the problem now is

$set_2 = $user_ip == $hit['hits_ip'] ? 1 : 0;


the $user_ip is getting repeated for every record in the database :S

so.. it's basicallt the same thing  :cry:

-----------------------------------
wtd
Fri Mar 12, 2004 10:36 pm


-----------------------------------
That's because you don't change $user_ip in the loop.

-----------------------------------
Amailer
Fri Mar 12, 2004 10:45 pm


-----------------------------------
:eh: errr, yes-- and the way to fix this is?

-----------------------------------
wtd
Fri Mar 12, 2004 11:07 pm


-----------------------------------
Somewhere inside your loop put:

$user_ip = something

The something depends on what kind of information you want to store in $user_ip.

-----------------------------------
Amailer
Sat Mar 13, 2004 10:15 am


-----------------------------------
no no no no, user_ip is already set -.- *duh*,
it's the encoded ip address of the user viisting the page-- but the script wrong woth because if you see... it' keeps duplicating the user_ip
(ie c0394101c0394101c0394101c0394101c0394101c0394101c0394101 )
that means the if statement does not work--- because there is never going to  be such a long ip in the database (cuz it get's repeated for every record in the db...)

that's the first problem it's self :S

btw,
there is an if statement saying
if ($set_2 == 1) {
   do bla


but because of this problem, the $set_2 is always 1 !!!!

-----------------------------------
Amailer
Sun Mar 14, 2004 8:52 am


-----------------------------------
OKAY.
Well i did solve the problem this way

//
// Here we are going to get/set the site's unique hits 
// (also delete them if they are older then a week).
//

$page = $_SERVER["REQUEST_URI"]; // get the page the user is at

if ($page == '/') {
	$page = 'index.php'; // The page
} //
$page = str_replace("/", "", $page); // ^

$sql = "SELECT * FROM " . UNIQUE_HITS_TABLE . " WHERE hits_ip='$user_ip'";

if (!$result = $db->sql_query($sql)) {
	error_message(GENERAL_ERROR, "Couldn't get unique hits information", "", __LINE__, __FILE__, $sql);
} 

if (mysql_affected_rows() == 0) {
	$set_2 = 1;
} else {
	$set_2 = 0;
}


$unique_hits = $db->select_table(UNIQUE_HITS_TABLE);


foreach ($unique_hits as $hit) {
	
   if (($hit['hits_time'] * (60 * 60 * 24 * 7)) sql_query($sql))
      {
         error_message(GENERAL_ERROR, "Couldn't delete unique hits", "", __LINE__, __FILE__, $sql);
      }

      // it's possible something like this
      // could be replaced with:
      // $result = $db->sql_query($sql) or error_message(GENERAL_ERROR, "Couldn't delete unique hit", "", __LINE__, __FILE__, $sql);
   }

} 


but i don't like that way :S so anyone know another way TELL me

-----------------------------------
wtd
Sun Mar 14, 2004 10:09 am


-----------------------------------
PHP makes me want to turn into the Hulk and smash things.  Since everything is dependent on global variables, it's very hard to help you without seeing all of the code.

-----------------------------------
Amailer
Sun Mar 14, 2004 11:02 am


-----------------------------------

$user_ip = 'c0101010';

//
// Here we are going to get/set the site's unique hits 
// (also delete them if they are older then a week).
//

$page = $_SERVER["REQUEST_URI"]; // get the page the user is at

if ($page == '/') {
	$page = 'index.php'; // The page
} //
$page = str_replace("/", "", $page); // ^

$sql = "SELECT * FROM " . UNIQUE_HITS_TABLE . " WHERE hits_ip='$user_ip'";

if (!$result = $db->sql_query($sql)) {
	error_message(GENERAL_ERROR, "Couldn't get unique hits information", "", __LINE__, __FILE__, $sql);
} 

if (mysql_affected_rows() == 0) {
	$set_2 = 1;
} else {
	$set_2 = 0;
}


$unique_hits = $db->select_table(UNIQUE_HITS_TABLE);


foreach ($unique_hits as $hit) {

   //
   // Now to check if the IP has been in the database for the past week
   // if it is delete it from the database (this is because this counter counts the ips for the past week)
   //
   if (($hit['hits_time'] * (60 * 60 * 24 * 7)) sql_query($sql))
      {
         error_message(GENERAL_ERROR, "Couldn't delete unique hits", "", __LINE__, __FILE__, $sql);
      }

   }

} 

$cookiename = 'gfxpro_'; // Cookie name for the sites cookies ;D

if (!isset($HTTP_COOKIE_VARS[$cookiename . 'unique'])) {
	if ($set_2 == 1) {
		$time = time();

		setcookie($cookiename . 'unique', serialize($time), time() + (60 * 60 * 24 * 7), $board_config['cookie_path'], $board_config['cookie_domain'], $board_config['cookie_secure']);

		$sql = "INSERT INTO " . UNIQUE_HITS_TABLE . " (hits_ip, hits_page, hits_time)
		          VALUES ('" . str_replace("\'", "''", $user_ip) . "', '" . str_replace("\'", "''", $page) . "', '$time')";

		if (!$result = $db->sql_query($sql)) {
			error_message(GENERAL_ERROR, 'Could not insert information into ' . UNIQUE_HITS_TABLE . ' table');
		} 

		$is_unique = true;
	} 
} 



Now, no need to see the OO code, cuz it's big -- so i think you should get the rest...(come on it's simple!)

$unique_hits = $db->select_table(UNIQUE_HITS_TABLE);

selects the table...

           $query = "SELECT * FROM " . $table;
           $result = mysql_query($query);
          	while ($row = mysql_fetch_array($result)) {
		           $res[] = $row;
	        }
	  return $res;


that.. k?

-----------------------------------
Homer_simpson
Wed Mar 17, 2004 4:53 am


-----------------------------------
yo i remember i used to have some code like that in my old website for downloads or something....
here's how it was:
i had a row for every download file...
everytime an ip would try downloading them it would check the existing ips and if they were'nt there it would just add it to the ip string in my database...
here's how the ip string worked:
ipÃžipÃžipÃžipÃžip  ie 24.24.65.78Ãž54.698.32.12Ãž01.54.38.65Ãž
i just used Ãž for separator... and it worked perfectly fine...
so u might wanna give that a try...

-----------------------------------
Amailer
Wed Mar 17, 2004 9:15 am


-----------------------------------
I fixed the unique hits problem but you gave me an idea for my download module... insted of using cookies  :oops:  i can do that,
thanks ;D

-----------------------------------
Homer_simpson
Thu Mar 18, 2004 3:14 am


-----------------------------------
netime...
if u need i could send u source code for my whole website...
it had some code parts that might come to use...

-----------------------------------
Amailer
Thu Mar 18, 2004 8:51 am


-----------------------------------
sure, why not  :)

-----------------------------------
Homer_simpson
Thu Mar 18, 2004 2:14 pm


-----------------------------------
pm me yer msn...
btw site address is: http://evilgamerz.sytes.net

-----------------------------------
wtd
Thu Mar 18, 2004 3:30 pm


-----------------------------------
Yes, cookies are evil.  The only reason you should use cookies is to hold an identifying number for a session.  Sessions are essentially cookies stored on the server, where they can't get corrupted or lost by users.
