View Single Post
Old 06-25-2019, 12:41 PM  
zerovic
Confirmed User
 
zerovic's Avatar
 
Industry Role:
Join Date: Apr 2010
Posts: 1,102
Quote:
Originally Posted by 2MuchMark View Post
Can you tell me more about this? Do you mean having a script in Ajax that pings the server to tell it its connected, then write it to a DB?
Here's something I just wrote real fast... it's not the best solution, however, it'll do the job and you'll get the idea ;)

Code:
<?php
//make sure you are connected to your database!
if(isset($_GET['username'])) {
	$username = $_GET['username'];
	$current_date = date('Y-m-d H:i:s');
	//update the database with the current date/time
	//for example
	//mysql_query("update `users` set `last_seen` = '$current_date' where `username` = '$username'");
}
?>
<head>
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
	<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	<script>
	$(document).ready(function() {	
		var username = 'username';
		UpdateUserTime = setInterval(function() {
			$.ajax({
				url: "http://localhost/2MuchMark.php?username=" + username,
				cache: false,
				success: function (html) {
					$('#foo').append('User time/date updated.<br />'); // you can leave this empty, it's just to see if the request was successful.
				}
			});
		}, 5000); // the interval you want to update the current time / date.
	});
</script>
</head>
<body>
<div id="foo"></div>
</body>
Basically, you have the current username stored as a jquery variable which is sent to the php script every 5 seconds which updates the users last seen time / date.

Let me know if it works for you, or you have any questions.

ps. you are not limited to only date/time. you can store anything such as the current page if you want. for this I'd suggest using POST instead of GET, but everything is the same.

Simply change the type of the request to POST and define the data you are sending with.
Code:
UpdateUserTime = setInterval(function() {
			$.ajax({
				type: "POST",
				data: 'username=' + user + '&current_page=' + current_page,
				url: "http://localhost/2MuchMark.php",
				cache: false,
				success: function (html) {
					$('#foo').append('User time/date updated.<br />'); // you can leave this empty, it's just to see if the request was successful.
				}
			});
		}, 5000);
Cheers,
z
zerovic is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote