PHP安装创建一个包含变量的config.php

时间:2014-07-23 08:15:13

标签: php variables installation installer

我已经制作了一个安装程序,它将生成一个如下所示的config.php:

<?php 

$dbhost = "localhost";  // The host which hosts your database
$dbuser = "root";   // The user for the host/database
$dbpass = "";   // The users password
$dbname = "donationkeys";   // The name of the database which holds the activationkeys and notifications table

$colorscheme = "white";     // Choose from "white" or "black"

$config_gmail           =   true;                               //  Are we using gmail?
$config_gmail_username  =   "";     //  GMail username
$config_gmail_password  =   "";                     //  GMail password

$config_merchant_domain =   "merchant@domain.com";                  //  The email you want to appear on the email
$config_merchant_name   =   "merchant name";                    //  The name you want to appaer on the email

$config_merchant_email  =   "";             //  IMPORTANT! The email you use with paypal to receive payment

$config_USD             =   true;                           //  Currency, set this to false if using �'s i.e. GBP


// Below is the config for your own reference and the IPN. This will not affect your payment link on your donation page.
// You configure the payment links etc in index.html. Just to re state. This is for paypals verification only!

$config_options = array(

    "1k"=>array(
        "title"         => "1k in-game cash",
        "price"         => 1,
        "description"   => "This will give you 1,000 in-game cash and donator status.",
        "number"        => 1
    ),
    "5k"=>array(
        "title"         => "5k in-game cash",
        "price"         => 5.00,
        "description"   => "This will give you 5,000 in-game cash and donator status.",
        "number"        =>  2
    ),
    "10k"=>array(
        "title"         => "10k in-game cash",
        "price"         => 10.00,
        "description"   => "This will give you 10,000 in-game cash and donator status.",
        "number"        => 3
    ),
    "50k"=>array(
        "title"         => "50k in-game cash",
        "price"         => 20.00,
        "description"   => "This will give you 50,000 in-game cash and VIP status.",
        "number"        => 4
    )
);

?>

以下是我的安装脚本的代码:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = $_POST['un'];
    $password = $_POST['pw'];
    $database = $_POST['db'];
    $apikey = $_POST['ak'];
    $host = $_POST["hn"];
    $email = $_POST["pp"];
    $server = $_POST["sn"];
    $currency = $_POST["cu"];
    if(!empty($username) && !empty($password) && !empty($database)) {
    if(empty($host)){ $host = "localhost"; }
    $file_contents = "<?
    /* MySQL Information */
    '$dbuser' = '$username';

    ?>";

    $file_contents2 = "<?
    \$API_KEY = '$apikey';
    ?>
    ";
    file_put_contents('../inc/config.php', $file_contents, FILE_APPEND | LOCK_EX);
    file_put_contents('../inc/apikey.php', $file_contents2, FILE_APPEND | LOCK_EX);
    include('success.php');

    } 
    else {
        header('Location: index.php');
    }
} else {
    header('Location: index.php');
}

?>

问题是当它生成config.php时,我希望它打印配置如:

$dbuser = "blah blah"

但问题是,它的印刷方式如下:

'' = "blah blah"

请帮忙!

1 个答案:

答案 0 :(得分:0)

当使用双引号时,你必须转义变量,否则PHP假定他必须解析那个变量(尽管它可能不存在)。在“真实”变量之间使用括号也是一种很好的做法。

您也可以使用单引号,然后无需转义它们。

$file_contents = "<?
  /* MySQL Information */
  '\$dbuser' = '{$username}';

  ?>";

$file_contents = '<?
  /* MySQL Information */
  $dbuser = "' .$username . '";

  ?>';