加入收藏 | 设为首页 | 会员中心 | 我要投稿 文章分享网_茂名站长网 (https://www.0668zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP网站安装程序的原理和代码

发布时间:2024-01-03 11:58:53 所属栏目:PHP教程 来源:DaWei
导读: 其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安装完成后需要修改安装文件。
其实PHP程序的安装原理无非就是将数据库结构和内容导入到相应的数据库中,从这个过程中重新配置连接数据库的参数和文件,为了保证不被别人恶意使用安装文件,当安装完成后需要修改安装文件。

步骤:

1、检查目录或文件的权限 

2、修改或填加配置文件 

3、检查配置文件正确性 

4、锁定或删除安装文件 

具体代码:

文件:由于只是展示原理,尽量让其简单化故用小Demo形式演示

install.html为表单填写文件

doAction.PHP  为处理表单文件

dbconfig.PHP  数据库配置文件

index.PHP执行成功跳转页面

install.html

<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" 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,

                      `pid` smallint(6) DEFAULT NULL,

                      `status` tinyint(1) unsigned DEFAULT NULL,

                      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" 

<head>

    <Meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

    <title>首页</title>

</head>

<body>

    <h2>^_^ 数据导入成功。</h2>

</body>

</html>

执行完安装文件(自动修改文件名):

数据库导入成功!

(编辑:文章分享网_茂名站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章