Stream

« see more

New reply 17 Sep, 2025, 2:33 pm

Plugin Releases Plugin Awards

New reply 31 Jul, 2025, 12:23 am

Plugin Releases Plugin Announcement Bars

New thread 31 Jul, 2025, 12:22 am

Plugin Releases Plugin Announcement Bars

New reply 19 Jul, 2025, 2:37 pm

Plugin Releases Plugin Advanced Post Edit

New thread 19 Jul, 2025, 2:29 pm

Plugin Releases Plugin Advanced Post Edit

New reply 15 Jul, 2025, 7:50 am

Plugin Releases Paid Plugin Awards Rules

New reply 15 Jul, 2025, 2:46 am

Plugin Releases Plugin Awards

New thread 13 Jul, 2025, 12:28 am

Plugin Releases Plugin Theme File Templates

New reply 3 Jul, 2025, 3:12 am

Plugin Releases Plugin Feedback

New thread 3 Jul, 2025, 2:55 am

Plugin Releases Plugin Feedback


Files
15 Aug, 2020, 5:46 pm
Tutorial Reduce attachments.php file resources usage.
Hi.

I use the attachment system quite often as an media system in forums. Take this site for example, I mainly use the system to manage images. I think it is unnecessary to load the whole global.php to serve its purpose, thus I decided to strip it out and only load what I find to be required for the script to properly work.

You can take a look to the attached images, basically the main visual difference should be the error system, which won't render the whole page, but users will be able to use the login form regardless.

This will save you from 2 queries (error load) up to 5 queries (successful load) by file load. If you take into account the number of loaded attachments by page load as n, you will save from n*2 to n*5 queries by page load. So, for example, if you are rendering 10 attached images in a page, you could save from 20 to 50 queries per page load.

This is enough for me to apply the changes.

Open attachment.php, and find:
require_once "./global.php";

Replace with:
$working_dir = dirname(__FILE__);
if(!$working_dir)
{
	$working_dir = '.';
}

$shutdown_queries = $shutdown_functions = array();

require_once $working_dir.'/inc/init.php';

$groupscache = $cache->read('usergroups');

if(!is_array($groupscache))
{
	$cache->update_usergroups();
	$groupscache = $cache->read('usergroups');
}

$current_page = my_strtolower(basename(THIS_SCRIPT));

if(isset($mybb->input['thumbnail']))
{
	define('NO_ONLINE', 1);
}

require_once MYBB_ROOT.'inc/class_session.php';
$session = new session;
$session->init();
$mybb->session = &$session;

$mybb->user['ismoderator'] = is_moderator(0, '', $mybb->user['uid']);

$mybb->post_code = generate_post_check();

if(isset($mybb->input['language']) && $lang->language_exists($mybb->get_input('language')) && verify_post_check($mybb->get_input('my_post_key'), true))
{
	$mybb->settings['bblanguage'] = $mybb->get_input('language');
	// If user is logged in, update their language selection with the new one
	if($mybb->user['uid'])
	{
		if(isset($mybb->cookies['mybblang']))
		{
			my_unsetcookie('mybblang');
		}

		$db->update_query('users', array('language' => $db->escape_string($mybb->settings['bblanguage'])), "uid = '{$mybb->user['uid']}'");
	}
	// Guest = cookie
	else
	{
		my_setcookie('mybblang', $mybb->settings['bblanguage']);
	}
	$mybb->user['language'] = $mybb->settings['bblanguage'];
}
else if(!$mybb->user['uid'] && !empty($mybb->cookies['mybblang']) && $lang->language_exists($mybb->cookies['mybblang']))
{
	$mybb->settings['bblanguage'] = $mybb->cookies['mybblang'];
}
else if(!isset($mybb->settings['bblanguage']))
{
	$mybb->settings['bblanguage'] = 'english';
}

$lang->set_language($mybb->settings['bblanguage']);

$lang->load('global');
$lang->load('messages');

if($mybb->cookies['lockoutexpiry'] && $mybb->cookies['lockoutexpiry'] < TIME_NOW)
{
	my_unsetcookie('lockoutexpiry');
}

Update: As of 1.8.23 the login form isn't displayed within the no permission page or container.
Attached Files Thumbnail(s)

 
 
Need to find something?

Try doing a search of our community forums.