Jump to content

Looking for the a simple solution, if possible RE: page tracking


hey_suburbia

Recommended Posts

I have a client who has a VERY basic site (12 html pages).

If a user signs up their info is put into a mySQL db and then they can sign in (using their email and password) to access 20+ html pages.

[b]My question:[/b]
My client would like to see what users went to what pages on what dates.

Right now for the login I just check to see that the email/password exist and then they get re-directed to a "logged in" page (actually just a new directory).

How can I put a date and what page next to the user who visited it.

The easiest solution would be appreciated. (They really didn't pay for this much...but that's another story)  Is this something that can be done with cookies?

Thanks!
Link to comment
Share on other sites

A cheap and simple solution would be to store that info in the MySql db.

At login store the username in the $_SESSION['username'] variable.

Create a table: counter, columns: username, pagename and date, where:
username  = user's name, can be taken from $_SESSION['username'] variable
pagename = the name of the visited page
date        = the timestamp for the visit to the page

At the beginning of each page you do some PHP/MySql with the following:
[code]INSERT INTO counter (pagename,username,date)
    VALUES ($_SERVER['SCRIPT_FILENAME'],$_SESSION['username'],time())[/code]

Ronald   8)
Link to comment
Share on other sites

yeah, that's the thing.  I brought this up to my client and they didn't really care.  They just wanted to gather some info on those looking for more information.  I told them that anybody could access this "logged in" area if they just put the correct url in.  They didn't mind and just wanted it done as easy as possible.  So, I quickly just did it the way they asked.

So, the question is..

Does this screw me when trying to track somebody who just logged in or can I just create the session variable now?
Link to comment
Share on other sites

King Arther is right. You must check if a user is logged in at every page.
And how else would your client know who visited the pages?

Login page:
session_start()
setup $_SESSION variable['username'] containing username
write visitor record to counter table

Every other page:
session_start()
check is username is set in $_SESSION['username'] variable
if not: redirect to login page
otherwise: write visitor record to counter table

That's basically it!

Ronald   8)
Link to comment
Share on other sites

Thanks, this helped out a lot.

I'm just having trouble on the other pages:

I have:
[code]<?php
mysql_query("INSERT INTO memberlist (email,pagename,date)".
      "VALUES ($_SESSION['email2'],$_SERVER['SCRIPT_FILENAME'],time()");
?>[/code]

I get an error:
[quote]Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in mysite.php on line 3[/quote]
Link to comment
Share on other sites

Are you sure the email address is in variable $_SESSION['email2']? Because that one is empty.
Your sure it is not just $_SESSION['email']?

Btw pass he variables within single quotes like

[quote]echo "INSERT INTO memberlist (email,pagename,date)".
      "VALUES ('$sess', '$scr',".time().")";[/quote]

Ronald   8)
Link to comment
Share on other sites

First off, Thanks ronverdonk for being patient with me, you're being VERY helpful!

Second, I think I'm missing some fundamentals here (please bare with me)...
 
On my login page I have:

[code]session_start();
$_SESSION ['email2'] = $email2;

//write visitor record to counter table
  $result=MYSQL_QUERY("INSERT INTO counter (email)".
      "VALUES ('$email2')"); [/code]

I checked my table and it successfully writes my email to the email field.

Now if I sign in with a different email it will write the SAME email into the counter table.  I assume this is because of the session_start(); and no logout....


That's part I.

Part II

I'm still unclear on what exactly I should have on my other pages that will record where they are.

Do I connect to the database again on each page?  IE (mysql_connect...etc.) and do I use session_start(); on these pages?

Thanks so much!
Link to comment
Share on other sites

First, I hope your code doesnot look like you quoted. The session_start should be at the very beginning.
Then you handle your login and after that you set the $SESSION variable.
You can do a mysql_pconnect (persisten connect) once or a mysql_connect at each page.

Ronald  8)
Link to comment
Share on other sites

I got it to work!

Now for some reason I'm having trouble writing it to my DB table???

The echo text came out perfect, why is this not writing to my DB:

[code]

session_start();

$sess=$_SESSION['email'] ;
$sess2=$_SESSION['visitor'] ;
$scr=$_SERVER['SCRIPT_FILENAME'];


      $result=MYSQL_QUERY( "INSERT INTO counter (email,name,pagename,date)".
      "VALUES ('$sess', '$sess2', '$scr2', ".date('l dS \of F Y h:i:s A').")");
[/code]
Link to comment
Share on other sites

I assume that your date field is defined as a CHAR/VARCHAR data type. So this value must be inserted using quotes, like this:
[code]"INSERT INTO counter (email,name,pagename,date)".
      "VALUES ('$sess', '$sess2', '$scr2', '".date('l dS \of F Y h:i:s A')."')";[/code]

'Saturday 05th of August 2006 12:11:38 AM' must be within quotes.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.