[eluser]phpMaster[/eluser]
hi!
Auto Install scripts - my ideas & code
A usual way to test if app is installed, is to check 'config.php' exists or is not empty.
I have another approach, that makes such tests
are not needed.
This will spare at least one IF execution, each time app is called.
My default 'config.php' have this:
Code:
<?php
session_start();
$_SESSION['install']=true;
header('location: install.php');
exit();
?>
After Auto Install parameters has been submitted,
a new 'config.php' is written with the values.
I prefer to use
auto install step by step. With only few fields in each.
In this code I am working on, there are 3 steps.
I use
switch - case and valid values from each step are stored $_SESSION.
'install.php'
Code:
<?php
session_start();
if(!isset($_SESSION['install']))
exit('no');
//install
$step = (isset($_POST['step'])) ? $_POST['step'] : 0;
switch( $step ):
case 1 :
$sitename=trim($_POST['sitename']);
include('install.forms.inc.php');
$len=strlen($sitename);
if($len<4){
$out=$form1.$ms1.'Site name too short'.$ms2;
break;
}
$_SESSION['sitename']=$sitename;
$out = $form2;
break;
case 2 :
$forum1=trim($_POST['forum1']);
$forum2=trim($_POST['forum2']);
$forum3=trim($_POST['forum3']);
include('install.forms.inc.php');
$len=strlen($forum1);
if($len<4){
$out=$form2.$ms1.'Forum 1 name too short'.$ms2;
break;
}
$len=strlen($forum2);
if($len!=0 && $len<4){
$out=$form2.$ms1.'Forum name too short'.$ms2;
break;
}
$len=strlen($forum3);
if($len!=0 && $len<4){
$out=$form2.$ms1.'Forum name too short'.$ms2;
break;
}
$_SESSION['forums']=array($forum1,$forum2,$forum3);
$out = $form3;
break;
case 3 :
$adminuser=trim($_POST['adminuser']);
$adminpass=trim($_POST['adminpass']);
include('install.forms.inc.php');
$len=strlen($adminuser);
if($len<6){
$out=$form3.$ms1.'Admin username too short'.$ms2;
break;
}
$len=strlen($adminpass);
if($len<6){
$out=$form3.$ms1.'Admin password too short'.$ms2;
break;
}
$_SESSION['adminuser']=$adminuser;
$_SESSION['adminpass']=$adminpass;
include('install.writeconfig.inc.php');
define("DBINSTALL",true);
include('install.db.inc.php');
$out = '<h2>Thank you.<br>You can now use your forum.</h2>';
$out.= 'Admin User: <b>'.$adminuser.'</b><br>';
$out.= 'Admin Pass: <b>'.$adminpass.'</b><br>';
break;
default:
include('install.forms.inc.php');
$out =$form1;
endswitch;
echo $header.$out.$footer;
?>
There is only one (1) 'echo' in total installing process!
This is when I know, I have managed to separate php-code/html-output well.
Regards, phpMaster