|
-
Nov 7th, 2008, 12:29 PM
#1
Thread Starter
Frenzied Member
XML Configuration File
I'm rebuilding this "system" (5th time).
This time I want whoever want to use it, just copy it to their web directory, and use it.
First time, it should see there is no configuration file (xml), and create a copy from a template. The user will then be taken to a "settings" page, where they can enter and save database connection parameters.
After that, just when you run the system, it will just verify the config file exist, got the necessary nodes, and the values are still valid.
Wrote the following code, late at night (only way to keep gf studying for coming exam is sit here next to her working), and don't feel so comfortable about my code. But then, I never did this before. Though it does works perfectly well (Guess will set a session variable to say file have been checked. No use doing that each time a page load huh) Some masta mind take a look and comment?
Code:
<?php
if($page_id=="config_settings") /* dont care to chec file if we on edit settings page */
return;
$error = "";
/* ensure the existence of our configuration file. If not exists, copy and rename template configuration file */
$error = check_config_exists();
/* does the file have the necessary fields (not corrupted?) */
if(!$error) {
$filename = $level."config/config.xml";
$xml = simplexml_load_file($filename);
/* see if database variables section exists */
if(!isset($xml->database))
$error = "Corrupt configuration file (Database settings missing). You might need to reinstall DataShop.";
else if(!isset($xml->database->db_database))
$error = "Corrupt configuration file (Database name node missing). You might need to reinstall DataShop.";
else if(!isset($xml->database->db_username))
$error = "Corrupt configuration file (Database user name node missing). You might need to reinstall DataShop.";
else if(!isset($xml->database->db_password))
$error = "Corrupt configuration file (Database password node missing). You might need to reinstall DataShop.";
else if(!isset($xml->database->db_host))
$error = "Corrupt configuration file (Database host name node missing). You might need to reinstall DataShop.";
else if(!isset($xml->database->db_table_prefix))
$error = "Corrupt configuration file (Database table prefix node missing). You might need to reinstall DataShop.";
/*see if the variables are valid to connect to the database*/
if(!$error) {
@ $mysqli = new mysqli($xml->database->db_host, $xml->database->db_username, $xml->database->db_password, $xml->database->db_database);
if (mysqli_connect_errno()) {
header("Location:".$level."config/settings.php");
}
}
}
if($error)
echo "This app is up to *****";
else
echo "all seems ok";
function check_config_exists() {
$template = $level."config/ds_config_template.xml";
$filename = $level."config/config.xml";
if (!file_exists($filename)) {
if (!file_exists($template))
return "ds_config_template.xml seems to be missing. Please ensure this file exist in your config directory, or reinstall DataShop.";
if (!copy($template, $filename))
return "Failed to create config.xml. Please ensure ds_config_template.xml exists, copy and rename it to 'config.xml'. Otherwise reinstall DataShop.";
}
}
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|