序言
做爲一名php開發工程師,確定少不了本身開發web系統項目。若是項目是面向大衆的,須要他人安裝你的產品,不可缺乏的就是須要弄個安裝嚮導,這樣才能讓他們簡單輕鬆的安裝你的產品。若是你以爲不必,以爲寫個文檔教程就能夠,那我想說,你的產品是面向同行人...不過做爲程序員,最終咱們仍是須要學習怎樣開發出系統的安裝嚮導,由於這不是有沒有用的問題,而是學沒學到的問題.... php
咱們都知道,通常系統有沒有安裝都是經過判斷系統中是否有某種文件,有則說明已安裝,沒有則未安裝。而這個文件是安裝完成後生成的,因此能夠拿來判斷。在這裏我也是使用判斷文件的方式來判斷系統是否已安裝。但這裏有個問題,對於使用新浪SAE來講,因爲不支持本地文件寫操做,那咱們就生成不了文件,這樣判斷文件是否存在就無效了。而這裏的解決方法是將文件生成在新浪的storage,但這裏又有個問題,就是生成的操做方式不同,storage是新浪SAE爲開發者提供的分佈式文件存儲服務,咱們只能用它給出的類來生成文件,因此若是系統須要在新浪SAE上完成安裝嚮導的話,則須要判斷當前是哪中平臺,而後根據不一樣平臺調用不一樣的方法.... css
開始
目錄結構
install --------------------------------->安裝入口文件夾
├ templates ------------------------->頁面模板文件夾
│ ├ images -------------------------->頁面圖片文件夾
│ │ └ ....
│ ├ js -------------------------------->頁面js文件夾
│ │ ├ jquery.js
│ │ └ validate.js
│ ├ css ------------------------------>頁面css文件夾
│ │ └ install.css
│ ├ 0.php ---------------------------->獲取新浪sae storage 頁面
│ ├ 1.php ---------------------------->安裝許可協議頁面
│ ├ 2.php ---------------------------->運行環境檢測頁面
│ ├ 3.php ---------------------------->安裝參數設置頁面
│ ├ 4.php ---------------------------->安裝詳細過程頁面
│ ├ 5.php ---------------------------->安裝完成頁面
│ ├ header.php --------------------->公共頁面頭部
│ └ footer.php ---------------------->公共頁面尾部
├ config.ini.php --------------------->數據庫配置文件模板
├ config.php ------------------------>安裝配置文件
├ index.php ------------------------->系統安裝入口
├ location.php ---------------------->本地環境安裝,非雲平臺
├ main.php -------------------------->當數據寫入到數據庫後,進行添加管理員,生成配置文件等操做
├ sae.php --------------------------->新浪sae平臺
├ db.sql ----------------------------->數據庫文件
└ license.txt ------------------------->協議文件 html
圖結構
install文件夾做爲安裝入口文件存放的地方,由於在安裝完成後這個文件夾是能夠刪除的,因此在開發的時候,這部分須要獨立出來,就是刪除後不影響系統運行... mysql
步驟
一、當進入安裝時,首先運行index.php入口文件
二、而後獲取配置信息config.php
2 |
$config = include './config.php'; |
4 |
exit(get_tip_html('安裝配置信息不存在,沒法繼續安裝!')); |
這裏的配置信息的目的是:只須要修改這個文件就能兼容在其餘系統上,而不須要修改太多的其餘文件 jquery
08 |
'powered'=>'Powered by chenhaizan.com', |
10 |
'footerInfo'=> 'Copyright © 2012-2013 chenhaizan.cn Corporation', |
14 |
'sqlFileName'=>'db.sql', |
16 |
'dbSetFile'=>'config.ini.php', |
20 |
'dbPrefix' => 'haizan_', |
24 |
'siteKeywords' => '個人博客', |
26 |
'siteDescription' => '個人博客', |
28 |
'uploaddir' => 'upload', |
36 |
'includes/uc_client/data', |
38 |
/* ------寫入數據庫完成後處理的文件------ */ |
39 |
'handleFile' => 'main.php', |
40 |
/* ------安裝驗證/生成文件;非雲平臺安裝有效------ */ |
41 |
'installFile' => '../config/install.lock', |
42 |
'alreadyInstallInfo' => '你已經安裝過該系統,若是想從新安裝,請先刪除站點config目錄下的 install.lock 文件,而後再嘗試安裝!', |
三、而後進行判斷當前運行的平臺,獲取相應的平臺文件
02 |
if(function_exists('saeAutoLoader')){ |
04 |
define('INSTALLTYPE', 'SAE'); |
06 |
}elseif(isset($_SERVER['HTTP_BAE_ENV_APPID'])){ |
08 |
define('INSTALLTYPE', 'BAE'); |
11 |
define('INSTALLTYPE', 'HOST'); |
13 |
require './localhost.php'; |
如當是本地環境時,加載location.php文件,咱們在這個文件中進行是否安裝判斷等操做 程序員
2 |
if(file_exists($config['installFile'])){ |
3 |
exit(get_tip_html($config['alreadyInstallInfo'])); |
7 |
function filewrite($file){ |
當在SAE中,加載sae.php,進行獲取storage domain操做,判斷安裝操做和一些服務是否開啓 web
002 |
if($_GET['step'] == 0){ |
003 |
if(empty($_POST['storagedomain'])){ |
004 |
$step_html = '<li class="current"><em>0</em>Storage設置</li>'; |
005 |
include './templates/0.php'; |
008 |
$_SESSION['STORAGEDOMAIN'] = $_POST['storagedomain']; |
009 |
if(!empty($_SESSION['STORAGEDOMAIN'])){ |
010 |
header('location:./index.php?step=1'); |
015 |
if(!isset($_SESSION['STORAGEDOMAIN']) || empty($_SESSION['STORAGEDOMAIN'])){ |
016 |
header('location:./index.php?step=0'); |
019 |
$config['uploaddir'] = $_SESSION['STORAGEDOMAIN']; |
020 |
define('SAESTOR_INSTALL_NAME', $_SESSION['STORAGEDOMAIN'].'/saestor_'. $_SERVER['HTTP_APPVERSION'] . '_install.lock'); |
021 |
$config['alreadySaeInstallInfo'] = "版本" . $_SERVER['HTTP_APPVERSION'] . "已完成安裝!請刪除網站根目錄下的install目錄!<br>若是須要從新安裝,請先刪除storage內的 saestor_" . $_SERVER['HTTP_APPVERSION'] . "_install.lock 文件"; |
022 |
if(fileExists(SAESTOR_INSTALL_NAME)){ |
023 |
exit(get_tip_html($config['alreadySaeInstallInfo'])); |
026 |
exit(get_tip_html('請開啓storage服務!')); |
029 |
exit(get_tip_html('請開啓memcahce服務!')); |
032 |
exit(get_tip_html('請開啓mysql服務!')); |
035 |
exit(get_tip_html('請開啓KV數據庫服務!')); |
039 |
function SaeStorage(){ |
040 |
static $SaeStorage = array(); |
041 |
if(!isset($SaeStorage['SaeStorage'])){ |
042 |
$SaeStorage['SaeStorage'] = new SaeStorage(); |
044 |
return $SaeStorage['SaeStorage']; |
047 |
function file_getdomainfilepath($filename){ |
048 |
$arr=explode('/',ltrim($filename,'./')); |
049 |
if($arr[count($arr)-1] == ''){ |
050 |
unset($arr[count($arr)-1]); |
052 |
$domain=array_shift($arr); |
053 |
$filePath=implode('/',$arr); |
054 |
return array('domain'=>$domain,'filepath'=>$filePath); |
057 |
function fileExists($filename){ |
058 |
$arr=file_getdomainfilepath($filename); |
059 |
return SaeStorage()->fileExists($arr['domain'], $arr['filepath']); |
062 |
function filewrite($file = ''){ |
063 |
$arr=file_getdomainfilepath(SAESTOR_INSTALL_NAME); |
064 |
SaeStorage()->write($arr['domain'], $arr['filepath'],'1'); |
069 |
function is_storage() { |
070 |
$s = new SaeStorage(); |
071 |
if (!$s->write(SAESTOR_NAME, 'is_storage', '1')) { |
079 |
$mmc = @memcache_init(); |
087 |
function is_mysql() { |
088 |
$mysql = @new SaeMysql(); |
089 |
$sql = "select database()"; |
090 |
$data = @$mysql->getData($sql); |
四、而後進行一些配置信息和知足條件的判斷
02 |
$phpversion = phpversion(); |
04 |
if($phpversion < '5.2.0'){ |
05 |
exit(get_tip_html('您的php版本太低,不能安裝本軟件,請升級到5.2.0或更高版本再安裝,謝謝!')); |
08 |
if(!file_exists('./'.$config['sqlFileName'])){ |
09 |
exit(get_tip_html('數據庫文件不存在,沒法繼續安裝!')); |
12 |
if (!file_exists('./'.$config['handleFile'])) { |
13 |
exit(get_tip_html('處理文件不存在,沒法繼續安裝!')); |
五、進行安裝流程步驟
1 |
$step = isset($_GET['step']) ? $_GET['step'] : 1; |
0)、設置stirage 當運行在sae上,首先咱們須要獲得storage的domain,由於須要判斷storage中是否存生成的文件 ajax
因此須要頁面跳轉到0.php,進行domain設置 sql
1)、安裝許可協議 數據庫
3 |
$license = @file_get_contents('./license.txt'); |
4 |
include ("./templates/1.php"); |
2)、運行環境檢測
10 |
if (function_exists('mysql_connect')) { |
11 |
$server['mysql'] = '<span class="correct_span">√</span> 已安裝'; |
13 |
$server['mysql'] = '<span class="correct_span error_span">√</span> 出現錯誤'; |
17 |
if (ini_get('file_uploads')) { |
18 |
$server['uploadSize'] = '<span class="correct_span">√</span> ' . ini_get('upload_max_filesize'); |
20 |
$server['uploadSize'] = '<span class="correct_span error_span">√</span>禁止上傳'; |
23 |
if (function_exists('session_start')) { |
24 |
$server['session'] = '<span class="correct_span">√</span> 支持'; |
26 |
$server['session'] = '<span class="correct_span error_span">√</span> 不支持'; |
30 |
$folder = $config['dirAccess']; |
31 |
$install_path = str_replace('\\','/',getcwd()).'/'; |
32 |
$site_path = str_replace('install/', '', $install_path); |
33 |
include ("./templates/2.php"); |
34 |
$_SESSION['INSTALLSTATUS'] = $error == 0?'SUCCESS':$error; |
檢測環境須要記錄錯誤,這裏用session保存,若是有錯誤,將不能進行下一步的安裝。在本地環境上
若是在sae上,由於sae已經禁止了本地文件操做,因此不必檢測讀寫判斷,這裏經過INSTALLTYPE判斷進行隱藏
3)、安裝參數設置
05 |
if (isset($_GET['testdbpwd'])) { |
06 |
empty($_POST['dbhost'])?alert(0,'數據庫服務器地址不能爲空!','dbhost'):''; |
07 |
empty($_POST['dbuser'])?alert(0,'數據庫用戶名不能爲空!','dbuser'):''; |
08 |
empty($_POST['dbname'])?alert(0,'數據庫名不能爲空!','dbname'):''; |
09 |
empty($_POST['dbport'])?alert(0,'數據庫端口不能爲空!','dbport'):''; |
10 |
$dbHost = $_POST['dbhost'] . ':' . $_POST['dbport']; |
11 |
$conn = @mysql_connect($dbHost, $_POST['dbuser'], $_POST['dbpw']); |
12 |
$conn?alert(1,'數據庫連接成功!','dbpw'):alert(0,'數據庫連接失敗!','dbpw'); |
15 |
$domain = empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; |
16 |
if ((int) $_SERVER['SERVER_PORT'] != 80) { |
17 |
$domain .= ":" . $_SERVER['SERVER_PORT']; |
19 |
$scriptName = !empty($_SERVER["REQUEST_URI"]) ? $scriptName = $_SERVER["REQUEST_URI"] : $scriptName = $_SERVER["PHP_SELF"]; |
20 |
$rootpath = @preg_replace("/\/(I|i)nstall\/index\.php(.*)$/", "", $scriptName); |
21 |
$domain = $domain . $rootpath; |
22 |
include ("./templates/3.php"); |
在本地環境上
若是在sae上,由於sae已經將數據庫的信息設置爲常量,因此這裏不須要給出數據庫輸入框,經過INSTALLTYPE判斷進行隱藏頁面的數據庫輸入部分,留出表前綴輸入框
4)、安裝詳細過程 困難的部分就在這一步,涉及到數據庫的寫入。
這裏設計是提交獲得配置信息後,跳轉到數據庫信息寫入頁面,由於須要在頁面中動態顯示建立表的信息,因此須要使用ajax來獲取信息。驗證數據庫鏈接正確性後,將數據庫文件提取出來,進行分割解析,獲得數組,而後循環運行每條sql語句,同時判斷當前語句是否爲建立表,若是是建立表,執行這條語句後,返回ajax信息,帶回當前數組key+1參數,先後接收後顯示在頁面,而後再發送ajax請求,帶回key參數,循環到結束。
在發送請求時,也須要驗證配置參數,這裏將配置信息json在頁面上
1 |
var data = <!--?php echo json_encode($_POST);?-->; |
經過$_GET['install']判斷ajax請求
完整js以下
02 |
var data = <!--?php echo json_encode($_POST);?-->; |
03 |
$.ajaxSetup ({ cache: false }); |
05 |
var url = "./index.php?step=4&install=1&n="+n; |
11 |
success: function(data){ |
12 |
$('#loginner').append(data.info); |
17 |
$('#installloading').removeClass('btn_old').addClass('btn').html('繼續安裝').unbind('click').click(function(){ |
23 |
$('#installloading').removeClass('btn_old').addClass('btn').attr('href','./index.php?step=5').html('安裝完成...'); |
24 |
setTimeout(function(){ |
25 |
window.location.href='./index.php?step=5'; |
當在sae平臺時,須要獲取在sae上的數據庫信息
01 |
if (!isset($_GET['install'])){ |
05 |
$_POST['dbhost'] = SAE_MYSQL_HOST_M; |
07 |
$_POST['dbport'] = SAE_MYSQL_PORT; |
09 |
$_POST['dbname'] = SAE_MYSQL_DB; |
11 |
$_POST['dbuser'] = SAE_MYSQL_USER; |
13 |
$_POST['dbpw'] = SAE_MYSQL_PASS; |
17 |
$_POST['dbhost'] = HTTP_BAE_ENV_ADDR_SQL_IP; |
19 |
$_POST['dbport'] = HTTP_BAE_ENV_ADDR_SQL_PORT; |
21 |
$_POST['dbuser'] = HTTP_BAE_ENV_SK; |
23 |
$_POST['dbpw'] = SAE_MYSQL_PASS; |
02 |
if (intval($_GET['install'])) { |
05 |
if($phpversion <= '5.3.0'){ |
06 |
set_magic_quotes_runtime(0); |
09 |
date_default_timezone_set('PRC'); |
11 |
$n = intval($_GET['n']); |
14 |
$dbHost = trim($_POST['dbhost']); |
16 |
$dbPort = trim($_POST['dbport']); |
18 |
$dbName = trim($_POST['dbname']); |
19 |
$dbHost = empty($dbPort) || $dbPort == 3306 ? $dbHost : $dbHost . ':' . $dbPort; |
21 |
$dbUser = trim($_POST['dbuser']); |
23 |
$dbPwd = trim($_POST['dbpw']); |
25 |
$dbPrefix = empty($_POST['dbprefix']) ? 'db_' : trim($_POST['dbprefix']); |
27 |
$c @ mysql_connect($dbHost, $dbUser, $dbPwd); |
32 |
mysql_query("SET NAMES 'utf8'"); //,character_set_client=binary,sql_mode=''; |
34 |
$version = mysql_get_server_info($conn); |
39 |
if (!mysql_select_db($dbName, $conn)) { |
41 |
if (!mysql_query("CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;", $conn)) { |
42 |
alert(0,'<li><span class="correct_span error_span">√</span>數據庫 ' . $dbName . ' 不存在,也沒權限建立新的數據庫!<span style="float: right;">'.date('Y-m-d H:i:s').'</span></li>'); |
44 |
alert(1,"<li><span class="correct_span">√</span>成功建立數據庫:{$dbName}<span style="float: right;" '="">".date('Y-m-d H:i:s')."</span></li>",0); |
49 |
$sqldata = file_get_contents('./'.$config['sqlFileName']); |
51 |
alert(0,'數據庫文件不能爲空!'); |
53 |
$sqlFormat = sql_split($sqldata, $dbPrefix,$config['dbPrefix']); |
59 |
$counts = count($sqlFormat); |
61 |
for ($i = $n; $i < $counts; $i++) { |
62 |
$sql = trim($sqlFormat[$i]); |
63 |
if (strstr($sql, 'CREATE TABLE')) { |
65 |
preg_match('/CREATE TABLE `([^ ]*)`/', $sql, $matches); |
67 |
preg_match('/CREATE TABLE IF NOT EXISTS `([^ ]*)`/', $sql, $matches); |
69 |
if(!empty($matches[1])){ |
70 |
mysql_query("DROP TABLE IF EXISTS `$matches[1]",$conn); |
71 |
$ret = mysql_query($sql,$conn); |
73 |
if(mysql_query($sql,$conn)){ |
74 |
$info = '<li><span class="correct_span">√</span>建立數據表' . $matches[1] . ',完成!<span style="float: right;">'.date('Y-m-d H:i:s').'</span></li> '; |
77 |
$info = '<li><span class="correct_span error_span">√</span>建立數據表' . $matches[1] . ',失敗,安裝中止!<span style="float: right;">'.date('Y-m-d H:i:s').'</span></li>'; |
83 |
$ret = mysql_query($sql); |
88 |
$data = include './'.$config['handleFile']; |
89 |
$_SESSION['INSTALLOK'] = $data['status']?1:0; |
90 |
alert($data['status'],$data['info']); |
92 |
include ("./templates/4.php"); |
寫入成功後,須要進行添加管理員,生成配置文件等操做,如上面代碼中的
2 |
$data = include './'.$config['handleFile']; |
3 |
$_SESSION['INSTALLOK'] = $data['status']?1:0; |
在配置文件中$config['handleFile']爲main.php
01 |
$username = trim($_POST['manager']); |
02 |
$password = trim($_POST['manager_pwd']); |
04 |
$site_name = addslashes(trim($_POST['sitename'])); |
06 |
$site_url = trim($_POST['siteurl']); |
08 |
$upload_path = $_SESSION['UPLOADPATH']; |
10 |
$seo_description = trim($_POST['sitedescription']); |
12 |
$seo_keywords = trim($_POST['sitekeywords']); |
14 |
mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$site_name' WHERE varname='site_name'"); |
15 |
mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$site_url' WHERE varname='site_domain' "); |
16 |
mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$seo_description' WHERE varname='site_description'"); |
17 |
mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$seo_keywords' WHERE varname='site_keywords'"); |
19 |
if(!empty($upload_path)){ |
20 |
mysql_query("UPDATE `{$dbPrefix}config` SET `value` = '$upload_path' WHERE varname='attach_storage_domain' "); |
22 |
if(INSTALLTYPE == 'HOST'){ |
24 |
$strConfig = file_get_contents('./' . $config['dbSetFile']); |
25 |
$strConfig = str_replace('#DB_HOST#', $dbHost, $strConfig); |
26 |
$strConfig = str_replace('#DB_NAME#', $dbName, $strConfig); |
27 |
$strConfig = str_replace('#DB_USER#', $dbUser, $strConfig); |
28 |
$strConfig = str_replace('#DB_PWD#', $dbPwd, $strConfig); |
29 |
$strConfig = str_replace('#DB_PORT#', $dbPort, $strConfig); |
30 |
$strConfig = str_replace('#DB_PREFIX#', $dbPrefix, $strConfig); |
31 |
$strConfig = str_replace('#AUTHCODE#', genRandomString(18), $strConfig); |
32 |
$strConfig = str_replace('#COOKIE_PREFIX#', genRandomString(6) . "_", $strConfig); |
33 |
$strConfig = str_replace('#DATA_CACHE_PREFIX#', genRandomString(6) . "_", $strConfig); |
34 |
$strConfig = str_replace('#SESSION_PREFIX#', genRandomString(6) . "_", $strConfig); |
35 |
@file_put_contents($config['dbConfig'], $strConfig); |
40 |
$verify = genRandomString(6); |
42 |
$ip = get_client_ip(); |
43 |
$password = md5($password . md5($verify)); |
44 |
$email = trim($_POST['manager_email']); |
45 |
$query = "INSERT INTO `{$dbPrefix}member` VALUES (1, 0, 0, '{$username}', '{$password}', '{$email}', '', '', 0, '', '', '{$verify}', 1, '{$time}', 0, 0, 1, 2, 1, '', 65535, 1, 1, 1, 1, 0, '')"; |
46 |
if(mysql_query($query)){ |
47 |
return array('status'=>2,'info'=>'成功添加管理員<br>成功寫入配置文件<br>安裝完成...'); |
49 |
return array('status'=>0,'info'=>'安裝失敗...'); |
若是在本地環境,將數據庫配置模板文件進行特定位置替換,生成配置文件到設置的$config['dbConfig'] (../config/config.ini.php)中
5)、安裝完成
3 |
include ("./templates/5.php"); |
5 |
if(isset($_SESSION['INSTALLOK']) && $_SESSION['INSTALLOK'] == 1){ |
6 |
filewrite($config['installFile']); |
在這一步生成.lock文件
安裝完成後再次運行時,出現提示信息
其餘一些函數
004 |
function get_tip_html($info){ |
005 |
return '<div style="border: 2px solid #69c; background:#f1f1f1; padding:20px;margin:20px;width:800px;font-weight:bold;color: #69c;text-align:center;margin-left: auto;margin-right: auto;border-radius: 5px;"><h1>'.$info.'</h1></div>'; |
008 |
function alert($status,$info,$type = 0){ |
009 |
exit(json_encode(array('status'=>$status,'info'=>$info,'type'=>$type))); |
011 |
function verify($step = 3){ |
013 |
//未運行環境檢測,跳轉到安裝許可協議頁面 |
014 |
if(!isset($_SESSION['INSTALLSTATUS'])){ |
015 |
header('location:./index.php'); |
018 |
//運行環境檢測存在錯誤,返回運行環境檢測 |
019 |
if($_SESSION['INSTALLSTATUS'] != 'SUCCESS'){ |
020 |
header('location:./index.php?step=2'); |
027 |
header('location:./index.php?step=3'); |
033 |
if(!isset($_SESSION['INSTALLOK'])){ |
034 |
header('location:./index.php?step=4'); |
039 |
function dataVerify(){ |
040 |
empty($_POST['dbhost'])?alert(0,'數據庫服務器不能爲空!'):''; |
041 |
empty($_POST['dbport'])?alert(0,'數據庫端口不能爲空!'):''; |
042 |
empty($_POST['dbuser'])?alert(0,'數據庫用戶名不能爲空!'):''; |
043 |
empty($_POST['dbname'])?alert(0,'數據庫名不能爲空!'):''; |
044 |
empty($_POST['dbprefix'])?alert(0,'數據庫表前綴不能爲空!'):''; |
045 |
empty($_POST['siteurl'])?alert(0,'網站域名不能爲空!'):''; |
046 |
empty($_POST['uploaddir'])?alert(0,'附件上傳的目錄不能爲空!'):''; |
047 |
empty($_POST['manager'])?alert(0,'管理員賬號不能爲空!'):''; |
048 |
empty($_POST['manager_pwd'])?alert(0,'管理員密碼不能爲空!'):''; |
049 |
empty($_POST['manager_email'])?alert(0,'管理員郵箱不能爲空!'):''; |
054 |
function testwrite($d) { |
055 |
$tfile = "_test.txt"; |
056 |
$fp = @fopen($d . "/" . $tfile, "w"); |
061 |
$rs = @unlink($d . "/" . $tfile); |
070 |
function dir_create($path, $mode = 0777) { |
073 |
$temp = explode('/', $path); |
075 |
$max = count($temp) - 1; |
076 |
for ($i = 0; $i < $max; $i++) { |
077 |
$cur_dir .= $temp[$i] . '/'; |
078 |
if (@is_dir($cur_dir)) |
080 |
@mkdir($cur_dir, $mode, true); |
081 |
@chmod($cur_dir, $mode); |
083 |
return dir_create($path); |
088 |
* @param $newTablePre 新的前綴 |
089 |
* @param $oldTablePre 舊的前綴 |
091 |
function sql_split($sql, $newTablePre, $oldTablePre) { |
093 |
if ($newTablePre != $oldTablePre){ |
094 |
$sql = str_replace($oldTablePre, $newTablePre, $sql); |
096 |
$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql); |
098 |
$sql = str_replace("\r", "\n", $sql); |
100 |
$queriesarray = explode(";\n", trim($sql)); |
102 |
foreach ($queriesarray as $k=>$query) { |
104 |
$queries = explode("\n", trim($query)); |
105 |
$queries = array_filter($queries); |
106 |
foreach ($queries as $query) { |
107 |
$str1 = substr($query, 0, 1); |
108 |
if ($str1 != '#' && $str1 != '-') |
116 |
* 產生一個指定長度的隨機字符串,並返回給用戶 |
118 |
* @param int $len 產生字符串的位數 |
121 |
function genRandomString($len = 6) { |
123 |
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", |
124 |
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", |
125 |
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", |
126 |
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", |
127 |
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", |
128 |
"3", "4", "5", "6", "7", "8", "9", '!', '@', '#', '$', |
129 |
'%', '^', '&', '*', '(', ')' |
131 |
$charsLen = count($chars) - 1; |
132 |
shuffle($chars); // 將數組打亂 |
134 |
for ($i = 0; $i < $len; $i++) { |
135 |
$output .= $chars[mt_rand(0, $charsLen)]; |
141 |
* @param integer $type 返回類型 0 返回IP地址 1 返回IPV4地址數字 |
144 |
function get_client_ip($type = 0) { |
145 |
$type = $type ? 1 : 0; |
147 |
if ($ip !== NULL) return $ip[$type]; |
148 |
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
149 |
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); |
150 |
$pos = array_search('unknown',$arr); |
151 |
if(false !== $pos) unset($arr[$pos]); |
153 |
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { |
154 |
$ip = $_SERVER['HTTP_CLIENT_IP']; |
155 |
}elseif (isset($_SERVER['REMOTE_ADDR'])) { |
156 |
$ip = $_SERVER['REMOTE_ADDR']; |
159 |
$l sprintf("%u",ip2long($ip)); |
160 |
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0); |
代碼下載
地址:
總結
安裝嚮導是參照水平凡的代碼,在基礎上進行增長代碼,因爲要兼容於sae,便於增長其餘平臺,便於修改等問題,使得在結構上費了很大的功夫,不少的時間,而後又要寫教程,製做圖片,使用有點力不從心了,因此在代碼上優化的不怎麼盡人意,不過花點時間學習也是很好的。