Config.php maker NEED HELP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

I want a form that, once you press submit, writes down the data that you inputed, and records it to config.php in the same folder, so whenever your creating a webpage, you can include config.php, so you dont have to define the same variable again and again.

I fixed the code i wrote before(sort of)

Code: Select all

//config_maker.php
<html>
<head>
<title>Configuration File Maker</title>
</head>
<body>
<center><h3>Configuration file Maker</h3></center>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Site Title: <input type="text" name="site_title"><br>
Database Host(Usually Localhost): <input type="text" name="db_host"><br>
Database Username: <input type="text" name="db_user"><br>
Database Password: <input type="password" name="db_pass"><br>
Database Name: <input type="text" name="db_name"><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
if (isset($_POST["submit"]))
 {
  $site_title = $_POST["site_title"]; // Site title
  $db_host = $_POST["db_host"]; // Database Host
  $db_user = $_POST["db_user"]; // Database User name
  $db_pass = $_POST["db_pass"]; // Database Password
  $db_name = $_POST["db_name"]; // Database Name
  }
 
$content = "<?php
define('site_title', '$site_title');
define('db_host', '$db_host');
define('db_user', '$db_user');
define('db_pass', '$db_pass');
define('db_name', '$db_name');
?>";

$file_to_write = "Config.php"
$fp = fopen($file_to_write, 'w');
fwrite($fp, $content);
fclose($fp);
echo "Success! <br>";
echo "$file_to_write";
echo " has been written";

?>
</body>
</html>
This is what i got after reading your responses. I am a php noob. all i know are constants, defining variable, echos and the really basic stuff. so any help that gets this to work will be appreciated.

also i wrote the following code a week ago, but it didnt work, so i created the script above, but here is the script that sort of works

Code: Select all

<?php
//Defining Variables
$site_name = $_POST["site_name"];
$site_title = $_POST["site_title"];
$db_host = $_POST["db_host"];
$db_user = $_POST["db_user"];
$db_pass = $_POST["db_pass"];
$db_name = $_POST["db_name"]; 
?>
<html>
<head>
<title>Config.php Maker</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="write" id="write">
 Site Title
 <input name="site_title" type="text" id="site_title">
 <br>
Host
 <input name="db_host" type="text" id="db_host">
 <br>
 Database User
 <input name="db_user" type="text" id="db_user">
 <br>
 Database Password
 <input name="db_pass" type="password" id="db_pass">
 <br>
 Database Name
 <input name="db_name" type="text" id="db_name">
 <br>
 <input type="submit" name="Submit" value="Submit">
</form>
<?php

if ($_POST['Submit']) {

//Defining Variables
$site_title = $_POST["site_title"];
$db_host = $_POST["db_host"];
$db_user = $_POST["db_user"];
$db_pass = $_POST["db_pass"];
$db_name = $_POST["db_name"]; 

$file_to_write = 'Config.php';

$content = '<?php
define("site_title", "$site_title");
define("db_host", "$db_host");
define("db_user", "$db_user");
define("db_pass", "$db_pass");
define("db_name", "$db_name");
?>';

$fp = fopen($file_to_write, 'w');
fwrite($fp, $content);
fclose($fp);
echo "Success! <br>";
echo "$file_to_write";
echo " has been written";
}

?>
<br>
<a href="chmod.php">Chmod Config.php</a><br>
<a href="delete.php">Click Here to Delete Config.php</a>
</body>
</html>

Code: Select all

//delete.php
<?php
$myFile = "config.php";
unlink($myFile);
echo " $myFile deleted";
?>
<html>
<head>
<title>Config.php Delete</title>
</head>
<body>
<a href="config_maker.php">Go Back</a>
</body>
</html>

Code: Select all

//chmod.php
<?php
$file = "config.php"
chmod("config.php", 0777); 
echo "$file chmoded";
?>
<html>
<head>
<title>Config.php Chmod</title>
</head>
<body>
<a href="config_maker.php">Go Back</a>
<a href="delete.php">Click Here to Delete Config.php</a>
</body>
</html>
And i get this result when i open config.php

Code: Select all

<?php
define("site_title", "$site_title");
define("db_host", "$db_host");
define("db_user", "$db_user");
define("db_pass", "$db_pass");
define("db_name", "$db_name");
?>
i dont get the values i inserted in the form
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

also: echo $file_to_write; (no quotes)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is this for a distributed application? I only ask because it seems like a lot of work to write a single file. Unless you are practicing, then more power to you.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

I'm not sure yet. if i get a good enough knowledge to learn php, i might incorporate it in a distributed application. right now i need it for myself and also i am practicing. 8)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The process you are following looks like something that would set up a config file for a distributed app (ala phpBB). It is not exactly difficult to do, but it is also not exactly entry level either.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

yes but i wanted a more simplified version than of phpbb. there too complicated and i dont understand those
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are on the right track. Just really pay close attention to the logic. What if a user enters an invalid string (or possibly an XSS string)? What happens if the user accidentally stumbles across this page after the config has been written to once? What happens if they enter incorrect data? What happens if they hit refresh after posting, or go back after submitting.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i'll worry about that later. First i need to get it to work!!
can u help on the CODE?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

All of us can help on the code. My first recommendation is the pseudo code what you want to do...

Code: Select all

<?php
// if the user submits the form
// validate their inputs
// If the inputs validate, test the inputs
// If the tests succeed, attempt use of the inputs
// If the use works, try to open the config file
// If the file open works, attempt to write to it
// ...
// You kinda get where I am going with this?
?>
When you get your comments written, add the code that makes it do what you want the comments to do.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i'm a noob. I dont know these things . can you tell me the actual code i need to put?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Yes we can give you the code you need... We won't... but we can.

Attempt it yourself, if you run into problems we'll help you solve them, we won't do it for you (unless we get paid :))

You will never learn to do it yourself if you are hand-fed all the solutions.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

can you at least give me some links?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You have come up with quite a bit so far. Why not take what we have offered so far, adapt it to the code you have written already, then keep asking us questions. I know it sounds a little off, but it really does work well when we do things like that.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

ok, but can you give me some links as to where i can learn this stuff, or where they teach it?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

You need to do a little searching via google.com and php.net they are your best friends....

Your getting the output:

Code: Select all

<?php
define("site_title", "$site_title");
define("db_host", "$db_host");
define("db_user", "$db_user");
define("db_pass", "$db_pass");
define("db_name", "$db_name");
?>
Because:

Code: Select all

$content = '<?php   // single quote not double
define("site_title", "$site_title");
define("db_host", "$db_host");
define("db_user", "$db_user");
define("db_pass", "$db_pass");
define("db_name", "$db_name");
?>';// end single quote not double
Read up on it here: http://www.weberdev.com/PrintExample.ph ... mode=color
Post Reply