简体   繁体   English

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

[英]PHP install create a config.php with variables included

I have made an installer that will generate a config.php that looks like this: 我做了一个安装程序,将生成一个看起来像这样的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
    )
);

?>

Here is the code for my install script: 这是我的安装脚本的代码:

<?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');
}

?>

The problem is when it generates the config.php , I want it to print the config like: 问题是当它生成config.php ,我希望它以如下方式打印配置:

$dbuser = "blah blah"

But the thing is, its printing it like: 但问题是,它的打印方式如下:

'' = "blah blah"

Please help! 请帮忙!

When using double quotes you have to escape the variables, else PHP assumes that he has to parse that variabele (eventhough it might not exist). 当使用双引号时,您必须转义变量,否则PHP假定他必须解析该variabele(尽管它可能不存在)。 It's also a good practise to use parentheses between 'real' variables. 在“真实”变量之间使用括号也是一种好习惯。

You can also use single quotes and then it is not neccesary to escape them. 您也可以使用单引号,然后不必将其转义。

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

  ?>";

or 要么

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

  ?>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM