Daily Lucky Numbers:
6
7
15
25
26
42

Visitor counter

Started by Dave, November 27, 2024, 09:52:19 AM

Previous topic - Next topic

Chen Zhen

#15
The contents are in the sp_parameters table.

I likely did something wrong with the json_encoding.
The PHP version shows the true number of visits & the plain html/javascript version does not record the proper amount because it will just be for the current user & their session.

Chen Zhen

#16
Try this PHP block:

global $user_info, $modSettings;
$actCounter = false; // boolean : true = different counter for each action, false = same counter for every action
$buttonName = !empty($actCounter) ? 'Page Views' : 'Visitors';
// allow a reset option for speicific users -> use array(0) for all admins, user id# for specific admin [ ie. array(1) ] or multiple user id#'s for specific users [ ie. array (1,3,8) ]
// requires $adminReset to be true else it will be ignored
$adminId = array(1);
$adminReset = true;
if (empty($user_info['is_guest']) && !empty($adminReset)) {
$allowReset = empty($adminId[0]) && !empty($user_info['is_admin']) ? true : (in_array($user_info['id'], $adminId) ? true : false);
}
list($title, $title2, $cursor, $ask, $ask2, $ask3, $visitCount, $data) = !empty($adminReset) && !empty($allowReset) ?
array('Click to reset the counter', 'Click to reset the current action counter', 'help', 'Reset the page visit counter?', 'Reset the page visit counter for this action?', 'Reset the page visit counter for all actions?', array(), array()) :
array('Page views', '', 'crosshair', '', '', '', array(), array());

