![]() |
|
embed time online? - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: embed time online? (/Thread-embed-time-online) Pages:
1
2
|
embed time online? - Inori - 04-25-2014 I was just wondering if there was any way to embed, or something equivalent to embedding, the 'time spent online' counter on a user's profile page.. any suggestions? embed time online? - Inori - 04-25-2014 I was just wondering if there was any way to embed, or something equivalent to embedding, the 'time spent online' counter on a user's profile page.. any suggestions? RE: embed time online? - levi01 - 04-25-2014 This schould work: time_online.class.php PHP Code: <?php
class time_online {
function time_online() {
$this -> userID = $_COOKIE["time_online_ID"]; // user's ID
$this -> userTT = $_COOKIE["time_online_TT"]; // user's total time on site
$this -> userST = $_COOKIE["time_online_ST"]; // time when user entered the site
$this -> userTO = $_COOKIE["time_online_TO"]; // time that the user has been online this sesion
$this -> displayID = 0; // ID used to generate diferent script in case of multiple call of display() function
if ($this -> userID != "") {
if ($this -> userST == "") {
$this -> userST = time();
setcookie("time_online_ST", $this -> userST);
}
$this -> userLPT = time() - $this -> userST - $this -> userTO;
$this -> userTT += $this -> userLPT;
setcookie("time_online_TT", $this -> userTT, time()+60*60*60*60*60);
$this -> userTO = time() - $this -> userST;
setcookie("time_online_TO", $this -> userTO);
$this -> userTO = time_online::normalizare($this -> userTO);
$this -> userTT = time_online::normalizare($this -> userTT);
}
if ($this -> userID == "") {
time_online::newID();
}
}
function newID() {
$this -> userID = md5(rand());
$this -> userST = time();
$this -> userTO = 0;
$this -> userTT = 0;
setcookie("time_online_ID", $this -> userID, time()+(60*60*24*365*10));
setcookie("time_online_ST", $this -> userST);
setcookie("time_online_TO", $this -> userTO);
setcookie("time_online_TT", $this -> userTT, time()+(60*60*24*365*10));
}
function normalizare($secunde) {
$minute = $secunde / 60;
$secunde = $secunde % 60;
$ore = $minute / 60;
$minute = $minute % 60;
$zile = $ore / 24;
$ore = $ore % 24;
return $timp = array("days" => (int)$zile, "hours" => $ore, "minutes" => $minute, "seconds" => $secunde);
}
function display_time($type){
$this -> displayID++;
if ($type == "current_page") {
$time_start_multiply = 0;
}
if ($type == "current_session") {
$time_start_multiply = $this -> userTO["days"]*24*60*60 + $this -> userTO["hours"]*60*60 + $this -> userTO["minutes"]*60 + $this -> userTO["seconds"];
}
if ($type == "total_time") {
$time_start_multiply = $this -> userTT["days"]*24*60*60 + $this -> userTT["hours"]*60*60 + $this -> userTT["minutes"]*60 + $this -> userTT["seconds"];
}
echo "
<script type=\"text/javascript\">
document.writeln(\"<span id=\\\"time_online" . $this -> displayID . "\\\"></span>\");
zi_inceput" . $this -> displayID . " = new Date();
ceas_start" . $this -> displayID . " = zi_inceput" . $this -> displayID . ".getTime();
function initStopwatch" . $this -> displayID . "() {
var timp_pe_pag" . $this -> displayID . " = new Date();
return((timp_pe_pag" . $this -> displayID . ".getTime()+(1000*$time_start_multiply) - ceas_start" . $this -> displayID . ")/1000);
}
function getSecs" . $this -> displayID . "() {
var tSecs" . $this -> displayID . " = Math.round(initStopwatch" . $this -> displayID . "());
var iSecs" . $this -> displayID . " = tSecs" . $this -> displayID . " % 60;
var iMins" . $this -> displayID . " = Math.round((tSecs" . $this -> displayID . "-30)/60);
var iHour" . $this -> displayID . " = Math.round((iMins" . $this -> displayID . "-30)/60);
var iMins" . $this -> displayID . " = iMins" . $this -> displayID . " % 60;
var iDays" . $this -> displayID . " = Math.round((iHour" . $this -> displayID . "-11)/24);
if (iDays" . $this -> displayID . " == -0) {iDays" . $this -> displayID . " *= (-1)}; // Stupid Opera :)
var iHour" . $this -> displayID . " = iHour" . $this -> displayID . " % 24;
var sSecs" . $this -> displayID . " = \"\" + ((iSecs" . $this -> displayID . " > 9) ? iSecs" . $this -> displayID . " : \"0\" + iSecs" . $this -> displayID . ");
var sMins" . $this -> displayID . " = \"\" + ((iMins" . $this -> displayID . " > 9) ? iMins" . $this -> displayID . " : \"0\" + iMins" . $this -> displayID . ");
var sHour" . $this -> displayID . " = \"\" + ((iHour" . $this -> displayID . " > 9) ? iHour" . $this -> displayID . " : \"0\" + iHour" . $this -> displayID . ");
document.getElementById('time_online" . $this -> displayID . "').innerHTML=iDays" . $this -> displayID . "+\":\"+sHour" . $this -> displayID . "+\":\"+sMins" . $this -> displayID . "+\":\"+sSecs" . $this -> displayID . ";
window.setTimeout('getSecs" . $this -> displayID . "()',1000);
}
window.setTimeout('getSecs" . $this -> displayID . "()',1000)
</script>
";
}
}
?>For ex: index.php PHP Code: <?php
include ("time_online.class.php");
$test = new time_online;
echo "<br> Curent time on page: ";
$test -> display_time("current_page"); // will display the time the user spent on the current page (dinamyc)
echo "<br> Curent time on site: ";
$test -> display_time("current_session"); // will display the time the user spent on the site on current session (dinamyc)
echo "<br> Total time on site: ";
$test -> display_time("total_time"); // will display the time the user spent on the site from all the visits (dinamyc)
echo "<br> Curent time on site: ";
print_r($test -> userTO); // display the array with the time the user spent on the site iin current session (static)
echo "<br> Total time on site: ";
print_r($test -> userTT); // display the array with the time the user spent on the site from all the visits (static)
?>RE: embed time online? - levi01 - 04-25-2014 This schould work: time_online.class.php PHP Code: <?php
class time_online {
function time_online() {
$this -> userID = $_COOKIE["time_online_ID"]; // user's ID
$this -> userTT = $_COOKIE["time_online_TT"]; // user's total time on site
$this -> userST = $_COOKIE["time_online_ST"]; // time when user entered the site
$this -> userTO = $_COOKIE["time_online_TO"]; // time that the user has been online this sesion
$this -> displayID = 0; // ID used to generate diferent script in case of multiple call of display() function
if ($this -> userID != "") {
if ($this -> userST == "") {
$this -> userST = time();
setcookie("time_online_ST", $this -> userST);
}
$this -> userLPT = time() - $this -> userST - $this -> userTO;
$this -> userTT += $this -> userLPT;
setcookie("time_online_TT", $this -> userTT, time()+60*60*60*60*60);
$this -> userTO = time() - $this -> userST;
setcookie("time_online_TO", $this -> userTO);
$this -> userTO = time_online::normalizare($this -> userTO);
$this -> userTT = time_online::normalizare($this -> userTT);
}
if ($this -> userID == "") {
time_online::newID();
}
}
function newID() {
$this -> userID = md5(rand());
$this -> userST = time();
$this -> userTO = 0;
$this -> userTT = 0;
setcookie("time_online_ID", $this -> userID, time()+(60*60*24*365*10));
setcookie("time_online_ST", $this -> userST);
setcookie("time_online_TO", $this -> userTO);
setcookie("time_online_TT", $this -> userTT, time()+(60*60*24*365*10));
}
function normalizare($secunde) {
$minute = $secunde / 60;
$secunde = $secunde % 60;
$ore = $minute / 60;
$minute = $minute % 60;
$zile = $ore / 24;
$ore = $ore % 24;
return $timp = array("days" => (int)$zile, "hours" => $ore, "minutes" => $minute, "seconds" => $secunde);
}
function display_time($type){
$this -> displayID++;
if ($type == "current_page") {
$time_start_multiply = 0;
}
if ($type == "current_session") {
$time_start_multiply = $this -> userTO["days"]*24*60*60 + $this -> userTO["hours"]*60*60 + $this -> userTO["minutes"]*60 + $this -> userTO["seconds"];
}
if ($type == "total_time") {
$time_start_multiply = $this -> userTT["days"]*24*60*60 + $this -> userTT["hours"]*60*60 + $this -> userTT["minutes"]*60 + $this -> userTT["seconds"];
}
echo "
<script type=\"text/javascript\">
document.writeln(\"<span id=\\\"time_online" . $this -> displayID . "\\\"></span>\");
zi_inceput" . $this -> displayID . " = new Date();
ceas_start" . $this -> displayID . " = zi_inceput" . $this -> displayID . ".getTime();
function initStopwatch" . $this -> displayID . "() {
var timp_pe_pag" . $this -> displayID . " = new Date();
return((timp_pe_pag" . $this -> displayID . ".getTime()+(1000*$time_start_multiply) - ceas_start" . $this -> displayID . ")/1000);
}
function getSecs" . $this -> displayID . "() {
var tSecs" . $this -> displayID . " = Math.round(initStopwatch" . $this -> displayID . "());
var iSecs" . $this -> displayID . " = tSecs" . $this -> displayID . " % 60;
var iMins" . $this -> displayID . " = Math.round((tSecs" . $this -> displayID . "-30)/60);
var iHour" . $this -> displayID . " = Math.round((iMins" . $this -> displayID . "-30)/60);
var iMins" . $this -> displayID . " = iMins" . $this -> displayID . " % 60;
var iDays" . $this -> displayID . " = Math.round((iHour" . $this -> displayID . "-11)/24);
if (iDays" . $this -> displayID . " == -0) {iDays" . $this -> displayID . " *= (-1)}; // Stupid Opera :)
var iHour" . $this -> displayID . " = iHour" . $this -> displayID . " % 24;
var sSecs" . $this -> displayID . " = \"\" + ((iSecs" . $this -> displayID . " > 9) ? iSecs" . $this -> displayID . " : \"0\" + iSecs" . $this -> displayID . ");
var sMins" . $this -> displayID . " = \"\" + ((iMins" . $this -> displayID . " > 9) ? iMins" . $this -> displayID . " : \"0\" + iMins" . $this -> displayID . ");
var sHour" . $this -> displayID . " = \"\" + ((iHour" . $this -> displayID . " > 9) ? iHour" . $this -> displayID . " : \"0\" + iHour" . $this -> displayID . ");
document.getElementById('time_online" . $this -> displayID . "').innerHTML=iDays" . $this -> displayID . "+\":\"+sHour" . $this -> displayID . "+\":\"+sMins" . $this -> displayID . "+\":\"+sSecs" . $this -> displayID . ";
window.setTimeout('getSecs" . $this -> displayID . "()',1000);
}
window.setTimeout('getSecs" . $this -> displayID . "()',1000)
</script>
";
}
}
?>For ex: index.php PHP Code: <?php
include ("time_online.class.php");
$test = new time_online;
echo "<br> Curent time on page: ";
$test -> display_time("current_page"); // will display the time the user spent on the current page (dinamyc)
echo "<br> Curent time on site: ";
$test -> display_time("current_session"); // will display the time the user spent on the site on current session (dinamyc)
echo "<br> Total time on site: ";
$test -> display_time("total_time"); // will display the time the user spent on the site from all the visits (dinamyc)
echo "<br> Curent time on site: ";
print_r($test -> userTO); // display the array with the time the user spent on the site iin current session (static)
echo "<br> Total time on site: ";
print_r($test -> userTT); // display the array with the time the user spent on the site from all the visits (static)
?>RE: embed time online? - The Real Slim Shady - 04-25-2014 Code it? The question isnt clear. This is for a website? forum? Many prebuilt software like MyBB have this already, or plugins available. If its a custom coded site than you would simply code that feature in. There isnt really any suggestions we can make without know what this is for or how it will be implemented. RE: embed time online? - The Real Slim Shady - 04-25-2014 Code it? The question isnt clear. This is for a website? forum? Many prebuilt software like MyBB have this already, or plugins available. If its a custom coded site than you would simply code that feature in. There isnt really any suggestions we can make without know what this is for or how it will be implemented. RE: embed time online? - levi01 - 04-25-2014 @Geoff I just posted an example of how you can implement this feature. And you are right there are many platforms that have this functionality by default. RE: embed time online? - levi01 - 04-25-2014 @Geoff I just posted an example of how you can implement this feature. And you are right there are many platforms that have this functionality by default. RE: embed time online? - The Real Slim Shady - 04-25-2014 (04-25-2014, 05:41 PM)levi01 Wrote: @Geoff I just posted an example of how you can implement this feature. Sure - But you're making assumptions. There might be more efficient methods of accomplishing the same thing depending on the platform he's referring to. Nor do you know his skill level, or even the language the site is coded in. Wasnt suggesting that we couldnt just throw code at him - but rather it was pointless to do so with such little information provided. hell he could even be talking about embedding the time spent online displayed on his account here, on another site - his question was that unclear. RE: embed time online? - The Real Slim Shady - 04-25-2014 (04-25-2014, 05:41 PM)levi01 Wrote: @Geoff I just posted an example of how you can implement this feature. Sure - But you're making assumptions. There might be more efficient methods of accomplishing the same thing depending on the platform he's referring to. Nor do you know his skill level, or even the language the site is coded in. Wasnt suggesting that we couldnt just throw code at him - but rather it was pointless to do so with such little information provided. hell he could even be talking about embedding the time spent online displayed on his account here, on another site - his question was that unclear. |