Tracking internal link php - php

I want to set up a few internal statistics for one of my dynamic sites. The idea is to make available to each member of the site:
a) How many times the profile has been seen in the day (1 click = 1 ip = 1 view)
b) How many times the profile has been seen in the month (1 click = 1 ip = 1 view)
c) How many have left since the mail button "contact".
Before developing this in php, I wanted to know if you would not have a resource that these actions. It would save me some time.
Sincerely,

Well, you would just simply need to have a DB where you could save those statistics. Then, you would create a class with a few functions that save statistics to this DB. E.g.
function addPageview($pageIdentifier, $loggedInUser) {
// code to save to DB
}
Then, when a page is viewed (e.g. the profile page of someone), you do a call to this addPageview() with the correct page identifier (e.g. the URL) and the logged in User so you know who has viewed the page. You leave $user empty if there is no logged in user.
Good luck!

So if you want to increase your profile-views counter by 1, you can restrict this to do so every 24 hours by setting a cookie on the visitors computer with that specific users ID. The user can clear their cookies and visit the profile again, but "commoners" dont know about this technique.
In your code for viewing the profile, you use the following pseudocode:
if user has no cookie
bump views up by 1

So I create my own internal link tracker for ZF.
I don't use cookie.
I check if an ip is already back on the site. If so, I change the date of last visit, otherwise I created. Then, I check if the called page has already been visited. If so, I change, otherwise I insert. Then, I check if the association ip / page exists: if so, I change, otherwise I insert.
In the end, I can have a system of click per day, month, year, and for su ...
I wrote a tutorial on the occasion on my blog, because now it is only really suited to the current project.
Thank you for your support.

Related

Create a page that shows a user which pages they have been to using PHP

I was wondering if there was a way to create a page on my website that would allow for a user to view the pages in the website that they have been to. I have searched around to see if I could find a hint to where I could start from, but I came up empty. I have already coded a system where a user can sign up and log in, I just need a way so that they can track where they have been. Thanks
I won't go into full detail, as I cannot comment to ask how you would prefer, but an example using sessions would be such;
At the start of each page, you could do something as follows;
session_start();
array_push($_SESSION['pages'], "`You would put a user-friendly page name here`");
Or alternatively;
session_start();
array_push($_SESSION['pages'], __FILE__);
The above would store each page the user visits in a session named pages. If you wanted to, for say, receive the last five visited pages, you could then do something as such;
array_slice($_SESSION['pages'], -5);
Although this wouldn't be the most efficient and/or is just basic, it is the bedrock in which you could expand upon.
Another idea would be to log the page visits to a database. You could have a table names page_views or similar with id, user identifier and page as the columns, then following the above example to 'log' the page views to the database. You could then select from the database and limit to the last 5 records matching the user identifier, therefor receiving the five latest logged pages.

The most efficient way to handle visitor/user statistics that I want to catch and display?

I am building a link directory style web application. For simplicity all of the following are examples. On my website I have 10 categories. Each category has it's own page and each page has 100 links in a table format. Each link has many columns like name, id, url, etc but the focus of this question deals with the "time last viewed" column. It will display a default text if the user/visitor has never clicked the link however if the link has been clicked by the user prior to the visit it will display the time/date the user last visited that link.
The way I have it set up is when the user clicks the link they are sent to another page/script (using GET method. link 1 is appended with ?rid=1) I use a switch contruct. (Case value is 1 from $_GET execute code block) this code block is where i need the user statistics caputuring to happen. Once the function runs and both captures and stores the visit statistics info the user is sent to the requested resource via header location. So the next time to user sees the list of links on the category page the link they visited will now display the time they visited it.
On my production site i have up to 1000 links. If they clicked each link it would say next to each link the last time they clicked it. Important to include users will be logged in when clicking each link.
How would you go about doing this? Store the info in a cookie or in the database? As there are 1000 links there could be 1000 different values. Thanks in advance.
It isn't a lot of data so you can do both, store in the database as well as store in a cookie. Ideally for performance, you should retrieve from the cookie first and then retrieve from the database if the cookie doesn't contain any user information pertaining to that link. Depending on your performance requirements and the amount of traffic you anticipate, you can use database storage, in-memory storage and asynchronous updates.
database updates are instant but can impact overall performance and page load times
in-memory caching such as apc gives best performance but data needs to be synchronised to the database
asynchronous updates are great for balancing out performance hits because you can register a view from the client side using JavaScript after the page has loaded, rather than during php execution on server side.
Personally I would use all 3 if possible because it gives a good platform for future development.

Checking user visits | PHP

