Main Menu
Daily Lucky Numbers:
16
17
24
39
41
48

Recent posts

#1
General Code/Script Inquiries / Re: Visitor counter
Last post by Dave - Today at 10:24:04 AM
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.
#2
General Code/Script Inquiries / Re: Visitor counter
Last post by Dave - Today at 04:40:36 AM
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.
#3
General Code/Script Inquiries / Re: Visitor counter
Last post by Chen Zhen - Today at 02:35:43 AM

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>';
#4
General Code/Script Inquiries / Re: Visitor counter
Last post by Chen Zhen - Today at 02:29:46 AM
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.
#5
General Code/Script Inquiries / Re: Visitor counter
Last post by Dave - Today at 02:19:15 AM
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
#6
General Code/Script Inquiries / Re: Visitor counter
Last post by Dave - Today at 02:14:06 AM
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
#7
General Code/Script Inquiries / Re: Visitor counter
Last post by Chen Zhen - December 05, 2024, 06:34:07 PM
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>';
#8
General Code/Script Inquiries / Re: Visitor counter
Last post by Chen Zhen - December 05, 2024, 06:14:44 PM
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.
#9
General Code/Script Inquiries / Re: Visitor counter
Last post by Dave - December 05, 2024, 07:46:01 AM
Last update.

I remembered I had a spare database on the Quiz host so I installed 2.1.4 on there and then installed the latest EHP and added the block and it works fine, so therefore it's a DJ thing.

I have now mirrored DJ with Quiz in respect of all the PHP settings. It was on PHP 7.4 (the reason for that is because I have an SMF2.0.19 test site set up so I can see the original themes that Skhilled, Twitch and I work on so when we're updating the new css files etc we can match as near as possible to the originals)

I've now set it to PHP8.3 and all the php options etc are now the same as Quiz.

I installed the EHPortal again and added the block code and it now works fine, so my best guess is that it was the PHP version that was at fault

I don't know if that's something you can address so that it's backwards compatible with older versions of PHP or even if you want to.

Anyway it's solved and now all I have to do is get DJ mods reinstalled and looking as it did before.

I hope this has helped.


Not sure if you've read this or not yet but here's some more info.

I have found out that for some reason when the database is saved the content of the blocks is not shown in the blocks table.
Below is the current blocks table with the welcome block having text etc added but as you can see it's not seen in the table, I wonder how it remembers it's there?

(1, 'User Info', 'sp_userInfo', 1, 1, 3, '', '', 1, 0, 0, 0, '', '', ''),
(2, 'Who&#039;s Online', 'sp_whosOnline', 1, 2, 3, '', '', 1, 0, 0, 0, '', '', ''),
(3, 'Board Stats', 'sp_boardStats', 1, 3, 3, '', '', 1, 0, 0, 0, '', '', ''),
(4, 'Theme Select', 'sp_theme_select', 1, 4, 3, '', '', 1, 0, 0, 0, '', '', ''),
(5, 'Search', 'sp_quickSearch', 1, 5, 3, '', '', 1, 0, 0, 0, '', '', ''),
(6, 'News', 'sp_news', 2, 1, 3, '', '', 1, 0, 0, 0, '', '', 'title_default_class~|title_custom_class~|title_custom_style~|body_default_class~windowbg|body_custom_class~|body_custom_style~|no_title~1|no_body~'),
(7, 'Welcome', 'sp_html', 2, 2, 3, '', '', 1, 0, 0, 0, 'sportal', '', 'title_default_class~|title_custom_class~|title_custom_style~|body_default_class~windowbg|body_custom_class~|body_custom_style~|no_title~1|no_body~'),
(8, 'Board News', 'sp_boardNews', 2, 3, 3, '', '', 1, 0, 0, 0, '', '', ''),
(9, 'Recent Topics', 'sp_recent', 3, 1, 3, '', '', 1, 0, 0, 0, '', '', ''),
(10, 'Top Poster', 'sp_topPoster', 4, 1, 3, '', '', 1, 0, 0, 0, '', '', ''),
(11, 'Recent Posts', 'sp_recent', 4, 2, 3, '', '', 1, 0, 0, 0, '', '', ''),
(12, 'Forum Staff', 'sp_staff', 4, 3, 3, '', '', 1, 0, 0, 0, '', '', ''),
(13, 'Calendar', 'sp_calendar', 4, 4, 3, '', '', 1, 0, 0, 0, '', '', ''),
(14, 'Top Boards', 'sp_topBoards', 4, 5, 3, '', '', 1, 0, 0, 0, '', '', ''),
(15, 'Visitors', 'sp_php', 6, 1, 3, '', '', 1, 0, 0, 0, 'sportal', '', 'title_default_class~catbg|title_custom_class~|title_custom_style~|body_default_class~|body_custom_class~|body_custom_style~|no_title~|no_body~1');

#10
General Code/Script Inquiries / Re: Visitor counter
Last post by Dave - December 05, 2024, 05:47:29 AM
UPDATE3:

I start from a fresh 2.1.4 install with the test site.

I then installed the latest version of the portal. I then added the latest code for the Visitors that you posted, and when I clicked on 'Preview' it worked, but as soon as I clicked on 'Add Block' it gave me the database error as previously mentioned.

I'm now going to uninstall the portal again and remove all tables (when uninstalling completely by selection the delete all option it leaves 2 tables behind see attached) and see what happens then.
I'm going to export the database now and then compare it to a copy from after the portal is removed  and see what's changed.

I might then reinstall 2.1.4 again and compare that way too.

UPDATE4:

I uninstalled the portal and saved the database, then I started again with 2.1.4. upon comparing the tables there are still remnants of the portal in the settings table and even though the visitors counter wasn't installed there is code in there for that at line 2058 in the attached 'u286627211_SMF2anoportal' I have also attached a copy of a fresh database from the new install so you can compare as I did. I use Notepad++.

I have just checked the DJ database settings table and removed the
website_visitor_count {"visitor_count_":1}variable for it. Then added the code to a new block again and did a preview and the counter shows, see attached image, but as soon as you click on 'Add Block' the error
Database error in block code. Please check the code. comes back .

I'm not sure if that helps you or not. I will check Quizland database and see if there's a similar variable in there.

The same variable is there on Quizland website_visitor_count {"visitor_count_":994} it's the same except for the numbers