News:

SMF 2.1.6 has been released! Take it for a spin! Read more.

Main Menu

Ocasional Access Denied error during php redirects to topics

Started by Melonking, February 02, 2024, 08:34:20 PM

Previous topic - Next topic

Melonking

Aside: Just a heads up to the mods; this forum loses your post if you hit back by mistake on Firefox, there are ways to fix this built into the SCEditor code, I did it on my forum so I know it works!

I just finished creating an SSI-powered page that redirects a visitor to a random topic when they visit the page. It works well, however occasionally it will flash a blank page titled "Access Denied" before completing the redirect - I'm unsure why this is happening because the redirect always works. You can observe it yourself here: https://forum.melonland.net/random.php

Even if the redirect is done via JavaScript instead of PHP, it still happens after the JavaScript page loads and attempts its redirect, so I can only conclude that the Access Denied page is displaying after/during the redirect process and therefore could be within SMF itself. There are no errors in the log.

If you go to the random page again rapidly after visiting it, the Access Denied message is much less common; it seems to happen after a delay; perhaps the database goes to sleep?

Does anyone have any idea what could be causing this?

SSI code:
function ssi_randomTopicIds($num_results = 1, $exclude_boards = '') {

global $smcFunc, $context, $modSettings, $scripturl, $txt, $settings;

$request = $smcFunc['db_query']('', '
SELECT id_topic
FROM {db_prefix}topics
WHERE id_board NOT IN ({array_int:exclude_boards})
ORDER BY RAND()
LIMIT {int:num_results}',
array(
'num_results' => $num_results,
'exclude_boards' => $exclude_boards
));

$randomTopics = array();

while ($row = $smcFunc['db_fetch_assoc']($request))
$randomTopics[] = $row['id_topic'];
$smcFunc['db_free_result']($request);

return $randomTopics;
}

random.php:
<?php
ob_start
();
require(
dirname(__FILE__) . '/SSI.php');
$randomTopicId = ssi_randomTopicIds(1, [25,28,16])[0];
header("Location: /index.php?topic=" . $randomTopicId, true, 302);
ob_end_flush();

vbgamer45

I am not seeing that did about 20 requests

HTTP/2 302
server: nginx/1.24.0
date: Sat, 03 Feb 2024 05:01:19 GMT
content-type: text/html; charset=UTF-8
expires: Thu, 19 Nov 1981 08:52:00 GMT
pragma: no-cache
cache-control: private
location: /index.php?topic=1220
x-frame-options: ALLOW
X-Firefox-Spdy: h2
Community Suite for SMF - Grow your forum with SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com - Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Melonking

Quote from: vbgamer45 on February 03, 2024, 12:04:23 AMam not seeing that did about 20 requests
Aha thank you!

I assumed this was serverside because it was affecting all browsers, but I have a system-wide ad blocker (Ad Guard) and switching it off solved the issue  :-X

I suppose this is closed - but anyone who wants to use the random thread code posted above is free to do so ^^

Advertisement: