This site uses cookies from Google to deliver its services, to personalize ads and to analyze traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. Learn More

Php Installation Script

Php Installation Script PHP Php Installation Script

In this post, we will discuss about installation script for our web site or web application. I divided into four functions for four steps.

Before we do the first step, we need some code to call our functions according to the url.
Open new document in your favourite editor and type below code and save as install.php

<!DOCTYPE html> <html> <head> <title>Installation Script</title> </head> <?php $step = (isset($_GET['step']) && $_GET['step'] != '') ? $_GET['step'] : ''; switch($step){ case '1': step_1(); break; case '2': step_2(); break; case '3': step_3(); break; case '4': step_4(); break; default: step_1(); } ?> <body> 
Step 1Step one is for license agreement. If the user agree to our license, we will call the step two. Type below code, in our install.php.

<?php function step_1(){ if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['agree'])){ header('Location: install.php?step=2'); exit; } if($_SERVER['REQUEST_METHOD'] == 'POST' && !isset($_POST['agree'])){ echo "You must agree to the license."; } ?> <p>Our LICENSE will go here.</p> <form action="install.php?step=1" method="post"> <p> I agree to the license <input type="checkbox" name="agree" /> </p> <input type="submit" value="Continue" /> </form> <?php } 
You need to do some design for your site. I don't do anything for that.
Step 2In step two, we will test the requirement for our site or application.

