How can I get cookie start time in php? - php

My problem is cookie has been set with expiry time and in value there is no start or expire time etc but I want to get its start time mean when this cookie created?
Basically when user enters into my site and browse through some pages and reach on a specific page where cookie created, now it continuous browsing and reach on some other page where I just want to check is this cookie created within 5 hours or within 1 day etc.
I want this because user may leave site during process completion and come again after some time then I will check if user came in specific time so I want to reopen same page where he left.
Sorry if my english is not good.

There is no option available to get Cookie Creation date/time. Whenever user visits your site, You can simply add a variable + value([creation_datetime=>2016-06-10 10:10 AM]) in the cookie, and retrieve it whenever you want it.
Here is example
Example 1
Example 2

You can add current time in cookie value and check anytime when it was created (until it expires)

Related

calculate the time logged in by a user

i want to calculate the time for which the user remains logged in on my site, for example if user A logs in at 10 am and logs off at 10:15 am then store 15 minutes time and if he again logs in then also calculate that time and add to this 15 minutes and shows his total time in the system.
Quick example I can give you.
First add the required field in your database. i.e. In your users table, have a field called 'totalTime' or something.
If you're using SESSIONS, keep a value $_SESSION["start_time"] when the user logs in, and when the user logs out take the difference of the current time subtracted by the start time and add it to the totalTime field in the database for the specified user.
Any variation of that should work.
You could have a loggedIn field in your table, and a totalTime field. When the user logs in, record the time in the loggedIn field. When he logs out, get the logged in time, calculate the difference, and store it in the totalTime field, and zero the loggedIn field.
If the user logs in and the loggedIn field is not zero, the user did not log out, add the difference between the current time and the last loggedIn value can be added to the totalTime (or zero added - you would decide on a policy) and the loggedIn field updated to the new log in time etc.
If you want to collect detailed information about users, and not use simple statistics that google paralytics provide, you have a couple options, none of which are perfect solutions for anything.
Option 1:
You can simply record the time when users click on your page. The issue with this is that there is no way to see how much time a user has spent on your last page. But overall it will provide a good estimate, but will be very costly if yo have over 1000 users.
Option 2:
Record the time when user enters the website, and then have javascript ajax call that will record the time when user either logs out or closes the page. This will work if you allow users to be in a single window only.
Option 3:
Have javascript execute every minute or so and tell database that user is still logged in. Almost all databases can do over 1000 queries like that per second. If you optimize it, change database engine or do anything else you can easily get to 10k-25k queries per second.
Option 3:
If you are charging people money per minute then you can wrap the entire thing in java, or flash. however it is also not very secure.
Bottom line is: There is no perfect way to do it. You will have to hope that users have javascript enabled and track their time using background scripts, or do the whole session thing and hope that users log out. Or Track everything click by click and hope that your algorithm works well enough.
The below solution would be the most efficient one
The log table should have userid, unique_identifier, start_time, end_time
Logging in to the website will add a unique identifier for that session. It can be random number of 12 digits.
A variable $default_duration = 1440; is defined to calculate the session exipry due to inactivity. This variable should be set according to your server settings. 1140 is 1140 seconds (24 minutes), which is the default session exipry time
On user login, lets say a user loggs in by 2PM, the end time is set on login as well, which will be 2.24PM. This will solve the scenario of a user's session expiry due to inactivity.
On each page load, the end time is updated with "Current Time + $default_duration"
If a user clicks on Logout button, the endtime for that session is taken from the current time. This records the exact duration of the user.
Incase if the user logout and logins again, it would have a different "unique identifier" and the log time is updated against it.

How to disable cookie triggered popup on first page visitor lands on?

I have a cookie set with expiry of 3 mins, this will allow a user to navigate through the site for 3 mins before seeing an advert. They will see the ad if cookie is not present on next page they visit and the cookie will be reset once the ad is closed.
However a consequence of this is that as soon as the website is visited the ad will show, how can I stop the ad showing on the first page a visitor lands on?
If you only want to use cookies: You can add another cookie to the site, set it to expire after the browser is closed, and only show the ad if this cookie is set but the other is not.
Alternatively use session like methods, like storing in a session the time the user has entered the page, and calculate when to show the ad from that time. This also means you have to check for the ad showing an other way (checking the value of the session variable), not via cookies.

Session in PHP and Mysql

