PHP網站安裝程序的原理及代碼


原理:

其實PHP程序的安裝原理無非就是將數據庫結構和內容導入到相應的數據庫中,從這個過程中重新配置連接數據庫的參數和文件,為了保證不被別人惡意使用安裝文件,當安裝完成后需要修改安裝文件。

步驟:

1、檢查目錄或文件的權限 
2、修改或填加配置文件 
3、檢查配置文件正確性 
4、導入數據庫 
5、鎖定或刪除安裝文件 

具體代碼:


文件:由於只是展示原理,盡量讓其簡單化故用小Demo形式演示

install.html  為表單填寫文件

doAction.php    為處理表單文件

dbconfig.php  數據庫配置文件

index.php  執行成功跳轉頁面

install.html

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>安裝程序</title>
</head>
<body>
    <center>
        <h2>PHP在線安裝程序</h2>
    <hr/>
        <form action="doAction.php" method="post">
            <table>
                <tr>
                    <td>主機地址:</td>
                    <td><input type="text" name="host"/></td>
                </tr>
                <tr>
                    <td>數據庫賬號:</td>
                    <td><input type="text" name="username"/></td>
                </tr>
                <tr>
                    <td>數據庫密碼:</td>
                    <td><input type="password" name="password"/></td>
                </tr>
                    <td>數據庫名:</td>
                    <td><input type="text" name="dbname"/></td>
                </tr>
                    <tr>
                    <td>數據庫表前綴:</td>
                    <td><input type="text" name="flag"/></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center;">
                        <input type="submit" value="安裝"/>
                        <input type="reset" value="重置"/>
                    </td>
                </tr>
            </table>
            

        </form>
    </center>
</body>
</html>

doAction.php

 

<?php
    
    $filename="dbconfig.php";

//配置文件內容
    $config='<?php';
    $config.="\n";
    $config.='$host="'.$_POST["host"].'";';
    $config.="\n";
    $config.='$user="'.$_POST["username"].'";';
    $config.="\n";
    $config.='$pass="'.$_POST["password"].'";';
    $config.="\n";
    $config.='$dbname="'.$_POST["dbname"].'";';
    $config.="\n";
    $config.='$flag="'.$_POST["flag"].'";';
    $config.="\n";
    $config.="?>";


    if(is_writable($filename)){//檢測是否有權限可寫
        $handle=fopen($filename, "w+");
        fwrite($handle, $config);

        //連接數據庫
        include_once($filename);
        if(!@$link=mysql_connect($host,$user,$pass)){
            echo "數據庫連接失敗,<a href='install.php'>返回設置</a>";
        }else{
            mysql_query("create database if not exists `$dbname`");
            mysql_select_db($dbname,$link);

            //建表語句
            $sql[]="CREATE TABLE IF NOT EXISTS `".$flag."access` (
                      `role_id` smallint(6) unsigned NOT NULL,
                      `node_id` smallint(6) unsigned NOT NULL,
                      `level` tinyint(1) NOT NULL,
                      `module` varchar(50) DEFAULT NULL,
                      KEY `groupId` (`role_id`),    
                      KEY `nodeId` (`node_id`)
                    ) ENGINE=MyISAM DEFAULT CHARSET=utf8";

            $sql[]="CREATE TABLE IF NOT EXISTS `".$flag."node` (
                      `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
                      `name` varchar(20) NOT NULL,
                      `title` varchar(50) DEFAULT NULL,
                      `status` tinyint(1) DEFAULT '0',
                      `remark` varchar(255) DEFAULT NULL,
                      `sort` smallint(6) unsigned DEFAULT NULL,
                      `pid` smallint(6) unsigned NOT NULL,
                      `level` tinyint(1) unsigned NOT NULL,
                      PRIMARY KEY (`id`),
                      KEY `level` (`level`),
                      KEY `pid` (`pid`),
                      KEY `status` (`status`),
                      KEY `name` (`name`)
                    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

            $sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role` (
                      `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
                      `name` varchar(20) NOT NULL,
                      `pid` smallint(6) DEFAULT NULL,
                      `status` tinyint(1) unsigned DEFAULT NULL,
                      `remark` varchar(255) DEFAULT NULL,
                      PRIMARY KEY (`id`),
                      KEY `pid` (`pid`),
                      KEY `status` (`status`)
                    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

            $sql[]="CREATE TABLE IF NOT EXISTS `".$flag."role_user` (
                      `role_id` mediumint(9) unsigned DEFAULT NULL,
                      `user_id` char(32) DEFAULT NULL,
                      KEY `group_id` (`role_id`),
                      KEY `user_id` (`user_id`)
                    ) ENGINE=MyISAM DEFAULT CHARSET=utf8";

            foreach ($sql as $value) {//由於mysql_query不支持一次性執行多條語句,所以用for循環遍歷
                mysql_query($value);
            }

            echo "<script>window.location='index.php';</script>";
            rename("install.html", "install.lock");

        }


    }else{
        echo "您沒有權限操作。";
    }
?>

 

dbconfig.php

 

<?php
$host="localhost";
$user="root";
$pass="";
$dbname="demo";
$flag="lcw_";
?>

 

index.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>首頁</title>
</head>
<body>
    <h2>^_^ 數據導入成功。</h2>
</body>
</html>

 

執行完安裝文件(自動修改文件名):

數據庫導入成功!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM