Jump to content

More efficient way of creating an installer


_Unique_

Recommended Posts

Hello,

I am still attempting to create a website installer via PHP, I have spent at least 2 weeks on trying to get my script working. The script I have is the base of the installer, it automatically assigns the user a step value via URL, I will then obtain that value via the $_GET method.

 

Whatever step value that is found in the URL is the current step of the user, and the information, etc will only be shown for that step. However, I have a problem, on Step 1, when the user has filled the form the form will submit, but the values will not be kept for step 2, 3, etc.

 

I am aware that you can store the values either by sessions or localstorage, but I am wondering if there is a more efficient method of doing this.

 

This is the installer page, that requires the manager file. This is what the user will see once the install page has loaded;

<?php

    require('manager.php');

?>
<!DOCTYPE HTML>
<html>

<head>
    <title>Website > Setup Configuration</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css" />
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
</head>

<body>
    <?php

        switch($currentStep) {
            /* Content For Step 0 */
            case 0;
                echo '
                        <p>Welcome to Website, before getting started we need some information about your MySQL database. You will need the following items before proceeding;</p>
                        <ol>
                            <li>Database host</li>
                            <li>Database username</li>
                            <li>Database password</li>
                            <li>Database name</li>
                        </ol>
                        <p>The information is going to be stored via FTP to establish a location between your website, and your database. If you are unaware of how to find these details, you can usually find them via your web hosting cPanel, if not, you can contact your web hoster.</p>
                        <p>Once you have the above details, press ‘Start install‘.</p>
                        <a href="install.php?step=1" class="next">Start install</a>
                ';
            break;
        
            /* Content For Step 1 */
            case 1;
                echo '
                    <p>Please enter the required MySQL details.</p>
                    <form action="" method="post">
                        <table>
                            <tr>
                                <td><label for="db-host">Database host</label></td>
                                <td><input type="text" name="db-host" /></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td><label for="db-username">Database username</label></td>
                                <td><input type="text" name="db-username" /></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td><label for="db-password">Database password</label></td>
                                <td><input type="password" name="db-password" /></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td><label for="db-name">Database name</label></td>
                                <td><input type="text" name="db-name" /></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td><input type="submit" name="db-submit" class="next" value="Next" /></td>
                            </tr>
                            <tr>
                                <td>'.$message.'</td>
                            </tr>
                        </table>
                    </form>
                ';
            break;
        
            /* Content For Step 2 */
            case 2;
                echo '
                <div id="outer-container">
                    <div id="inner-container">
                        <p>You need to create an administrator account for use on your website.</p>
                        
                    </div>
                </div>
                ';
            break;
            
            /* Content For Step 3 */
            case 3;
                echo '
                <div id="outer-container">
                    <div id="inner-container">
                        <p>You have now finished the setup! Expecting more steps?</p>
                    </div>
                </div>
                ';
            break;
        }

    ?>
</body>

</html>

And this is the manager.php, that is required by the install.php, the manager.php manages the background scripts, etc for the install.php.

<?php

    $currentStep = $_GET['step'];

    $message = '';

    if($currentStep == null || $currentStep == '') {
        header('Location: install.php?step=0');
        exit;
    }

    if($currentStep > 3) {
        die('Invalid Step!');
    }

    if(isset($_POST['db-submit'])) {
        if(empty($_POST['db-host']) || empty($_POST['db-username']) || empty($_POST['db-password']) || empty($_POST['db-name'])) {
            $message = 'Please fill in the required fields!';
        }else {
            $dbHost = $_POST['db-host'];
            $dbUser = $_POST['db-username'];
            $dbPass = $_POST['db-password'];
            $dbName = $_POST['db-name'];
            
            define('DB_HOST', $dbHost, true);
            define('DB_USER', $dbUser);
            define('DB_PASS', $dbPass);
            define('DB_NAME', $dbName);
            
            header('Location: install.php?step=2');
            exit;
        }
    }

?>

Once there is a more efficient method of what I am attempting to do, I am going to implement other features such as checking if there is a database connection once the user has entered the details, etc.

 

Oh by the way, I would rather the more 'efficient' method to be for PHP and not jQuery, etc, and I am not asking for someone to rewrite a script for me, I am just asking for someone to explain a more efficient method (thats how I learn best, but if you want to write me a script, then please leave messages explaining the different functions, etc).

 

Thanks in advance,

 

Unique

Link to comment
Share on other sites

If you dont want to use session then do not advance the user pass the first step if the database credentials are not correct. If the credentials are correct save them to your config file (as was discussed in your previous thread). On the install steps that require a database you would include your config file.

 

If you dont want the config file to be created until the install process has finished then use sessions for storing the credentials

Link to comment
Share on other sites

If you dont want to use session then do not advance the user pass the first step if the database credentials are not correct. If the credentials are correct save them to your config file (as was discussed in your previous thread). On the install steps that require a database you would include your config file.

 

If you dont want the config file to be created until the install process has finished then use sessions for storing the credentials

Okay, is there a way I can send the user to the install.php without getting a redirect loop?

 

I have this following script that I tried to get working, but for some reason, I keep getting the redirect loop error;

<?php

    function isInstalled() {
        if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/config/config.php')) {
            header('Location: http://'.$_SERVER['HTTP_HOST'].'/admin/install/setup.php?step=0');
            exit;
        }
    }

?>

Thanks

 

 

EDIT: Nevermind, I forgot to unlink the isInstalled() function from the setup.php. How dumb of me :/

Edited by _Unique_
Link to comment
Share on other sites

How is isInstalled() function being called? It should never need to be called from the installer.

 

A work around would be to not redirect if the current page is setup.php

function isInstalled()
{
    if(!strstr($_SERVER['SCRIPT_NAME'], 'setup.php') && 
       !file_exists($_SERVER['DOCUMENT_ROOT'].'/config/config.php'))
    {
        header('Location: http://'.$_SERVER['HTTP_HOST'].'/setup.php?step=0');
        exit;
    }
}
Link to comment
Share on other sites

similar to my reply in your other thread, if you are at the point of trying to make an installer script, you should be dynamically producing any form and dynamically processing the form data, rather than writing out the specific html/php code for each value for each step.

 

you would do this by having the form fields and validation steps for each field/type defined in a data structure somewhere (a database table or an array.) the data structure would contain the dynamic information for each field in each step, such as the form field name, the label for the form field, the type of form field, any lists of choices (select/checkbox/radio), and what validation step(s) to 'call' (using variable functions) for each field.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.