I want to create a page on which I can see, who is logged in on my site.
For every user after logging in I create a session, and send a query to change the row "Logged" in mysql from No to Yes.
Then, on the page I use PHP to show everyone, who has Yes set in mysql, and it works.
But, I have a problem, which occurs when someone close the browser without clicking Logout - the query doesn't execute and the Yes value in mysql stays...
What can I do to create a page like this?
Instead of having a boolean value for logged in or out, determine a reasonable amount of time for the session to expire (e.g., 1 month) and then add that amount of time to a timestamp and store that in the database as session_expires or something. Then you can check if the user's session has expired whenever you do your login checks.
Additionally, to manually expire the session (for instance if a user logs out) just set the session_expires field to a timestamp in the past.
EDIT
I didn't have access to a computer over the weekend, so I couldn't properly update my answer.
If you need to see active users, one thing I've done in the past is to use a last_seen timestamp field that updates any time the user does anything on the site. Then, on your "users logged in" page, just query for something recent like "last seen in the past 15 minutes."
You cannot do anything about that, but defining a timeout. That means your initial call after logging in that sets your customer row to "YES" should be triggered on every request (or to spare resources, every minute) to update a date column in that very customer row. If no update comes in anymore, you define that customer as "not logged in anymore" and update the column to "NO"
The fact that the user keeps on staying logged in is due to the PHP SESSION TIMEOUT which may be configured in PHP settings php.ini.
Have a look at http://php.net/manual/en/function.session-set-save-handler.php to learn how to set a session shutdown handler (that will be called once the session becomes invalid). When the shutdown handler is called, set your current session's "logged" to false in the database. Still, it may take up to 24 minutes (default value in php.ini for session timeout - session.gc_maxlifetime = 1440 minutes).
Decrease this value (session.gc_maxlifetime) to 2-3 minutes and have a ajax function on each of your pages keeping the session alive. This will definitely increase your server's load but you will definitely have a better representation of the currently active users. You can decrease the value from php.ini or by usig ini_set(‘session.gc_maxlifetime’,30); in your code.
Regards

is there a way to time a visit to a site, disregarding moving through pages?

I'm looking for a way to time a visitors stay on a website, whilst not restarting or interrupting the timer when changing pages.
Store the initial visit time in a session value. Any future visits to the site use a secondary session variable to hold the last visited time and work out the time on your site from that.
Unless you use Ajax to update the session values the user could be active on your site (on the same page) for 20minutes but it would show up as a zero length visit.
Basic example:
<?php
session_start();
if(isset($_SESSION['firstVisit']))
$_SESSION['latestVisit'] = date();
else
$_SESSION['firstVisit'] = date();
echo $_SESSION['firstVisit'] . " - " . $_SESSION['latestVisit'];
?>
You can start a session the moment the user requests the first page. With the session you can track the user from page to page. so you could calculate the total time between the first and last page requst of their visit.
You should also be able to send an Ajax request on page unload, thereby you could detect the time spent on a single page. However, if they browser in multiple windows/tabs, your reading is false, unless you are somehow able to detect page focus.
Combine the two and you should be able to get a quite complete picture.
store the data in the session, and save the data when the user performs logout
(if the user does not logout it will not be saved, but that should not be a problem)
edit: if the users does not login/logout, you have to catch when the session dies and save the data then. when evaluating the data you have to compensate for how long the session lives before it dies, e.g. 5 mins or whatever, and subtract it. will give you a ballpark figure, not exact time the user looked at the site.
You can track the time for which the user was logged in, but if you want the amount of time (a non logged in) user stays, I dont think it is possible, as there are a lot of ways to move from one page to another

Check if user is offline

I have an online game. I wish to show how many user are online. The problem is to know when a user is offline.
Is there a way to perform a check on sessions cookie to acknowledge whether the session with the broswer was closed?
I was thinking about simply set a timeout on the server which launch a script that count how many session cookie are present, but how do I check if the session cookie is about somebody who's logged and not just a visitor?
How did you handle this?
1) I don't want to rely on a script fired with the logout button, since nobody ever logout... people simply close the browser.
2) About timestamps and registering activity? Since in my game users interact with an svg (not moving through pages), they generate a huge amount of clicks. Making a query for each click for each of them refreshing a record would be very expensive.
When the user interacts with the site, set their last activity time.
If it is longer than 30 mins or so, you can assume they are offline.
You can also explicitly set someone to offline when they click logout.
However, your case is a little different. You could use a heartbeat style script.
Whilst they are on the page, use setInterval() to extend the expiry date, up to a maximum range (in case the user leaves their browser window open for hours on end).
Since your code gets executed when the page is loaded you cannot make a check if the user closed his browser or not.
So the common approach would be to use timestamps and update this stamp if the user does something on your site and if the timestamp is older than say 5 minutes you just assume he is offline

Categories