代码指示符数据库配置以编程方式形成用户输入 [英] codeigniter database configuration programmatically form user input

查看:133
本文介绍了代码指示符数据库配置以编程方式形成用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我吗?



如何以编程方式配置CI数据库设置?我看到这与其他系统,如OSCommerce,Joomla,Wordpress等。



用户需要提交一个表格与数据库信息。



MojoMotor 基于CI的安装脚本使得添加到 application / config / database.php ,这意味着它



MM的许可协议禁止我发布实际的安装脚本,但您应该能够创建一些可以工作的东西。



否则,设想如下:

  //用户输入数据
db_config ['hostname'] = $ this-> input-> post('db_host');
$ db_config ['username'] = $ this-> input-> post('db_user');
$ db_config ['password'] = $ this-> input-> post('db_password');
$ db_config ['database'] = $ this-> input-> post('db_name');
$ db_config ['dbdriver'] = $ this-> db_driver;
$ db_config ['dbprefix'] = $ this-> db_prefix;
$ db_config ['pconnect'] =($ this-> input-> post('pconnect'))?真假;

$ this-> CI =& get_instance();
$ this-> CI-> load-> helper('file');

$ prototype = array(
'hostname'=>'localhost',
'username'=>'',
'password'=> '',
'database'=>'',
'dbdriver'=>'mysql',
'dbprefix'=>'mojo_',
'pconnect '=> TRUE,
'db_debug'=> FALSE,
'cache_on'=> FALSE,
'cachedir'=>'',
'char_set' =>'utf8',
'dbcollat​​'=>'utf8_general_ci'
);

//现在我们以字符串的形式读取文件数据
$ config_file = read_file(APPPATH.'config / database'.EXT);

//美元符号似乎使我们的preg_replace
//产生了一个问题,所以我们暂时把它们交换出来
$ config_file = str_replace('$','@s @',$ config_file);

//循环使用newconfig数组并换出数据
if(count($ dbconfig)> 0)
{
foreach($ dbconfig as $ key => $ val)
{
if($ val ==='y')
{
$ val = TRUE;
}
elseif($ val =='n')
{
$ val = FALSE;
}

if(is_bool($ val))
{
$ val =($ val == TRUE)? '真假';
}
else
{
$ val ='\''。$ val.'\'';
}

$ val。=';';

//更新值
$ config_file = preg_replace(#(\ @ s \ @ db \ [(['\])。$ active_group。 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ s \\1 $ val,$ config_file);
}
}

//将美元符号返回
$ config_file = str_replace('@ s @','$',$ config_file);

//只是为了确保我们没有任何不需要的空格
$ config_file = trim($ config_file);

//写文件
$ fp = fopen($ this-> database_path,FOPEN_WRITE_CREATE_DESTRUCTIVE);

flock($ fp,LOCK_EX);
fwrite($ fp,$ config_file,strlen($ config_file));
flock($ fp,LOCK_UN);
fclose($ fp);

我没有测试这个,但我想实现类似于我的一个



我希望这可以让你更近一些。


Can anyone help me ?

How can I configure the CI database settings programmatically ? I saw this with other systems like OSCommerce, Joomla, Wordpress etc..

The user needs to submit a form with database information.

解决方案

This is actually a pretty complicated process.

MojoMotor which is based on CI has an installation script which makes the additions to application/config/database.php which means it's not impossible.

The license agreement for MM prohibits me from posting the actual installation script, but you should be able to create something that works.

Otherwise, imagine something like this:

    // Data from user input
    $db_config['hostname'] = $this->input->post('db_host');
    $db_config['username'] = $this->input->post('db_user');
    $db_config['password'] = $this->input->post('db_password');
    $db_config['database'] = $this->input->post('db_name');
    $db_config['dbdriver'] = $this->db_driver;
    $db_config['dbprefix'] = $this->db_prefix;
    $db_config['pconnect'] = ($this->input->post('pconnect')) ? TRUE : FALSE;

    $this->CI =& get_instance();
    $this->CI->load->helper('file');

    $prototype = array(
                        'hostname'  => 'localhost',
                        'username'  => '',
                        'password'  => '',
                        'database'  => '',
                        'dbdriver'  => 'mysql',
                        'dbprefix'  => 'mojo_',
                        'pconnect'  => TRUE,
                        'db_debug'  => FALSE,
                        'cache_on'  => FALSE,
                        'cachedir'  => '',
                        'char_set'  => 'utf8',
                        'dbcollat'  => 'utf8_general_ci'
                    );

    // Now we read the file data as a string
    $config_file = read_file(APPPATH.'config/database'.EXT);

    // Dollar signs seem to create a problem with our preg_replace
    // so we'll temporarily swap them out
    $config_file = str_replace('$', '@s@', $config_file);

    // Cycle through the newconfig array and swap out the data
    if (count($dbconfig) > 0)
    {
        foreach ($dbconfig as $key => $val)
        {
            if ($val === 'y')
            {
                $val = TRUE;
            }
            elseif ($val == 'n')
            {
                $val = FALSE;
            }

            if (is_bool($val))
            {
                $val = ($val == TRUE) ? 'TRUE' : 'FALSE';
            }
            else
            {
                $val = '\''.$val.'\'';
            }

            $val .= ';';

            // Update the value
            $config_file = preg_replace("#(\@s\@db\[(['\"])".$active_group."\\2\]\[(['\"])".$key."\\3\]\s*=\s*).*?;#", "\\1$val", $config_file);
        }
    }

    // Put the dollar signs back
    $config_file = str_replace('@s@', '$', $config_file);

    // Just to make sure we don't have any unwanted whitespace
    $config_file = trim($config_file);

    // Write the file
    $fp = fopen($this->database_path, FOPEN_WRITE_CREATE_DESTRUCTIVE);

    flock($fp, LOCK_EX);
    fwrite($fp, $config_file, strlen($config_file));
    flock($fp, LOCK_UN);
    fclose($fp);

I haven't tested this, but I'm trying to implement something like similar to one of my own projects.

I hope this could bring you a little closer.

这篇关于代码指示符数据库配置以编程方式形成用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