[PHP - TUT] Site Online/Offline Status

Synkc

Active Member
Messages
1,765
Reaction score
0
Points
36
Intro:
This is a simple script I wrote a while ago; it allows you to simple turn your entire site offline, or just specific pages. It also permits you to have access, while displaying a "Site is currently offline" message at the top of each page.

Note:
This code requires that you use a "config.php" file to store all your site's varibles, and a "global.php" file, which is linked to both your config file and site pages, for executing the php that appears globally on your site.

For example, your site's root directory may contain:

- index.php
- config.php
- global.php

or

- index.php
- inc/config.php
- inc/global.php

etc.


Procedure:

Ok, first, copy the following code into your config.php file:

PHP:
// Site Online status (false=Online, true=Offline)
$offline = false;

$ip = $_SERVER['REMOTE_ADDR'];

// The IP address of the administrator (Change to your ip if site is not locally hosted)
$admin_ip = '127.0.0.1';

// Site Messages
$message_offline = "The $site_name website is currently offline due to maintenance. Please check back soon!";
$admin_message_offline = "Notice: The site is currently set to \"offline\".";

Remember to place it within the <? ?> tags.

Now place this into your global.php file:

PHP:
// Links to the config.php file (change the path to suit)
require_once $_SERVER['DOCUMENT_ROOT'] ."/inc/config.php";

if($page_name == "") {
    $page_title = "";
}
else {
    $page_title = " - $page_name";
}

// Checks to see if the website is set to "offline" through the "config.php" file, and if it is it displays a message. Also changes the page title to show "Offline".
if ($offline == true) {
    if($page_name == "") {
        $page_title = " :: Offline";
    }
    else {
        $page_title = " - $page_name :: Offline";
    }
    if($ip == $admin_ip){
        $notice = "
            <center>
                <table class='offline'>
                    <tr>
                        <td align='center'>
                            <strong>
                                $admin_message_offline
                            </strong>                    
                        </td>
                    </tr>
                </table>
            </center>";
    }
    else {    
        echo "$message_offline"; 
        exit;

    }
}

Now, place the following code at the top of every page that you want to be able to turn offline:

PHP:
// Links to the global.php file (change the path to suit)
require_once $_SERVER['DOCUMENT_ROOT'] .'/inc/global.php';
$page_name = "YOUR PAGE'S NAME";

Also, while placing this code, put a "$notice" after the opening <body> tag on each page. This will determine where on the page the offline message will appear for the admin.

To change the title of your pages from whatever to "SITENAME - PAGENAME :: Offline", you need to change the html title tag for each page into "<title>$title$page_title</title>" (without quotation marks).

Now finally, if you have a style sheet, open it and place at the bottom (Change this to suit your site's style):

HTML:
.offline {
    margin: 0px;
    padding: 0px;
    background-color:#FFFFFF;
}

If all done correctly, you can now turn off your site via your config.php file; just change the varible "$offline" to true.

If you have any questions, feel free to ask.

100% written by me, Synkc.
 

Despistado

New Member
Messages
96
Reaction score
0
Points
0
wow you have a great knowledge with php, thats fantastic, this is very helpful thank you so much

best regards
 

Synkc

Active Member
Messages
1,765
Reaction score
0
Points
36
Thanks for the feedback guys, hope you enjoy the script :)
 

Icecoldcoke

New Member
Messages
60
Reaction score
0
Points
0
I get this
"Parse error: syntax error, unexpected '&' in /index.php on line 7"

It could be something with it making 5,000 "&nbsp" whenever I save them..

EDIT: NM..

But Im blocked from my website when it's down.. lol
 
Last edited:

Synkc

Active Member
Messages
1,765
Reaction score
0
Points
36
Did you change the "$admin_ip" variable in config.php to your IP address?
 

Synkc

Active Member
Messages
1,765
Reaction score
0
Points
36
Hmm, well I've looked over the code and it seems to be fine. Do you mind posting/pming me the content of you index.php file, so I can try and work out whats wrong?
 

Xemnas

New Member
Messages
812
Reaction score
0
Points
0
Nice tutorial Synkc, thanks! I have a question though: at the moment I have a GUI (I don't call it an Admin CP because it only has system online/offline functions right now) which I use to turn systems (e.g. login portal, new user registrations) online and offline. The status of each system is stored in a database. Is this preferable to storing the data in a config.php file? Should I use config.php instead?
 

Jesse

Active Member
Messages
1,360
Reaction score
0
Points
36
Can i put my banner when my site is offline, how do i do it?
 

Synkc

Active Member
Messages
1,765
Reaction score
0
Points
36
Nice tutorial Synkc, thanks! I have a question though: at the moment I have a GUI (I don't call it an Admin CP because it only has system online/offline functions right now) which I use to turn systems (e.g. login portal, new user registrations) online and offline. The status of each system is stored in a database. Is this preferable to storing the data in a config.php file? Should I use config.php instead?

I prefer to store important information, which is vital to the operation of the site, in files, simply because if the server, your database is hosted on, ever goes offline, your site will still be able to preform some basic operations.

Can i put my banner when my site is offline, how do i do it?

Just add your banner to the "$message_offline" varible, defined in config.php.
 

Aurayu

New Member
Messages
6
Reaction score
0
Points
0
Shame that the code uses "admin_ip"

Wouldn't it be better to show a link to an admin control panel where people could turn on and off the offline messageis they have sufficient rights?
 

mattura

Member
Messages
570
Reaction score
2
Points
18
Looks good to me.

I wrote a similar script for one of my sites which does the same, but dynamically writes a file like your 'config' when I log in and press a big red button! My case is a bit more complicated as I still allow administrators to log in, but everyone else gets a nice 'bugger off' page ;)

I'd certainly agree not to have this functionality in a database as the database could go down and you would be stuffed.
 
Top