简体   繁体   中英

PHP install create a config.php with variables included

I have made an installer that will generate a config.php that looks like this:

<?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:

$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). 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 . '";

  ?>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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