SELFPHP: Version 5.8.2 Befehlsreferenz - Tutorial – Kochbuch – Forum für PHP Einsteiger und professionelle Entwickler

SELFPHP


Professional CronJob-Service

Suche



CronJob-Service    
bei SELFPHP mit ...



 + minütlichen Aufrufen
 + eigenem Crontab Eintrag
 + unbegrenzten CronJobs
 + Statistiken
 + Beispielaufrufen
 + Control-Bereich

Führen Sie mit den CronJobs von SELFPHP zeitgesteuert Programme auf Ihrem Server aus. Weitere Infos



:: Buchempfehlung ::

Das Zend Framework

Das Zend Framework zur Buchempfehlung
 

:: Anbieterverzeichnis ::

Globale Branchen

Informieren Sie sich über ausgewählte Unternehmen im Anbieterverzeichnis von SELFPHP  

 

:: Newsletter ::

Abonnieren Sie hier den kostenlosen SELFPHP Newsletter!

Vorname: 
Name:
E-Mail:
 
 

Zurück   PHP Forum > SELFPHP > PHP Entwicklung und Softwaredesign
Hilfe Community Kalender Heutige Beiträge Suchen

PHP Entwicklung und Softwaredesign Hier können strukturelle (Programmier-) Konzepte diskutiert und Projekte vorgestellt werden

 
 
Themen-Optionen Ansicht
  #1  
Alt 01.06.2008, 23:53:51
Benutzerbild von Socrates
Socrates Socrates ist offline
Member
 
Registriert seit: Jul 2006
Ort: Göttingen/Deutschland
Alter: 35
Beiträge: 586
Configuration Handling

Abend!
Ich bin gerade dabei eine Klasse zu schreiben, die mit Konfigurations-Dateien arbeiten soll. Kurz gesagt sie soll sie lesen und schreiben. Die Ausgabe funktioniert hervorragend, das Problem liegt also beim Schreiben. Da die Klasse natürlich nicht weiß, welche Felder in der Konfigurations-Datei existieren, muss vor jedem Eintrag geprüft werden, ob das Feld existiert. Ist das der Fall soll das Value für dieses Feld geändert werden. Meine Frage ist jetzt, ob es noch unsicher ist, einen Aufruf der Klasse so zu gestalten, oder ob genug Sicherheit durch die Verarbeitung der Daten inerhalb der Methode setConfig gegeben ist.
Wäre euch echt dankbar, wenn ihr da mal euren Senf zu geben würdet. :-)
MfG, Andy

Der Aufruf
PHP-Code:
<?php 
   
// call Config-Class
   
require_once('includes/Config.php');
   
   
// set reply to an array
   
$config $_GET[];
   
   
// set new configuration
   
$ih = new Config();
   
$ih->setConfig($config'config/config.ini');
   unset(
$config);

   
// print out config
   
$config getConfig('config/config.ini');
   
print_r($config);
?>
Config.php
PHP-Code:
<pre>
<?php  
error_reporting
(E_ALL);

/***
* The Config class allows reading the configuration and editting it
*  
* @package Config
* @version 1.0
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/  
class Config
{
    
/**
    * getConfig() - Gets the config-file and stores it in an array
    *
    * @access: public
    * @param Str $path
    * @return Array
    */
    
public function getConfig($path)
    {
        
// array that contains theconfig
        
$config = array();
        
        
// check if file exists and create an absolute path
        
if (file_exists($path)) 
        {
            
$path realpath($path);
        } 
    
        else 
        {
            throw new 
Exception('Path not found!');
        }
        
        
// save each line of data to an array-element
        
$lines file($path);
                        
        foreach(
$lines as $line => $value)
        {
            
// save information to array
            
if( preg_match('/([^:]+)=(.+)/m'$value$match) )
            {
                
$config[$this->getName($match[1])] = trim($match[2]);
            }
        }
        
        return 
$config;
    }
    
    
/**
    * getName() - Returns the name of a received field
    *
    * @access: private
    * @param Str $str
    * @return String
    */
    
private function getName($str)
    {
        return 
preg_replace('/(?<=^|[\x09\x20\x2D])./e''strtolower("\0")'strtolower(trim($str)));
    }  
    
    
/**
    * setConfig() - Writes the data from config-array into the configurtaion-file
    *
    * @access: private
    * @param Arr $config
    * @param Str $path
    * @return String
    */
    
private function setConfig($config$path)
    {        
        
// read configuration file
        
$file file_get_contents($path);
        
        
// find the config-field and insert new value
        
foreach( $config as $name => $value)
        {    
            
// clean strings from php- and html-code
            
$name trim(strip_tags($name));
            
$value trim(strip_tags($value));
            
            
// search if data set entry exists and assign new value
            
preg_replace("/($name)=(.+)/m""$name = $value"$file);
        }
                
        
// write the new configuration to config-file
        
$fp fopen($path'w');
        
fwrite($fp$file);
        
fclose($fp);        
        
        return 
true;
    }  
}
    
?>
</pre>
config.ini
Code:
# database values
dbhost = your_database_host
db = your_db
dbuser = testuser
dbpwd = testpwd

# meta tags
title = title of your website
author = the author of the website
copyright = copyright by
discription = this is a great website
keywords = website, your, nice
favicon = favicon.ico

# error pages
error400 = Error 400 - Bad request.
error404 = Error 404 - Page could not be found.
error500 = Error 500 - Error occured.
__________________
BSc. Applied Computer Science
http://www.bornageek.com

Geändert von Socrates (01.06.2008 um 23:55:01 Uhr)
Mit Zitat antworten
 


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind aus.
[IMG] Code ist aus.
HTML-Code ist aus.

Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Kein PHP auf virtual Hosts ToB Apache HTTP-Server 2 14.08.2007 03:33:15
error handling bei unknown function tharem PHP für Fortgeschrittene und Experten 3 16.11.2004 17:18:03
Session Handling jensn PHP für Fortgeschrittene und Experten 12 20.12.2003 16:27:10
Exception Handling ?! Phil PHP für Fortgeschrittene und Experten 3 10.12.2003 21:13:43
Link handling Loki PHP für Fortgeschrittene und Experten 3 22.10.2002 10:38:49


Alle Zeitangaben in WEZ +2. Es ist jetzt 08:15:28 Uhr.


Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.


© 2001-2024 E-Mail SELFPHP OHG, info@selfphp.deImpressumKontakt