Ok so I store the User ID and a PHP Timestamp on a db. What I want to do is check whether the user has visited on a daily basis. Something very similar to SO's visited system. I know that I should have the user's visit record.
But what I can't imagine right now is how do I determine the days that the user has visited and not visited. Any ideas? I really can't piece it together some help would be appreciated. A simple point to the direction if its ok. thanks
Edit:
I want to know the days the user has visited. Literally if the user visited on Nov. 1, 2013. I want to identify that but it wouldn't be just one day it would be each day since the user registered.
Append your user table with the following columns:
visittime
visitcount
consecutive (boolean)
When a user visits, check if the visittime was more than a day ago, and update consecutive accordingly. Als increment visitcount if visited consecutively.
There are a couple of approaches to solve this problem.
The most common way is to create a new table in your database.
This table should contain an "access log" of yout users (on entry per user per day). This way you can check if the user has accessed the site or not on a particular day. With this information you can calculate (almost) any information regarding to access stuff.
The fields coud be:
user_id
visit_time
visit_count
consecutive (tiny int)
You could add those fields to your existing table as well, but I think an additional table is cleaner.
This should get you up and running I guess.
So I suppose your main problem is how to determine if visitor is new or not.
You can use a $_COOKIE for that (as google analytic do), incrementing it for each visitor (this will be your visitor-unique-id)
Then if the cookie and you is not present, this is a new visitor.
Then store every page loading you want in a db

how to handle online status. guest, users etc

Whats the best way to keep track of how many users and guests are online? Im making a forum for fun and learning
Right Now I have a 2 fields in the users table called is_online and last_access_time.
If current time is 5 minutes or more than last_access_time i set is_online it to zero. And if the signed in user refreshes browser i set it to 1.
But what about guests? I wanna keep track on how many guests are on also
Another thing that would be really cool is to show what page the user is viewing. and on the page, forum thread for example, 5 guests, Homer and Shomer are viewing this page. But how should i structure this? Hmm maybe i should make another question for that.
i dont know what i should do
What do you suggest?
I'd use cookies for this. Set a cookie when the user enters (checking first to make sure one doesnt exist). Easy way to generate a unique id for that user is to hash their IP plus the current time.
$id = md5($_SERVER['REMOTE_ADDR'] . time());
Store that id in your database and use that to reference
You can check what page they are viewing by grabbing either $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] near the top of your php source. Store that in the table. I'd take a look at php.net's explanation of whats stored in the _SERVER global, as it should help out quite a bit if you find that you need more then just the document they are on (ex index.php). Found here.
You may need to pull apart of the query string that was used to access that page, parse out the variables to determine the page they are requesting. Either way, this could all be done through cookies, or just use a single cookie to store the unique id and use your table for storing everything else.
You cannot know for certain which page a user is viewing, but you can keep track of which page they last viewed. Every time you deliver a page to a user, record that page's path in a database row associated with them. Voila.
To keep the number of guests, I suggest tracking the number of distinct unauthenticated IP/HTTP-User-Agent combinations seen on a certain page in the last X minutes.
I found this article on Web Monkey that might help you.
http://www.webmonkey.com/2010/02/how_many_users_are_on_your_site_right_now/

Marking latest news as ... "new"

On some websites or forums I usually go to, entries that I havn't read yet are marked as "new". I would like to implement this feature on a website I have developed (with news being posted from time to time) -- php/mySQL. How is this usually done ? Does it require using a cookie remembering the last visit date/time ? Or the last viewed posts/urls ? How can I identify the users ?
Cookies are just one possible way of identifying the user for the session, or between visits for those without authentication. Though a very common and useful way. (PHP can also use the sid or another parameter, though its not common anymore.)
You need to store either which threads/posts the user have read, or which he/she has not. You can sum things up with read everything before 'date' or postId for certain subforums.
This all depends on the lay out of your forums, posts and news, and how dynamic they are. You might also only want to show new posts since last visit, show new posts while the user is currently at your site, then use the new posts since last visit if the user is away for more then a predefined (x hours)/calculated (y hours if weekend, z hours if admin) time.
Edit: CSS for visited links will not help you with new comments for news, new posts in a thread, go directly to the first unread post or accessing the site at work/school and home.
A side note: doing so would actually be duplicating browser functionality: (as long as you use fixed urls) the browser will give links a different style on visited links. Of course this will reset if you clear history, but then again a cookie only based solution will allso clear if the cookie is deleted (with many browsers having a "delete private data" function wich deletes both by default, it would usually be reset at the same time)...
This site have fixed url's (for questions) and doen't set the visited color the same as normal link color, so you can see the questions you've visited by the link color.
EDIT: You can also use CSS to add an icon to visited links.
Cookie is about the only reliable way to do this type of thing.
I'd use a cookie to store when a user last visited, but also have a reasonable default of say 1 week if the cookie doesn't exist.
Then show new against things newer than that date.
You can either store the actual last visited date in a cookie, or you can just store a unique id for that person in a cookie and track what they last read in the database. If you do the latter, you can allow them to log in with the same id on different browsers and still get an accurate count.
I do it this way:
If the user doesn't have a username and isn't logged in, then I don't show new items.
If the user is logged in, and they have been shown all items then I store the current date/time on the user file, and use this value to work out what items are new.
If the user date/time is within the last ten minutes then I don't update the user date/time.
Here's my code...
function drawPage
if (isLoggedIn)
get dbUser from database
lastUserDateTime = dbUser.LastCommentTime
else
lastUserDateTime = yesterdaye
end if
for each post
get date of post
if post->date < lastUserDateTime mark it as new
draw the post
loop
if (isLoggedIn)
if (lastUserDateTime + 10 mins) < now
dbUser.LastCommentTime = now
update dbUser in database
end if
end if
end function

Categories