Jump to content

Install Script - fwrite(), $content and more funzies...


Recommended Posts

I'm currently making an install script for a blog software which I've been working on. I've got about 8 months PHP experience, so don't be afraid to go a little advanced/slap me about. What's happening is I have lots of forms fields, and they $_POST[]'s are being assigned variables, and then wrote to a file.

 

$file = fopen("./includes/config.php","w") or die("Could not open config file. CHMOD to 777 to work."); // opens config

$content = "<?php ";
$content .= "$"."mysql_server = '$mysql_server';";
$content .= "$"."mysql_username = '$mysql_username';";
$content .= "$"."mysql_password = '$mysql_password';";
$content .= "$"."mysql_database = '$mysql_database';";
$content .= "$"."address ='$url';";
$content .= "$"."desc ='$descrption';";
$content .= "$"."name = '$sitename';";
$content .= "$"."owner ='$ownername';";
$content .= "?>";

fwrite($file, $content); // write variables to file

fclose($file); // close the file

 

Which gives me:

<?php $mysql_server = 'localhost'; $mysql_username = ''; $mysql_password = '';$mysql_database = '';$address ='';$desc ='';$name = '';$owner ='';?>

 

The problem is that my first line is working fine, but the rest are not. The variables are the right ones, no spelling errors at all. Any ideas what to do to fix this?

 

EDIT: The

"$"."mys...

bit is to make sure that that the variable name is wrote to the file as plain text, where the actual variable is wrote as it's value.

 

Thanks,

Scott

$content = "<?php ";
$content .= "\$mysql_server = '$mysql_server';";
$content .= "\$mysql_username = '$mysql_username';";
$content .= "\$mysql_password = '$mysql_password';";
$content .= "\$mysql_database = '$mysql_database';";
$content .= "\$address ='$url';";
$content .= "\$desc ='$descrption';";
$content .= "\$name = '$sitename';";
$content .= "\$owner ='$ownername';";
$content .= "?>";

 

Is what you are looking for. You have to escape the $ inside of double quotes like shown.

Still not working. First line is however...

 

here is the whole thing:

if($_POST['installprocess'] == 1) { // where $_POST['installprocess'] is a hidden input

// checks is fields were empty, if so: will die.
if(empty($_POST['url'])) {
die("You forgot to specify a URL");
}

if(empty($_POST['sitename'])) {
die("You forgot to specify a Sitename");
}

if(empty($_POST['description'])) {
die("You forgot to specify a Description");
}

if(empty($_POST['ownername']))  {
die("You forgot to specify a  Ownername");
}

// server stuff
if(empty($_POST['mysqluser'])) {
die("You forgot to specify a  MySQL Username");
}

if(empty($_POST['mysqldatabase'])) {
die("You forgot to specify a  MySQL Database");
}

// admin/user1 stuff
if(empty($_POST['username'])) {
die("You forgot to specify a username for your account");
}

if(empty($_POST['password'])) {
die("You forgot to specify a password for your account");
}

if(empty($_POST['realname'])) {
die("You forgot to specify a real name for your account");
}

if(empty($_POST['email'])) {
die("You forgot to specify an email for your account");
}

// run this:
// generic variables
$_POST['url'] = $url;
$_POST['sitename'] = $sitename;
$_POST['description'] = $description;
$_POST['ownername'] = $ownername;
// server variables
if(!isset($_POST['mysqlserver'])) {
$_POST['mysqlserver'] = $mysql_server;
} else { $mysql_server = "localhost"; }
$_POST['mysqluser'] = $mysql_username;
$_POST['mysqlpassword'] = $mysql_password;
$_POST['mysqldatabase'] = $mysql_database;
// user variables
$_POST['username'] = $username;
$_POST['password'] = $password;
$_POST['realname'] = $realname;
$_POST['email'] = $email; 

mysql_connect($mysql_server, $mysql_username, $mysql_password) or die(mysql_error());
mysql_select_db(blog) or die("§");

$file = fopen("./includes/config.php","w") or die("Could not open config file. CHMOD to 777 to work."); // opens config
  
$content = "<?php ";
$content .= "\$mysql_server = '$mysql_server';";
$content .= "\$mysql_username = '$mysql_username';";
$content .= "\$mysql_password = '$mysql_password';";
$content .= "\$mysql_database = '$mysql_database';";
$content .= "\$address ='$url';";
$content .= "\$desc ='$descrption';";
$content .= "\$name = '$sitename';";
$content .= "\$owner ='$ownername';";
$content .= "?>";

fwrite($file, $content); // write variables to file

fclose($file); // close the file

I think it's because I've been an idiot and put the $_POST and $var the wrong way around, and overwriting the $_POST with an empty, non-existant variable.

 

EDIT: Seems it was that. All is working now... Thanks for the help with the double quote escape premiso! :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.