Whether you’re developing a project to sell or building it for a client, it’s always best to provide them with an installer file. So, they can set up your project on their machine with minimum technical knowledge. Take WordPress, for example. It provides a user-friendly installer file. So that people can easily set up WordPress on their servers. Ever wondered how to create your own PHP script installer, just like WordPress? Well, you’re in the right place, then! We’re going to build one for you in this article today.
Overview
Before we start writing any codes, let’s take a look at our overall plan for the task.
Here’s a list of steps we’ll cover to create a PHP script installer using PHP.
- Check installation status
Like WordPress or any other ready-made PHP script, we will check if the website is ready to use. Depending on the site’s status, we will provide users an installer form or let them enjoy the site!
- Create the script installer Form
We’ll create a simple form to receive configuration settings. Like, as database credentials, admin account setup, site title, subtitle, etc.
- Validate the configuration settings
Then we’ll run some validation tests. For example, checking if the provided database credentials are valid or not.
- Import Databases
Now that we’ve successfully validated the database credentials, we may automatically create all the necessary tables on the database by importing an SQL file.
- Complete the installation
Last but not least, we will save the provided credentials on the server. So, the next time users visit the website, we serve them the actual content instead of showing them the installer file again.
Seems good? Easy-peasy, right? Let’s do it, then!
Check installation Status
We all have that one file that contains all the configuration settings for the entire project, right? Well, we’ll begin by checking for the existence of that file. That means we’ll consider the project as installed if the file exists on the server. Otherwise, we’ll launch the installer file. Here’s a sample code to do that.
<?php
if (!file_exists( __DIR__ . '/conf.php')) {
require_once __DIR__ . '/installer.php';
die();
}else{
require_once __DIR__ . '/conf.php';
}
We’ve assumed the file name would be conf.php
. And you should run this check at the very beginning of your index.php
file.
Create The Script installer Form
In the last section, we included a file called installer.php
using the require_once PHP expression. Create that file if you haven’t already, and start with a basic form.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Install The App To Get Started</title>
</head>
<body>
<div>
<form method="post">
<label for="lname">Site Title</label>
<input type="text" name="title" placeholder="My First Site">
<label for="fname">Full URL</label>
<input type="text" name="fullurl" placeholder="The Base URL">
<label for="lname">Database Host</label>
<input type="text" name="host" placeholder="localhost">
<label for="lname">Database Username</label>
<input type="text" name="username" placeholder="username">
<label for="lname">Database Password</label>
<input type="text" name="password" placeholder="****">
<label for="lname">Database Name</label>
<input type="text" name="db_name" placeholder="db_name">
<input type="submit" name="submit" value="Submit">
</form>
</div>
</body>
</html>
Too boring? You may like to add some CSS to it. Here is a quick CSS snippet to add a bit of a colorful touch to your form.
#radioButtons{
margin: 5px 0 10px 0;
}
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit] {
width: 100%;
background-color: #016a70;
color: white;
padding: 14px 20px;
margin-top: 12px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #018c94;
}
div {
margin: auto;
width: 30%;
border-radius: 5px;
background-color: #ededed;
padding: 20px;
font-family: 'Work Sans', sans-serif;
}
Validate the credentials
The form that we just created asks the user for the database credentials. So, we need to validate if the user actually provided us correct credentials before saving them. Here’s a quick PHP code snippet to check that.
$con = mysqli_connect($_POST['host'],$_POST['username'],$_POST['password']);
$db_error = false;
if(!@$con)
{
$db_error = true;
echo "Sorry, these details are not correct.";
}
if(!$db_error and !@mysqli_select_db($con,$_POST['db_name']))
{
$db_error = true;
echo "The host, username and password are correct.
But something is wrong with the given database.";
}
Notice how we’re keeping track of a variable called $db_error
. We can use this variable to show another error in the file if we want.
Import The Database Tables
You might have exported your tables from MySQL database from terminal, phpMyAdmin, or you might have your own database backup and restore system. Doesn’t matter how you exported your tables. We’ll have to import them into the user-selected database, right? Here’s how to do it.
//Import SQL FIle
$filename = 'db.sql';
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';'){
// Perform the query
if(!mysqli_query($con, $templine)){echo 'Error performing query \'<strong>' . $templine . '\': ' . mysqli_error() . '<br /><br />';}
// Reset temp variable to empty
$templine = '';
}
}
Please modify the $filename
variable and put your own SQL file path here.
Complete the installation
We have checked if the configuration file exists on the server, launched the PHP script installer file in the absence of the file, created the installer form, validated the database connection, and finally imported the tables. What a long journey, huh! The process would repeat if we didn’t create the conf.php file after completing the installation. Let’s do it now and put the configuration settings from the form.
$connect_code="<?php
define('HOST','".$_POST['host']."');
define('USERNAME','".$_POST['username']."');
define('PASSWORD','".$_POST['password']."');
define('DB_NAME','".$_POST['db_name']."');
\$base = '".$_POST['fullurl']."';
\$siteTitle = '".$_POST['title']."';
\$con = mysqli_connect(HOST, USERNAME,PASSWORD,DB_NAME);
?>";
$fp = fopen(__DIR__ . '/conf.php','a');
fwrite($fp,$connect_code);
fclose($fp);
Your PHP script installer
Congrats! You’ve created your own PHP Script Installer in less than 15 minutes. Awesome, right? If you’re having trouble putting it all together. You can download the source code instead!
Blog Desire is Up for Sale!
Ready for a new chapter? Blog Desire could be yours!
Inquire Now!