Link to home
Start Free TrialLog in
Avatar of SaLz
SaLz

asked on

I would like to see, which pages visitors went to on my website.

Hi!

I would like to see, which pages visitors went to on my website. It would somehow get the page's name and path, and add it ( if it doesn't exist yet ) to where user's IP matches the current user.

It would then display the stats for each day like so:

29.08.2005
IP: 206.27.86.101
Pages visited:
index.php,
files/test.php
----------------
IP: 210.22.35.96
Pages visited:
index.php,
files/new.php
----------------

28.08.2005
IP: 206.27.86.101
Pages: ...

And maybe show what pages are most visited ( top 5 ).

How can that be done ? code wise please.

Thank you,
Sal
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

although you could read the php logs (file) for such things, better to log each page visited to a database (mysql for example).
Then, when you need the report, just issue a query like this:
SELECT Page, count(*) from logtable group by page order by count(*) LIMIT 1,5
Avatar of SaLz
SaLz

ASKER

ok.. now how can that be done code wise ? :)
Avatar of SaLz

ASKER

btw, mysql is the DB it should go to, ya.
Avatar of SaLz

ASKER

nope, nothing there, thank you for trying to help angel,
but i would rather see how it can be done :)
Salz, want me to add this sort of thing to my other script i made up for you?
Avatar of SaLz

ASKER

heya neester!

yeah please do :)
ASKER CERTIFIED SOLUTION
Avatar of neester
neester
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SaLz

ASKER

thank you neester! I'm trying it out now. :)

I got this error,
on this line:
if (mysql_num_rows($qCheckIfExisting) > 0) {
it's Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource


by the way, i updated the code a little, instead of visitor_complete, i added a Date field and am checking if there is a record for today's date, like so:

$PAGE_URL=addslashes($_SERVER['REQUEST_URI']);
$qCheckIfExisting=mysql_query("SELECT visitor_id FROM visitors WHERE Date='".date('Y-m-d')."' AND visitor_ip='$REMOTE_ADDR' ");
if (mysql_num_rows($qCheckIfExisting) > 0) {
  // Exisitng User
  $visID=mysql_fetch_array($qCheckIfExisting);
  $visID=$visID['visitor_id'];
  mysql_query("UPDATE visitors SET visitor_last_hit=".time().", visitor_hits=visitor_hits+1 WHERE visitor_ip='$REMOTE_ADDR' AND Date='".date('Y-m-d')."'");
  mysql_query("INSERT INTO visitor_pages (visitor_id,page_url,page_time) VALUES ($visID,'$PAGE_URL',".time().")");
}else{
   // New User
  mysql_query("INSERT INTO visitors (visitor_hits,visitor_first_hit) VALUES (1,".time().")");
  $visID=mysql_insert_id();
  mysql_query("INSERT INTO visitor_pages (visitor_id,page_url,page_time) VALUES ($visID,'$PAGE_URL',".time().")");
}

can you see why it comes up with an error, maybe i messed something up ? :/
Ahh yeah, you can't just call a CELL date.
$qCheckIfExisting=mysql_query("SELECT visitor_id FROM visitors WHERE >>Date<<='".date('Y-m-d')."' AND visitor_ip='$REMOTE_ADDR' ");

See the arrows >> <<
Date is a MYSQL Function.
To fix that, simply change it to this:

$qCheckIfExisting=mysql_query("SELECT visitor_id FROM visitors WHERE visitors.Date='".date('Y-m-d')."' AND visitor_ip='$REMOTE_ADDR' ");

So it knows you mean the Field DATE.
:D
BTW you will need to make that change wherever you have Date in the Query.
:)
Avatar of SaLz

ASKER

ohh, of course! thank you hehe.

but for some reason it still gives the same error
Avatar of SaLz

ASKER

also, here's the table info, everything should be ok:

SQL query:
CREATE TABLE `visitors` (
`visitor_id` INT( 15 ) NOT NULL AUTO_INCREMENT ,
`visitor_ip` VARCHAR( 20 ) NOT NULL ,
`visitor_hits` INT( 5 ) NOT NULL ,
`visitor_first_hit` TIME NOT NULL ,
`visitor_last_hit` TIME NOT NULL ,
`Date` DATE NOT NULL ,
PRIMARY KEY ( `visitor_id` )
) TYPE = MYISAM ;

SQL query:
CREATE TABLE `visitor_pages` (
`visitor_id` INT( 15 ) NOT NULL ,
`page_url` VARCHAR( 255 ) NOT NULL ,
`page_time` TIME NOT NULL
) TYPE = MYISAM ;
Avatar of SaLz

ASKER

ah nm, was having some trouble with the database, i had to recreate the tables now, it works :)

ok, i'll try displaying the stats now. I'll let you know how it goes :)
Avatar of SaLz

ASKER

i have updated the code a little, we forgot to update ip and time.
I'll post it here a bit later, but i have to run for a while, so i'll get back to ya when i get back :)

take care till then,
Sal
Hey mate,

Good to see you got it working!

You are using MySQL Time/Date functions which I didn't originally plan on using - although its fine, it still works the same.

Cheers,
- Chris
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SaLz

ASKER

Hi Chris, hi gerodim.

ya i just added the date and ip insertion:

$PAGE_URL=addslashes($_SERVER['REQUEST_URI']);
$qCheckIfExisting=mysql_query("SELECT visitor_id FROM visitors WHERE visitors.Date='".date('Y-m-d')."' AND visitor_ip='$REMOTE_ADDR' "); echo mysql_error();
if (mysql_num_rows($qCheckIfExisting) > 0) {
  // Exisitng User
  $visID=mysql_fetch_array($qCheckIfExisting);
  $visID=$visID['visitor_id'];
  mysql_query("UPDATE visitors SET visitor_last_hit=".time().", visitor_hits=visitor_hits+1 WHERE visitor_ip='$REMOTE_ADDR' AND visitors.Date='".date('Y-m-d')."'");
  mysql_query("INSERT INTO visitor_pages (visitor_id,page_url,page_time) VALUES ($visID,'$PAGE_URL',".time().")");
}else{
   // New User
  mysql_query("INSERT INTO visitors (visitor_ip,visitor_hits,visitor_first_hit,visitors.Date) VALUES ('$REMOTE_ADDR',1,".time().",'".date('Y-m-d')."')");
  $visID=mysql_insert_id();
  mysql_query("INSERT INTO visitor_pages (visitor_id,page_url,page_time) VALUES ($visID,'$PAGE_URL',".time().")");
}

i'll mess with it more and post any followups on it :)

Thank you,
Sal