MyBB Community Forums

Full Version: Run a PHP script on each page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Well, this might make plugin developers faint, but here's what I'm doing:

I'm an absolute beginner in PHP and today I've written (highly based on tutorials) a login script. It allows me to add a little PHP script at the top of my pages to check whether the user is logged in or not. If he's not, it will redirect him to the login page. I need this script to run on a MyBB forum I have made up for the website of my charity association. I don't want to use any plugin as I want this very special script to run. I won't bother you with details.

So basically I have added the checking script in head_include. No errors are showing up (this is good at least) but even if I'm not loged in, it doesn't prompt the login page. The code is perfectly working, I've wrote in on some test html/php pages before trying to add into MyBB.

So, I guess header_include isn't the good place to add this script. Or MyBB is blocking the execution of this script this it's not an installed plugin (I don't know how to make it into a plugin, so as I've already done enough PHP for a first day, I'd like to avoid turning it into a plugin).

Maybe I'm all wrong, and.. I don't know. I know my script it working, and if it could run at the beginning of each page of my forum, it would be perfect. Here's the script I'd like to run:

<?php
session_start();  //It starts the session.

require_once("includes/connection.php");
include_once("includes/constants.php");
require_once("includes/functions.php");

is_logged_in();
?>

Thanks guys, and sorry if I'm not clear.

EDIT: Sorry, added the wrong PHP script. Also, another thing I'm not sure about, is whether I need to add the "<?php" "?>" bit, or is it not necessary?
So you want to add this for guests ?

You may use this: http://community.mybb.com/thread-6190.html as a base for that script.
If you mean you're putting this in a template, it's not going to work, you can't put this in templates. The plugin you'd need would be simple:

<?php
$plugins->add_hook("global_start", "logincheck");

function logincheck_info()
{
	return array(
		'name' => 'Login Check',
		'description' => 'Check if a user is logged in to the site.',
		'website' => '',
		'author' => 'MattRogowski',
		'authorsite' => '',
		'version' => '1.0',
		'compatibility' => '16*'
	);
}

function logincheck_activate()
{

}

function logincheck_deactivate()
{

}

function logincheck()
{
	session_start();  //It starts the session.

	require_once("includes/connection.php");
	include_once("includes/constants.php");
	require_once("includes/functions.php");

	is_logged_in();
}
?>

Call this logincheck.php, put it in ./inc/plugins/, activate it.

I assume this is to check that they're logged in to your site, and not if they're logged in to the forum, right...??
(2011-01-13, 11:12 AM)MattRogowski Wrote: [ -> ]I assume this is to check that they're logged in to your site, and not if they're logged in to the forum, right...??

The forum will be for members of the association only. They'll need to be logged in as members to access it. I'll try to do this plugin thing. Will let you know. Thanks.

EDIT: Works like a bleeding charm! I know you'll laugh at me, but this took me like 3 good hours.. But I'm happy that it works great and also I feel like I have developed a plugin Smile
I didn't know where the code needed to be injected, so it was "global_start".

Also, the plugin shows with a black name in ACP, no problems?
(2011-01-13, 11:19 AM)Bencori Wrote: [ -> ]Also, the plugin shows with a black name in ACP, no problems?

You need to add the info() function. Look at hello_info() in the hello.php that comes with MyBB in the /inc/plugins folder.
If you look at the code, it has an info function. If it didn't it wouldn't even show up in the ACP. It also shows fine when I put it on my forum, it has the name in the info array...
(2011-01-13, 01:09 PM)MattRogowski Wrote: [ -> ]If you look at the code, it has an info function. If it didn't it wouldn't even show up in the ACP. It also shows fine when I put it on my forum, it has the name in the info array...

Oops, sorry. Wasn't paying attention.