MyBB Community Forums

Full Version: [Tutorial] Running Tasks from your .html pages, and from non-mybb .php pages.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In this tutorial you will learn how to make all of your site's pages run your MyBB tasks, as well as how to parse .html files for PHP code. Why would you want to do either of those you ask?

Well, for the tasks, this means that they will be more likely to run on time. Also they will be run when someone visits your SITE, but without visiting your FORUM.

EDIT:
I've come across a simpler way that doesn't require the PHP parsing of your .html files. Thanks frostschultz of MyBBoard.net for pointing out that I overthought this far too much.

Instead of all of the above... just do this in your footer:
<img src="http://www.urltomyforums.com/forum/task.php" />
/EDIT

For the PHP code inside .html, it allows for a LOT more flexibility in your site's structure. Including putting login boxes and other such PHP powered constructs anywhere in your site.

This tutorial assumes several things, but attempts to explain them in as generic of terms as possible.

Assumptions:
  • You have a main site in your website's root directory.
  • Your forum is in a subfolder such as /forum or /bb. I will use /bb in my examples here.
  • You know some basic HTML so you will understand what I mean when I say things like, "Put the following in your footer".
  • You have common sense. While I realize that is an assumption that can bite me in the rear, I would hope that you do.

Step 1 - Setting up your web server to read & parse PHP code written into your html files:
In your website's root directory (usually /public_html or similar in your ftp) open your .htaccess file for editing and place the following line at the end of it (or grouped with any other AddType lines you may see)
AddType application/x-httpd-php .html

Step 2 - Placing the PHP code into the footer of html files:
This is the basic code you will be putting in your files, in fact in most cases you can use this without changing anything other than the /bb to whatever you are using for your site. Place this inside the <footer></footer> tags of your page. If you do not have them, they go after </body> and before </html>.
<!-- The following piece of code allows MyBB to run scheduled tasks. DO NOT REMOVE -->
<?php
define("IN_MYBB", "1");
require_once "./bb/global.php";
echo $task_image;
?>
<!-- End task image code -->
Notes:
Remember to change the /bb to whatever you're using. If you're using /forums change it to that. If you have the forum in your website's root, then just remove /bb completely leaving ./global.php in that line.

Now, what about files you want this in that aren't in the web root, and are in fact in their own subfolder? For example:
www.example.com/gallery/index.html
You simply need to change the require_once so that it backs up to the root level before descending. You do this by adding a ../ for every level you need to go up.

Using our example above, and saying our forum is in /bb the require_once line would look like this:
require_once "../bb/global.php";
You'll notice that when doing this the ./ before the bb changes to a ../ - when going upwards through folders we eliminate the ./ completely and replace it with however many ../ we need to reach the website's root.

Another example:
www.example.com/gallery/upload/admin/index.html

Now, here we're 3 levels deep: gallery, upload, and admin.
So our require_once line would become:
require_once "../../../bb/global.php";

Please use you common sense in adjusting this to suit each file. It doesn't take a lot of though to figure out what exactly you need for that line.

Step 3 - Adding this to PHP files:
PHP files I'm only going to cover in the most general terms. If you don't know at least some PHP I suggest you only do this in PHP files that output an html page and only then if you can find where the footer is, or belongs, in the html inside the PHP. Otherwise this is too complex to explain without explaining a lot of PHP.

In many cases you will have a mixed PHP/html file. In others you will have PHP that is using echo to send html. In either of these cases you should be able to find the footer tags, or the closing body tag. In such cases please refer to the instructions above for html footers.

If you want to add this to OTHER PHP pages that have output, but are using straight PHP...
Well, its pretty much safe to put this code anywhere other things are being echoed to the user. However, this is where common sense comes in. I wouldn't put it near anything clickable for example.
define("IN_MYBB", "1");
require_once "../bb/global.php";
echo $task_image;


Closing notes:
In many cases it is safe to include this at the tail end of your body section, just before your closing tag. So you can TRY inserting this there. I've yet to find a single case of this causing an issue, but you may. So always test it first.
You should actually use chown() to change directory and then require './global.php'. If you go require '../some/other/dir/global.php' I believe you will run into filename issues...
The easiest way is to add this little piece of code to any PHP file you want:

