skip to Main Content

I have my database information file called main-custom.php file which have information in array like this

<?php defined('MW_PATH') || exit('No direct script access allowed');

return array(

    // application components
    'components' => array(
        'db' => array(
            'connectionString'  => 'mysql:host=localhost;port=3306;dbname=databasebane',
            'username'          => 'username',
            'password'          => 'password',
            'tablePrefix'       => 'mw_',
        ),
    ),
    // params
    'params' => array(
        'email.custom.header.prefix' => 'X-Ltxf-'
    ),
);

Its in Mailwizz.I am looking for get and use this database information in my another file so I have included it in my index.php file like below

<?php
define("MW_PATH","OK");
require_once("../apps/common/config/main-custom.php");

Now I am not getting idea how I can use this database information for create variable like

$connectionString =?
$username =?
.....

Since main-custom.php file returning array, its making me confused and not able to get information from it to my index.php file.

since mailwizz is using main-custom.php as it so I can not modify it. Is there any way to extract required information from it to index.php file without modify it?

Thanks!

4

Answers


  1. Chosen as BEST ANSWER

    I have achieved it using file_get_contents like below

    $content = str_replace(' ', '', file_get_contents("../apps/common/config/main-custom.php"));
    
    $db_host ="localhost";
    $db_port ="3306";
    $db_name = get_string_between($content, 'dbname=', "',");
    $username = get_string_between($content, "'username'=>'", "',");
    $password = get_string_between($content, "'password'=>'", "',");
    $tablePrefix = get_string_between($content, "'tablePrefix'=>'", "',");
    
    function get_string_between($string, $start, $end){
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
    

    Its working fine as expected. Thanks!


  2. You have two options:

    • modify the main-custom.php to not return an array and to assign that array to a variable $app = [‘components’ => [..], ‘params’ => [..]] and in the index.php as you write it with require_once you can access the variable $app.

    • assign $app = ("../apps/common/config/main-custom.php")

    Login or Signup to reply.
  3. you got bunch of problems here:

    first of all you can’t just return something in the middle of the file, you need to put it inside a variable to be able to access it, whether its in the same file or you include it

    <?php defined('MW_PATH') || exit('No direct script access allowed');
    
        $config= array(
        
            // application components
            'components' => array(
                'db' => array(
                    'connectionString'  => 'mysql:host=localhost;port=3306;dbname=databasebane',
                    'username'          => 'username',
                    'password'          => 'password',
                    'tablePrefix'       => 'mw_',
                ),
            ),
            // params
            'params' => array(
                'email.custom.header.prefix' => 'X-Ltxf-'
            ),
        );
    

    secondly, you can dump what’s inside your array using var_dump() function:

    <?php define("MW_PATH", "OK");
    require_once("./test.php");
    var_dump($config);
    ?>
    

    and finally I hope you are aware this is not a database connection, you are just putting database connection string info in a array and passing it to another file, a better way would be to connect to database and pass the connection.

    Login or Signup to reply.
  4. There are two considerations here.

    Firstly, the constant MW_PATH acts to prevent you from executing the rest of the content, so we need to define that.

    Secondly, the actual code is just a return statement. That only makes sense inside a function or class method, so we need to mock up that environment.

    This gives us:

    // define the constant if its not already defined.
    if (!defined('MW_PATH')) define('MW_PATH', 'OK');
    
    // create the function and include the file in it.
    function getDBdata() {
    require("main-custom.php");
    }
    
    // call the function to get the data
    $dbData = getDBdata();
    
    $connectionString = $dbData['components']['db']['connectionString']  // mysql:host=localhost...
    
    Note that defining MW_PATH to an arbitrary value may cause problems later in the code if it is redefined, or has an incorrect value.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search