Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Unique hits
Index -> Programming, PHP -> PHP Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Amailer




PostPosted: Fri Mar 12, 2004 8:21 pm   Post subject: Unique hits

code:

// 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)) <= time()) {
                $hits_ip = $hits[$j]['hits_ip'];

                $sql = "DELETE FROM " . UNIQUE_HITS_TABLE . "
                                       WHERE hits_ip = '$hits_ip'";
                if (!$result = $db->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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Mar 12, 2004 9:21 pm   Post subject: (No subject)

First I'd replace that for loop with something like:

code:
foreach ($hits as $hit)
{
   if (($hit['hits_time'] * (60 * 60 * 24 * 7)) <= time())
   {
      $hits_ip = $hit['hits_ip'];

      $sql = "DELETE FROM " . UNIQUE_HITS_TABLE . "
                   WHERE hits_ip = '$hits_ip'";
      if (!$result = $db->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




PostPosted: Fri Mar 12, 2004 10:20 pm   Post subject: (No subject)

see the problem now is
code:

$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 Crying or Very sad
wtd




PostPosted: Fri Mar 12, 2004 10:36 pm   Post subject: (No subject)

That's because you don't change $user_ip in the loop.
Amailer




PostPosted: Fri Mar 12, 2004 10:45 pm   Post subject: (No subject)

Eh errr, yes-- and the way to fix this is?
wtd




PostPosted: Fri Mar 12, 2004 11:07 pm   Post subject: (No subject)

Somewhere inside your loop put:

code:
$user_ip = something


The something depends on what kind of information you want to store in $user_ip.
Amailer




PostPosted: Sat Mar 13, 2004 10:15 am   Post subject: (No subject)

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




PostPosted: Sun Mar 14, 2004 8:52 am   Post subject: (No subject)

OKAY.
Well i did solve the problem this way
code:

//
// 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)) <= time())
   {
      $hits_ip = $hit['hits_ip'];

      $sql = "DELETE FROM " . UNIQUE_HITS_TABLE . "
                   WHERE hits_ip = '$hits_ip'";
      if (!$result = $db->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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Mar 14, 2004 10:09 am   Post subject: (No subject)

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




PostPosted: Sun Mar 14, 2004 11:02 am   Post subject: (No subject)

code:

$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)) <= time())
   {
      $hits_ip = $hit['hits_ip'];

      $sql = "DELETE FROM " . UNIQUE_HITS_TABLE . "
                   WHERE hits_ip = '$hits_ip'";
      if (!$result = $db->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...
code:

           $query = "SELECT * FROM " . $table;
           $result = mysql_query($query);
                while ($row = mysql_fetch_array($result)) {
                           $res[] = $row;
                }
          return $res;


that.. k?
Homer_simpson




PostPosted: Wed Mar 17, 2004 4:53 am   Post subject: (No subject)

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




PostPosted: Wed Mar 17, 2004 9:15 am   Post subject: (No subject)

I fixed the unique hits problem but you gave me an idea for my download module... insted of using cookies Embarassed i can do that,
thanks ;D
Homer_simpson




PostPosted: Thu Mar 18, 2004 3:14 am   Post subject: (No subject)

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




PostPosted: Thu Mar 18, 2004 8:51 am   Post subject: (No subject)

sure, why not Smile
Homer_simpson




PostPosted: Thu Mar 18, 2004 2:14 pm   Post subject: (No subject)

pm me yer msn...
btw site address is: http://evilgamerz.sytes.net
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: