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
Related
I am writing a hotel booking software using PHP and MySQL. I am pretty much done with it but I ran into a race condition problem. For example there is only one standard room left and when 2 guests both select it it shows available to both of them. I tried fixing it by checking the room again when the guest clicks confirm before payment but that still has issues. I also tried making the room status to pending when whoever clicks the confirm first but I can't figure out how to change it back to available if the guest decides not to pay or just closes the browser. I searched SO for answers but I didn't really find a definitive answer. Thanks in advance
One solution is to add two columns to a table in the database. One column is the session ID or user ID or whatever of the user that is being offered the room. The second column is a timestamp indicating when that offer will expire.
Then, in your app, only show rooms that have an expired timestamp in the hold column. (Set the initial timestamp to 0 so that it starts out expired.) When a room is selected, check the column again. If there's an unexpired timestamp there, the user gets a "sorry, you were too slow" message. Otherwise, put a timestamp there for 15 minutes into the future or whatever, and proceed.
You see this on travel sites and ticket-purchasing sites a lot where it says something like "We're holding these seats for you for another 14 minutes. Please complete the transaction by then or it will be released blah blah blah."
I would also go with the pending state. You could save the state together with the session id of this user and have a cronjob that deletes all pending states that have expired session ids associated to them.
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.
I wonder if this is a good solution for a medium size social site. Its about managing online/offline indications of users. My solution is to include a script on each page that first of all updates the "last activity" field of the current user with a timestamp, then i select all users that havent been active within the session expire time(but is still online accordingly to the table) and sets the online flag(in db) to false. Is this a solution that is commonly used or will the sever load be to much when a bigger amount of users are navigating throug the site? Is there any better solution? Be aware that im not only intrested in the amount of online users but also names, therefore a session based script seems to complicated and inaccurate.
Thanks in advance!
It is good enough but better you could store this information in activity table: for any active user insert/update a row with his user_id and for inactive for more than N minutes one - remove rows.
A table that stored sessions should contain a field called "LAST_ACTIVITY_DATE", TIMESTAMP. So whenever they load a new page or you update something with AJAX, just update LAST_ACTIVITY_DATE with current_timestamp.
Then when you need to get online users, get those who have last_activity_date within 5 minutes from Current_timestamp.
Easy as!
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/
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