hey_suburbia Posted August 4, 2006 Share Posted August 4, 2006 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! Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 4, 2006 Share Posted August 4, 2006 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'] variablepagename = the name of the visited pagedate = the timestamp for the visit to the pageAt 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) Quote Link to comment Share on other sites More sharing options...
hey_suburbia Posted August 4, 2006 Author Share Posted August 4, 2006 Thanks a lot.One more question:I don't have a $_SESSION variable.The form I have just checks to see that their email is in the DB and then redirects them to a new directory.Is this method going to work? Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 4, 2006 Share Posted August 4, 2006 If you're not using sessions how do you know users haven't just gone straight to the new directory without logging in first? I.e. how are you authorizing access to these new pages? Quote Link to comment Share on other sites More sharing options...
hey_suburbia Posted August 4, 2006 Author Share Posted August 4, 2006 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? Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 4, 2006 Share Posted August 4, 2006 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 usernamewrite visitor record to counter tableEvery other page: session_start() check is username is set in $_SESSION['username'] variableif not: redirect to login pageotherwise: write visitor record to counter tableThat's basically it!Ronald 8) Quote Link to comment Share on other sites More sharing options...
hey_suburbia Posted August 4, 2006 Author Share Posted August 4, 2006 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] Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 4, 2006 Share Posted August 4, 2006 Try:[code]$sess=$_SESSION['email2'] ;$scr=$_SERVER['SCRIPT_FILENAME'];echo "INSERT INTO memberlist (email,pagename,date) ". "VALUES ($sess, $scr, time()";[/code]Ronald 8) Quote Link to comment Share on other sites More sharing options...
hey_suburbia Posted August 4, 2006 Author Share Posted August 4, 2006 The page renders, but on top I get:[quote]INSERT INTO counter (email,pagename,date) VALUES (, /com/mysite/public_html/page1.php, time() [/quote] Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 4, 2006 Share Posted August 4, 2006 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) Quote Link to comment Share on other sites More sharing options...
hey_suburbia Posted August 4, 2006 Author Share Posted August 4, 2006 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 III'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! Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 4, 2006 Share Posted August 4, 2006 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) Quote Link to comment Share on other sites More sharing options...
hey_suburbia Posted August 4, 2006 Author Share Posted August 4, 2006 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] Quote Link to comment Share on other sites More sharing options...
nethnet Posted August 4, 2006 Share Posted August 4, 2006 Ignore this post. I was stupid. Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 4, 2006 Share Posted August 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.