// don't bother changing code below this comment
$allAction = !empty($_REQUEST['visitorReset']) && is_string($_REQUEST['visitorReset']) && $_REQUEST['visitorReset'] == 'all' ? true : false;
$action = !empty($_REQUEST['action']) && !empty($actCounter) && is_string($_REQUEST['action']) ? strtolower($_REQUEST['action']) . '_count' : 'visitor_count';
$reset = !empty($_REQUEST['visitorReset']) && is_string($_REQUEST['visitorReset']) && $_REQUEST['visitorReset'] == $action && !empty($allowReset) ? true : false;
$data = !empty($modSettings['website_visitor_count']) && json_validate($modSettings['website_visitor_count'])  && empty($allAction) ? json_decode($modSettings['website_visitor_count'], true, JSON_OBJECT_AS_ARRAY | JSON_NUMERIC_CHECK) : array('visitor_count' => 1);
$visitCount[$action] = empty($reset) && !empty($data) && !empty($data[$action]) ? intval($data[$action]) + 1 : 1;
$data[$action] = $visitCount[$action];
$setting = json_encode($data,  JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK);
updateSettings(array('website_visitor_count' => $setting));
echo '
<div class="visitor_data_body">
<div>' . $buttonName . '</div>
<div class="website_visit_counter"></div>
</div>
<script>
$(document).ready(function() {
$(".visitor_data_body").css({"display":"flex","justify-content":"center","align-items":"center","flex-direction":"column"});
$(".website_visit_counter").prop("title", "' . (empty($actCounter) ? $title : $title2) . '");
$(".website_visit_counter").css({
"display":"flex",
"justify-content":"center",
"align-items":"center",
"flex-direction":"column",
"background-color":"#ff4957",
"height":"30px",
"width":"50px",
"color":"white",
"border-radius":"30px",
"font-weight":"700",
"font-size":"15px",
"margin-top":"10px",
"cursor" : "' . $cursor . '"
});

});
var posthash, visitCount = Number("' . $visitCount[$action] . '");
$(".website_visit_counter").html(visitCount);' . (!empty($allowReset) ? '
$( ".website_visit_counter" ).on( "click", function() {' . (!empty($actCounter) ? '
if (confirm("' . ($ask3) . '") == true) {
posthash = location.hash.replace("#", "");
if(posthash != ""){
location.hash = "";
}
window.location.href = window.location.href.replace(/\;visitorReset=\d+/g,"").replace(/\;+$/, "").replace(/\#+$/, "") + ";visitorReset=all";
}
else ' : '') . '
if (confirm("' . (empty($actCounter) ? $ask : $ask2) . '") == true) {
posthash = location.hash.replace("#", "");
if(posthash != ""){
location.hash = "";
}
window.location.href = window.location.href.replace(/\;visitorReset=\d+/g,"").replace(/\;+$/, "").replace(/\#+$/, "") + ";visitorReset=' . $action . '";
} else {
return false;
}
});' : '') . '
</script>';

Dave

Quote from: Chen Zhen on December 05, 2024, 06:34:07 PMTry this PHP block:

Yes that works OK with PHP8.3 but nothing less, but as I said before it's your choice if you don't want it to work with less

Thanks for the new code
If you want play quizzes or games click below

Dave

Quote from: Chen Zhen on December 05, 2024, 06:14:44 PMThe contents are in the sp_parameters table.

Ah OK I didn't realise that, thanks for the info. I'll have to do some more testing now then
If you want play quizzes or games click below

Chen Zhen

Quote from: Dave on Today at 02:14:06 AMYes that works OK with PHP8.3 but nothing less, but as I said before it's your choice if you don't want it to work with less

The previous code block had something that was only compatible with PHP 7.2 or above but not the one I just posted.
It should now work for any PHP 7 or PHP 8 versions.

Chen Zhen


I apologize, you're correct!

json_validate() is only available in PHP 8.
It doesn't really need it so try this for your PHP 7 ....

global $user_info, $modSettings;
$actCounter = false; // boolean : true = different counter for each action, false = same counter for every action
$buttonName = !empty($actCounter) ? 'Page Views' : 'Visitors';
// allow a reset option for speicific users -> use array(0) for all admins, user id# for specific admin [ ie. array(1) ] or multiple user id#'s for specific users [ ie. array (1,3,8) ]
// requires $adminReset to be true else it will be ignored
$adminId = array(1);
$adminReset = true;
if (empty($user_info['is_guest']) && !empty($adminReset)) {
$allowReset = empty($adminId[0]) && !empty($user_info['is_admin']) ? true : (in_array($user_info['id'], $adminId) ? true : false);
}
list($title, $title2, $cursor, $ask, $ask2, $ask3, $visitCount, $data) = !empty($adminReset) && !empty($allowReset) ?
array('Click to reset the counter', 'Click to reset the current action counter', 'help', 'Reset the page visit counter?', 'Reset the page visit counter for this action?', 'Reset the page visit counter for all actions?', array(), array()) :
array('Page views', '', 'crosshair', '', '', '', array(), array());

// don't bother changing code below this comment
$allAction = !empty($_REQUEST['visitorReset']) && is_string($_REQUEST['visitorReset']) && $_REQUEST['visitorReset'] == 'all' ? true : false;
$action = !empty($_REQUEST['action']) && !empty($actCounter) && is_string($_REQUEST['action']) ? strtolower($_REQUEST['action']) . '_count' : 'visitor_count';
$reset = !empty($_REQUEST['visitorReset']) && is_string($_REQUEST['visitorReset']) && $_REQUEST['visitorReset'] == $action && !empty($allowReset) ? true : false;
$data = !empty($modSettings['website_visitor_count']) && empty($allAction) ? json_decode($modSettings['website_visitor_count'], true, JSON_OBJECT_AS_ARRAY | JSON_NUMERIC_CHECK) : array('visitor_count' => 1);
$visitCount[$action] = empty($reset) && !empty($data) && !empty($data[$action]) ? intval($data[$action]) + 1 : 1;
$data[$action] = $visitCount[$action];
$setting = json_encode($data,  JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK);
updateSettings(array('website_visitor_count' => $setting));
echo '
<div class="visitor_data_body">
<div>' . $buttonName . '</div>
<div class="website_visit_counter"></div>
</div>
<script>
$(document).ready(function() {
$(".visitor_data_body").css({"display":"flex","justify-content":"center","align-items":"center","flex-direction":"column"});
$(".website_visit_counter").prop("title", "' . (empty($actCounter) ? $title : $title2) . '");
$(".website_visit_counter").css({
"display":"flex",
"justify-content":"center",
"align-items":"center",
"flex-direction":"column",
"background-color":"#ff4957",
"height":"30px",
"width":"50px",
"color":"white",
"border-radius":"30px",
"font-weight":"700",
"font-size":"15px",
"margin-top":"10px",
"cursor" : "' . $cursor . '"
});

});
var posthash, visitCount = Number("' . $visitCount[$action] . '");
$(".website_visit_counter").html(visitCount);' . (!empty($allowReset) ? '
$( ".website_visit_counter" ).on( "click", function() {' . (!empty($actCounter) ? '
if (confirm("' . ($ask3) . '") == true) {
posthash = location.hash.replace("#", "");
if(posthash != ""){
location.hash = "";
}
window.location.href = window.location.href.replace(/\;visitorReset=\d+/g,"").replace(/\;+$/, "").replace(/\#+$/, "") + ";visitorReset=all";
}
else ' : '') . '
if (confirm("' . (empty($actCounter) ? $ask : $ask2) . '") == true) {
posthash = location.hash.replace("#", "");
if(posthash != ""){
location.hash = "";
}
window.location.href = window.location.href.replace(/\;visitorReset=\d+/g,"").replace(/\;+$/, "").replace(/\#+$/, "") + ";visitorReset=' . $action . '";
} else {
return false;
}
});' : '') . '
</script>';

Dave

#21
Thanks for the new code. I have issues on DJ at the moment where I tried to solve the issue by removing code and database tables previously, so I'll re-install the mods etc then try that later as I have to go out now.

It's worth noting that it didn't work on anything less than PHP8.3 not even 8.2 or 8.0 I know it's irrelevant now but if you were to do anything in the future it might have an effect you don't want.
If you want play quizzes or games click below

Dave

#22
New code working fine on DJ with PHP7.4 and on Quiz with PHP8.3 Thank you for that as I didn't know what I was going to do about checking the 2.0 themes.
If you want play quizzes or games click below