OUGC Network

Tutorial  Reduce attachments.php file resources usage.


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__);
	$working_dir = '.';

$shutdown_queries = $shutdown_functions = array();

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

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

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

$current_page = my_strtolower(basename(THIS_SCRIPT));

	define('NO_ONLINE', 1);

require_once MYBB_ROOT.'inc/class_session.php';
$session = new session;
$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

		$db->update_query('users', array('language' => $db->escape_string($mybb->settings['bblanguage'])), "uid = '{$mybb->user['uid']}'");
	// Guest = cookie
		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';



if($mybb->cookies['lockoutexpiry'] && $mybb->cookies['lockoutexpiry'] < TIME_NOW)

Attached Files


There are currently no posts to display. Be the first one to leave a reply.
Next Oldest Next Newest

Current time: 1 Dec, 2020, 2:08 pm

This forum uses Lukasz Tkacz MyBB addons.