Installer tutorial

Any tutorials (or useful resources) should go in here.
Post Reply
Torniquet
Posts: 52
Joined: Sun Jun 19, 2011 8:10 am
Contact:

Installer tutorial

Post by Torniquet »

Taken from the suggestion from here... viewtopic.php?f=9&t=378

I have not long had to make one, So i will be going through the basic procedure of writing a database configuration file through php.

NOTE: This will note be the full outfit, as A. i dont have the time currently to do this, and it is really only a basic form submission like anything else.

If you dont know how to proccess a form submission, then you really are NOT at the point of needing to create an installer!

But this tutorial will cover the file creation.


So let us presume you have a form where a user inputs their database information. It is at the point of being parsed and tested. Everything is AOK and variables are defined...

[syntax=php]
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'password';
$db_table = 'table';
[/syntax]

we want to create a file which will contain something that resembles the following...

[syntax=php]
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "password";
$db_name = "table";

$dba = new mysqli($db_host, $db_user, $db_pass, $db_name);

define("INSTALLED", 1, true);
?>
[/syntax]

This is a basic database configuration file that you will find in places like phpBB.

So how do we create said file? Well it is a simple process of writing to a file.

We need to first define the target file.

[syntax=php]
$target_file = '../scripts/db_config.php';
[/syntax]

Simple...

Now what we want to do is create a handle and open the file. It doesnt matter if this file physically exists, because if it doesn't, it will be created for us. we will achive this by using the functon fopen with its I/O mode set to 'w'. This will erase anything contained in the file, making it a fresh blank file to work with.

Now we need to define what we want to contain inside the newly opened file.

Well we define a new variable called $contents. We will define this variable with the same layout and structure we would as if we were to write the file in notepad.

[syntax=php]
$content =
'<?php
$db_host = "' . $db_host . '";
$db_user = "' . $db_user . '";
$db_pass = "' . $db_password . '";
$db_name = "' . $db_name . '";

$dba = new mysqli($db_host, $db_user, $db_pass, $db_name);

define("INSTALLED", 1, true);
?>
';
[/syntax]

Now we have written this inside a string escaping only to place in dynamic variable information. One thing to make sure of, make sure there is NO white space between your opening quote and the first php tag.

The final part is simply to write the file to the system and then close the file. We will be using our $handle variable for both of these and the $content variable when we write.

The 2 methods we want to use are fwrite, in which we supply the handle and the content variables. The second is the fclose function in which we pass through the handle variable.

So...

[syntax=php]
fwrite($handle, $content);
fclose($handle);
[/syntax]

Sorted, we have now successfully written our new db_config.php file which will connect us to mysql databse using database information supplied by the end user.

Now let me just explain the 'define('INSTALLED', 1);' i have added.

In my system, i run a check to first see if the file exsists, if it does, we include it. I also make a defined check to make sure that the INSTALLED constant has been made. without the script the define wont happen, if we try to include a file which isnt there, we get an error. So to run this smoothly, i have done the following.

[syntax=php]
///// CALL DATABASE SETUP
if(file_exists($root_dir . "scripts/db_config.php"))
include ($root_dir . "scripts/db_config.php");

////// FORCE TO INSTALL IF INSTALL NOT DEFINED
if(!defined('INSTALLED'))
header("Location: install/");
[/syntax]

if the constant INSTALLED hasn't been defined, it means the config file has not been created, so we want to force the user to the install screen.

Hope that explains everything nice and easy for anyone who may need the code, either for an instillation script, or any kind of file you may need to write.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Installer tutorial

Post by Temor »

Great tutorial! Really well documented !
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Installer tutorial

Post by jacek »

You can also use the file_put_contents() function to simplify this a little. But yeah, very nice, thanks for contributing this :)
Image
Torniquet
Posts: 52
Joined: Sun Jun 19, 2011 8:10 am
Contact:

Re: Installer tutorial

Post by Torniquet »

no prob Jacek.

I dont write to files very much via php, but i will look into the file_put_contents() function next time i need to.

When i have the time, I will post up some little jQuery tuts.
Post Reply