exec('/usr/bin/php /this/is/your/path/to/mybb/task.php');

Of course you have to adjust the path to your php binary. Works like a charm and is very easy to use. Smile And the best: Nobody perceive the task - there is no picture or something else. Everything runs in background on server.
I haven't tried, but I think if you run it that way, your browser will hang waiting for the script to finish running.
(2010-04-01, 09:35 PM)querschlaeger Wrote: [ -> ]The easiest way is to add this little piece of code to any PHP file you want:

exec('/usr/bin/php /this/is/your/path/to/mybb/task.php');

Of course you have to adjust the path to your php binary. Works like a charm and is very easy to use. Smile And the best: Nobody perceive the task - there is no picture or something else. Everything runs in background on server.

And you run ONE task. Not the whole task system. The point of this was to run the whole task SYSTEM. Just like if you view a page on MyBB itself. And a 1px by 1px transparent image isn't going to be noticed ever anyways.

(2010-04-01, 08:13 PM)April Fool Wrote: [ -> ]You should actually use chown() to change directory and then require './global.php'. If you go require '../some/other/dir/global.php' I believe you will run into filename issues...

I think you mean chdir()

I've never had filename issues. However it is recommended practice to use chdir(), I don't know why I never do. Lazy I guess. Its 2 more lines of code since you have to chdir() back as well; otherwise you can run into issues with some browsers.
These methods cause a lot of overhead and load, which is unnecessary. You do not want to load the entire MyBB subsystem and run queries to a database when all you are serving is a single, static HTML page. So what if the tasks are executed late. That's actually a good thing - no need to run maintenance tasks every half hour if there is no one to do maintenance for.

If you absolutely want to do this no matter what, the easiest way would be to simply <img src> the task.php unconditionally. It does the exact same thing as the code above - loads the MyBB system, checks if a task needs to be run, and does it if it's actually required. It's just as expensive as the other solutions but does not require PHP inside HTML support, so you should use that method for sake of simplicity.

However my recommendation still is, allow the tasks to be late. If you absolutely need something to be on time, don't use the task system at all, make a real cron job.
(2010-04-02, 12:50 AM)frostschutz Wrote: [ -> ]These methods cause a lot of overhead and load, which is unnecessary.

I made it for a client, and this is what they wanted. I suppose they don't care about load.

Quote:If you absolutely want to do this no matter what, the easiest way would be to simply <img src> the task.php unconditionally. It does the exact same thing as the code above - loads the MyBB system, checks if a task needs to be run, and does it if it's actually required. It's just as expensive as the other solutions but does not require PHP inside HTML support, so you should use that method for sake of simplicity.

The client needed the PHP inside html for other things as well. Though if I had actually put any thought into it instead of just knocking it out, you're right. Just using normal html would STILL be better way to do it. Going to have it changed now. Thanks for the suggestion. Sometimes I overthink things. Despite being a believer in K.I.S.S. I still overthink things lol.

Quote:However my recommendation still is, allow the tasks to be late. If you absolutely need something to be on time, don't use the task system at all, make a real cron job.

That was what I was going to do in the first place. But her hosting service doesn't allow cron jobs at the level she is at. Moving on. And again, thanks for the suggestion above. I love simple.
(2010-04-02, 12:08 AM)ralgith Wrote: [ -> ]
(2010-04-01, 09:35 PM)querschlaeger Wrote: [ -> ]The easiest way is to add this little piece of code to any PHP file you want:

exec('/usr/bin/php /this/is/your/path/to/mybb/task.php');

Of course you have to adjust the path to your php binary. Works like a charm and is very easy to use. Smile And the best: Nobody perceive the task - there is no picture or something else. Everything runs in background on server.

And you run ONE task. Not the whole task system. The point of this was to run the whole task SYSTEM. Just like if you view a page on MyBB itself. And a 1px by 1px transparent image isn't going to be noticed ever anyways.

No, you run the whole task system. Check out the source code of task.php - it's prepared to run on CLI.
If you want you can add a parameter to specify the task ID:
/usr/bin/php /this/is/your/path/to/mybb/task.php 2
But if you run the task.php on command prompt without a parameter it runs the task that have to be executed next.