Preg_replace question

Discussion in 'Coding Help' started by ziycon, Oct 30, 2009.

  1. ziycon

    ziycon New Member

    This is the file its modifying but there will be more functions added down the road.
    <?php

    $DBHost = '';
    $DBName = '';
    $DBUser = '';
    $DBPass = '';
    $DBPrefix = '';

    function getDBHost() {
    global $DBHost;
    return $DBHost;
    }
    function getDBName() {
    global $DBName;
    return $DBName;
    }
    function getDBUser() {
    global $DBUser;
    return $DBUser;
    }
    function getDBPass() {
    global $DBPass;
    return $DBPass;
    }
    function getDBPrefix() {
    global $DBPrefix;
    return $DBPrefix;
    }
    ?>
     
  2. louie

    louie New Member

    Because the file is PHP, is there a chance the server will process the file on request?
    What about trying the save the file as .txt and give it another go?
     
  3. ziycon

    ziycon New Member

    I would prefer to keep it as a PHP if possible, if this isn't going to work, is there any other ways I could go about it?
     
  4. louie

    louie New Member

    Yes you can,so lets see what I can come up with this minute.
    Change your php file with the following code:

    $DBHost = '[dbhost]';
    $DBName = '[dbname]';
    $DBUser = '[dbuser]';
    $DBPass = '[dbpass]';
    $DBPrefix = '[dbprefix]';

    function getDBHost() {
    global $DBHost;
    return $DBHost;
    }
    function getDBName() {
    global $DBName;
    return $DBName;
    }
    function getDBUser() {
    global $DBUser;
    return $DBUser;
    }
    function getDBPass() {
    global $DBPass;
    return $DBPass;
    }
    function getDBPrefix() {
    global $DBPrefix;
    return $DBPrefix;
    }

    :::::::::::::::::::::::::::::::::
    Now forget about preg_replace and try str_replace

    function setDBDetails($DBHost,$DBName,$DBUser,$DBPass,$DBPrefix) {
    global $configFileLoc;
    $fhandle = fopen($configFileLoc,"r");
    $content = fread($fhandle,filesize($configFileLoc));
    //echo $content;
    //echo "<hr />";
    $find = array("[dbhost]","[dbname]","dbuser]","[dbpass]","[dbprefix]");
    replace = array("DBHOST","DBNAME","DBUSER","DBPASS","DBPREFIX");
    $content = str_replace($find, $replace, $content);
    //echo $content;
    //echo "<hr />";
    $fhandle = fopen($configFileLoc,"w");
    fwrite($fhandle,$content);
    fclose($fhandle);
    //echo $content;
    }

    But your file must have read & write permission setup on the server which is NOT SAFE
     
  5. ziycon

    ziycon New Member

    This works fine, thanks louie. What would you recommend for a secure way of storing and being able to retrieve the details via PHP?
     
  6. louie

    louie New Member

    It's alright the way it is if only done once at the setup time.
    Once done, check and make sure the permission to write to the file is changed.
    Something like Wordpress does it.
    If that's not the case I highly not recommended.
     

Share This Page