[PHP] Maintenance Panel Tutorial
If you need to turn your site on and off quickly, then I think I might've helped you out.
This tutorial will teach you how to make a PHP/MySQL site status editor with a login page.
Got moved into Tutorials section after being out for 2 minutes!
Requirements:
- A PHP Server
- A MySQL Database
- A Notepad or FTP Client
- A little HTML knowledge for styling. (Optional)
Note: If you don't understand the code, then read the comments in it, they're there for a reason, to help you!
Step 1:
The Database
Create a new MySQL database, call it whatever you wish but something appropriate like "status" etc would be best.
Keep your database information with you, you will need it in Part 2.
Run the following MySQL query:
PHP Code:
CREATE TABLE `sitestatus` (
`id` INT( 2 ) NOT NULL AUTO_INCREMENT ,
`status` VARCHAR( 10 ) NOT NULL ,
`offmsg` TEXT NOT NULL ,
`onpage` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM
INSERT INTO `sitestatus` (
`id` ,
`status` ,
`offmsg` ,
`onpage`
)
VALUES (
'1', 'off', 'The site is currently offline for some much needed maintenance work on the frameworks!', 'http://example.com/'
);
You have now set up your database.
End of Step 1
Step 2:
The General Configuration
Create a new .php document, call it config.php, in the file, add the following code:
PHP Code:
<?php
//==============================================================================
$host = "localhost"; //Set this to the database host.
$user = "root"; //Set this to the database username.
$pass = ""; //Set this to the database password.
$name = "users"; //Set this to the database name.
$set_password = "12321"; //Set this to the password you want the user to provide before they're given access to the site.
//==============================================================================
mysql_connect($host,$user,$pass); //Connects to the database.
mysql_select_db($name); //Selects the database.
$info = mysql_query("SELECT * FROM `sitestatus` WHERE `id` = '1' ;"); //Is getting the information we need.
$i = mysql_fetch_object($info); //Is formatting the information we need.
?>
Read the comments, then the code will all make sence.
Now, you need to edit the database connection info at the top of the script, you should already know this from when you set up the database in Part 1.
End of Step 2
Step 3:
The Login & Information Editor
Create yet another .php document, this time calling it login.php, in the file, add the following code:
PHP Code:
<?php
require("config.php"); //Includes the configuration file we made earlier.
if(!isset($_POST['login'])){ //If they haven't posted the login form...
echo("Please enter the password to edit the site status!<br />
<form method=\"post\">
Password: <input type=\"password\" name=\"password\">
<br />
<input type=\"submit\" value=\"Login\" name=\"login\">
</form>");
//^^ Then show the login form so they can login.
}else{ //If they have posted the login form...
$s_password = $_POST['password']; //Change the password variable to something easier to handle.
if($s_password != $set_password){ //If the supplied password doens't match the set password in the configuration file...
die("You did not enter the correct password!"); //Tell them so, and close the session so they can't edit anything.
} //Ends the if statement (the supplied password didn't match the set one)
setcookie("loggedin","yes"); //Sets a cookie with the users information in, will be used in the next step.
header("Location: edit.php"); //Sends them off to the edit info page.
} //End the if statement (the login from hasn't been posted)
mysql_close(); //Close any remaining MySQL connections.
?>
You shouldn't need to edit this code too much.
Now, moving onto the information editor page, create another .php document, this one called edit.php, in the file, add the following code:
PHP Code:
<?php
require("config.php"); //Includes the configuration file.
if($_COOKIE[loggedin] == "yes"){ //If they're logged in...
if(!isset($_POST['update'])){ //Check to see if they've posted the form yet.
echo("Please choose the setting to edit here:<br />
<form method=\"post\">
Site Status: <select name=\"status\"><option value=\"on\">On</option><option value=\"off\">Off</option></select>
<br />
Offline Message: <input type=\"text\" name=\"offmsg\" size=\"40\">
<br />
Online Page To Redirect To: <input type=\"text\" name=\"onpage\" size=\"30\">
<br />
<input type=\"submit\" value=\"Update!\" name=\"update\">
</form>");
//^^ Show them the form because they haven't posted it yet.
}else{ //If they have posted the form...
$status = $_POST['status']; //Makes the variable easier to handle.
$offmsg = $_POST['offmsg']; //Makes the variable easier to handle.
$onpage = $_POST['onpage']; //Makes the variable easier to handle.
mysql_query("UPDATE `sitestatus` SET `status` = '" . $status . "', `offmsg` = '" . $offmsg . "', `onpage` = '" . $onpage . "' ;"); //Updates the MySQL database with the information they specified in the form.
echo("The site has been succesfully updated!"); //Tells them that the database has been updated.
setcookie("loggedin","",time() - 3600); //Unsets the logged in cookie.
} //End the if statement (if have(n't) posted the form yet.
}else{ //If they're not logged in...
die("Please login to edit the site status!"); //End the if statement(if they're (not) logged in.
} //End the if statment (if not logged in)
mysql_close(); //Closes any remaining MySQL connections.
?>
You shouldn't need to edit this code too much either.
End of Part 3.
Part 4:
The Index page
Create the final .php document, this one called index.php, it will replace the your current index, which you should rename to index2.php so you can make this script redirect to it when you've finished.
On the page, add the following code:
PHP Code:
<?php
require("config.php"); //Includes the configuration file we made earlier.
if($i->status == "on"){ //If the site status is set to on...
header($i->onpage); //Then forward the user to the online page.
}elseif($i->status == "off"){ //If the site status is set to off...
echo("The site is currently in offline mode!<br />The reason for this is: <b>" . $i->offmsg . "</b><br />We appologise for any disturbance this may cause.");
//^^ Then tell them so, and show the offline message.
} //Closes the statement.
mysql_close(); //Stops any MySQL connections we had running.
?>
You shouldn't need to edit this code much, unless you want to change the phrasing on the offline page.
End of Part 4.
Now you should have completed my tutorial, I hope you have learnt something from it!
If you have any comments to make, then please make them below.
Thanks alot & I hope I've helped,
Edd.
Thread moved to tutorials & Guides by --ss-- (Forum Super Moderator): Excellent tutorial!