function step_2(){ if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['pre_error'] ==''){ header('Location: install.php?step=3'); exit; } if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['pre_error'] != '') echo $_POST['pre_error']; if (phpversion() < '5.0') { $pre_error = 'You need to use PHP5 or above for our site!<br />'; } if (ini_get('session.auto_start')) { $pre_error .= 'Our site will not work with session.auto_start enabled!<br />'; } if (!extension_loaded('mysql')) { $pre_error .= 'MySQL extension needs to be loaded for our site to work!<br />'; } if (!extension_loaded('gd')) { $pre_error .= 'GD extension needs to be loaded for our site to work!<br />'; } if (!is_writable('config.php')) { $pre_error .= 'config.php needs to be writable for our site to be installed!'; } ?> <table width="100%"> <tr> <td>PHP Version:</td> <td><?php echo phpversion(); ?></td> <td>5.0+</td> <td><?php echo (phpversion() >= '5.0') ? 'Ok' : 'Not Ok'; ?></td> </tr> <tr> <td>Session Auto Start:</td> <td><?php echo (ini_get('session_auto_start')) ? 'On' : 'Off'; ?></td> <td>Off</td> <td><?php echo (!ini_get('session_auto_start')) ? 'Ok' : 'Not Ok'; ?></td> </tr> <tr> <td>MySQL:</td> <td><?php echo extension_loaded('mysql') ? 'On' : 'Off'; ?></td> <td>On</td> <td><?php echo extension_loaded('mysql') ? 'Ok' : 'Not Ok'; ?></td> </tr> <tr> <td>GD:</td> <td><?php echo extension_loaded('gd') ? 'On' : 'Off'; ?></td> <td>On</td> <td><?php echo extension_loaded('gd') ? 'Ok' : 'Not Ok'; ?></td> </tr> <tr> <td>config.php</td> <td><?php echo is_writable('config.php') ? 'Writable' : 'Unwritable'; ?></td> <td>Writable</td> <td><?php echo is_writable('config.php') ? 'Ok' : 'Not Ok'; ?></td> </tr> </table> <form action="install.php?step=2" method="post"> <input type="hidden" name="pre_error" id="pre_error" value="<?php echo $pre_error;?>" /> <input type="submit" name="continue" value="Continue" /> </form> <?php } 
I add some requirement as the sample. You should change according to your requirement.
Step 3In step three, we need to create the config file and database for our site. And we will also save admin name and password in our database. To do so, our site users will fill their database information and admin information. You can also add other information that you need for your site.
function step_3(){ if (isset($_POST['submit']) && $_POST['submit']=="Install!") { $database_host=isset($_POST['database_host'])?$_POST['database_host']:""; $database_name=isset($_POST['database_name'])?$_POST['database_name']:""; $database_username=isset($_POST['database_username'])?$_POST['database_username']:""; $database_password=isset($_POST['database_password'])?$_POST['database_password']:""; $admin_name=isset($_POST['admin_name'])?$_POST['admin_name']:""; $admin_password=isset($_POST['admin_password'])?$_POST['admin_password']:""; if (empty($admin_name) || empty($admin_password) || empty($database_host) || empty($database_username) || empty($database_name)) { echo "All fields are required! Please re-enter.<br />"; } else { $connection = mysql_connect($database_host, $database_username, $database_password); mysql_select_db($database_name, $connection); $file ='data.sql'; if ($sql = file($file)) { $query = ''; foreach($sql as $line) { $tsl = trim($line); if (($sql != '') && (substr($tsl, 0, 2) != "--") && (substr($tsl, 0, 1) != '#')) { $query .= $line; if (preg_match('/;\s*$/', $line)) { mysql_query($query, $connection); $err = mysql_error(); if (!empty($err)) break; $query = ''; } } } @mysql_query("INSERT INTO admin SET admin_name='".$admin_name."', admin_password = md5('" . $admin_password . "')"); mysql_close($connection); } $f=fopen("config.php","w"); $database_inf="<?php define('DATABASE_HOST', '".$database_host."'); define('DATABASE_NAME', '".$database_name."'); define('DATABASE_USERNAME', '".$database_username."'); define('DATABASE_PASSWORD', '".$database_password."'); define('ADMIN_NAME', '".$admin_name."'); define('ADMIN_PASSWORD', '".$admin_password."'); ?>"; if (fwrite($f,$database_inf)>0){ fclose($f); } header("Location: install.php?step=4"); } } ?> <form method="post" action="install.php?step=3"> <p> <input type="text" name="database_host" value='localhost' size="30"> <label for="database_host">Database Host</label> </p> <p> <input type="text" name="database_name" size="30" value="<?php echo $database_name; ?>"> <label for="database_name">Database Name</label> </p> <p> <input type="text" name="database_username" size="30" value="<?php echo $database_username; ?>"> <label for="database_username">Database Username</label> </p> <p> <input type="text" name="database_password" size="30" value="<?php echo $database_password; ?>"> <label for="database_password">Database Password</label> </p> <br/> <p> <input type="text" name="admin_name" size="30" value="<?php echo $username; ?>"> <label for="username">Admin Login</label> </p> <p> <input name="admin_password" type="text" size="30" maxlength="15" value="<?php echo $password; ?>"> <label for="password">Admin Password</label> </p> <p> <input type="submit" name="submit" value="Install!"> </p> </form> <?php } 
According to our script, we need to crate a sql file. Open your Notepad and type blow sql script and save as data.sql in your project folder.
I create only admin table for the simplicity.
CREATE TABLE IF NOT EXISTS `admin` ( `admin_id` int(11) NOT NULL AUTO_INCREMENT, `admin_name` varchar(50) NOT NULL, `admin_password` varchar(50) NOT NULL, PRIMARY KEY (`admin_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

The next thing we need to do is to create a config.php file in your project folder. You don't need to do anything for this file save as blank.

Step 4In this function, I only write tow link for our site home page and for admin page.
function step_4(){ ?> <p><a href="http://localhost/installsample/">Site home page</a></p> <p><a href="http://localhost/installsample/admin">Admin page</a></p> <?php } ?> 
index.phpBelow is our index.php file.
<!DOCTYPE html> <html> <head> <title>Installation Script</title> </head> <body> <?php require 'config.php'; if (!defined('DATABASE_NAME')) { header('Location: install.php'); exit; } ?> <p>This is our site.</p> </body> </html> 
Below is some screenshot of our scritp.
PHP installation script

PHP installation script

0 Response to "Php Installation Script"

Posting Komentar

Contact

Nama

Email *

Pesan *