I am currently generating config.php file that has an array w user details in it ,but I have a problem with getting the array back out to be used on the page, the config.php looks like this
Array
(
[DBLocation] => localhost
[DBName] => name
[DBUsername] => name
[DBPassword] => 123456
)
How can I use this array later?
Convert your config.php file like this:-
<?php
$arr = Array
(
'DBLocation' => 'localhost',
'DBName' => 'TAK14_Ostermann',
'DBUsername' => 'TAK14_Ostermann',
'DBPassword' => '123456'
);
?>
Now include this file into others with code:-include 'config.php' and use $arr variable.
Note:
Better to do a complete configuration code in the file (config.php) itself and create a database connection object. Now use that object by including the file. It will remove db-connection code redundancy in each page.
If the array you posted is the exact content of the config.php file you are not generating it in the right way.
The posted "array" is what print_r() outputs. As the documentation says:
print_r — Prints human-readable information about a variable.
The key here is "human-readable". The purpose of print_r() is to produce an output that is easy to read and understand by the programmer. It is a debug function, not meant to be used in the production code.
The function you need to generate the content of config.php is var_export(). It produces correct PHP code and it is specifically crafted for this purpose.
Assuming your configuration data is stored in the $config array, the code that generates config.php should be like this:
file_put_contents('config.php', '<?php return '.var_export($config, TRUE).";\n");
The generated config.php file will look like this:
<?php return array (
'DBLocation' => 'localhost',
'DBName' => 'name',
'DBUsername' => 'name',
'DBPassword' => '123456',
);
In order to load the configuration use include:
$config = include 'config.php';
Related
How can i parse ini file with .php extension. eg config.ini.php
below are content file as in it.
<?php
[Session]
SessionTimeout=1200
ActivityTimeout=600
CookieTimeout=0
SessionNameHandler=custom
Handler=SessionHandlerDB
?>
I tried parse_ini_file its not working.
here is the error i am getting Warning: syntax error, unexpected END_OF_LINE, expecting '='
I am using a framework in which I cannot remove PHP tag.
<?php
$configContent = file_get_contents('config.ini.php');
$iniContent = preg_replace('/<\?php|\?>/m', '', $configContent);
var_export(parse_ini_string($iniContent));
Result:
array (
'SessionTimeout' => '1200',
'ActivityTimeout' => '600',
'CookieTimeout' => '0',
'SessionNameHandler' => 'custom',
'Handler' => 'SessionHandlerDB',
)
If you cannot remove the PHP-tags, a better approach:
<?php
return [
'session' => [
'SessionTimeout' => 1200,
'ActivityTimeout' => 600,
'CookieTimeout' => 0,
'SessionNameHandler' => 'custom',
'Handler' => 'SessionHandlerDB',
]
];
Use:
$config = require 'config.inc.php';
I know this is more than 5 years old now, but I wanted to add my 2¢.
I don't know why everyone is giving the OP such a hard time about creating their ini file as PHP. It adds extra security so that even if a user knows the path to the file, it cannot be accessed.
To answer the OP's original question, I'm doing this in my own homebrewed CMS and it's working beautifully with parse_ini_file:
;<?php die();
/*
[Session]
SessionTimeout=1200
ActivityTimeout=600
CookieTimeout=0
SessionNameHandler=custom
Handler=SessionHandlerDB
*/
?>
Just block comment out the ini code and prepend the first line with a semi-colon. The ini sees the PHP as a comment and the PHP sees the ini as a comment, all while parse_ini_file can still read it. For even extra security, you can add the die();.
I've a PHP Configuration File in TYPO3 like
<?php
return array(
'DB' => array(
'database' => 'vicous',
'extTablesDefinitionScript' => 'extTables.php',
'host' => 'localhost',
'password' => '',
'socket' => '',
'username' => 'root',
)
);
?>
I want to get this array on an external php file. How to get that?
Something like this.
function getArray("filepath"){
$variable = filepath return that array
}
My requirement is to get array inside $variable
include and require behave exactly like you want:
$variable = include 'path/to/file.php';
From the docs:
It is possible to execute a return statement inside an included file
in order to terminate processing in that file and return to the script
which called it. Also, it's possible to return values from included
files. You can take the value of the include call as you would for a
normal function.
You Could use like
function getArray($path){
return $variable = require_once( $path );
}
I have a custom config file setup for twitter, which has some vars in but the data is saved in the database, so therefore i need to call something like:
$this->wc_settings->get()->row_array('consumer_key')
Here's the config file
$config['tweets'] = array(
'_tokens' => array(
'consumer_key' => $this->wc_settings->get()->row_array('consumer_key'),
//'consumer_secret' => '',
//'access_key' => '',
//'access_secret' => '',
),
This doesn't work but I need it to in order for the config file to work, but need to get the data from the database like shown above.
Help greatly appreciated.
You can dynamically set the config in your controller or model using:
$this->config->load('tweets');
$dynamic_config = $this->wc_settings->get()->row_array('consumer_key');
$this->config->set_item('_tokens['consumer_key']', $dynamic_config);
This will be simple for someone, and I have looked it up but don't understand why my syntax returns blank.
$this ->adminship_model->get_admininfo();
Returns an Array;
'username' => $this->adminship_model->get_admininfo(),
put in the session stores an array and when I print_r it, I can see the array of data.
[UserName] => SomeUserName
Is in the array
$variable = $this ->adminship_model->get_admininfo();
Sets the array to that variable
'username' => $this->$varaible->UserName,
returns blank
'username' => $this->$variable['UserName'],
returns blank
'username' => $variable['UserName'],
returns blank
What am I doing wrong please. Sorry if it is simple, I'll only need told once. :)
I'm a little confused by your syntax, but it looks like you should have
$variable = $this->adminship_model->get_admininfo(); // space removed
$user_name = $variable['UserName'];
echo $user_name;
That should echo SomeUserName
I got configuration file database.php
<?php defined('_ENGINE') or die('Access Denied'); return array (
'adapter' => 'mysqli',
'params' =>
array (
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'db',
'charset' => 'UTF8',
'adapterNamespace' => 'Zend_Db_Adapter',
),
'isDefaultTableAdapter' => true,
'tablePrefix' => 'engine4_',
'tableAdapterClass' => 'Engine_Db_Table',
); ?>
How to get only password from this array?
something like echo $array['password'];
How do I get the array from database.php?
You'll need to include the file and bind the returned value to a variable, such as in the below example.
$db_conf = require ('/path/to/database.php');
$db_conf will contain the data returned by database.php.
Documentation
PHP: include - Manual
How do I read the specific value from my array?
Since you are working with a nested array the solution is not as far away as you might think. First use $a[key] to get to the array stored under params, and then get the value of password from there.
As in the below example.
$password = $array['params']['password'];
Note: The above is, in a logical sense, equivalent to;
$params = $array['params'];
$password = $params['password'];
Documentation
PHP: Arrays - Manual
I tried the above but it's just shouting "Access Denied" in my face, why?
To protect database.php from unintended access it has been protected with a check to see so that it's being used inside of the engine.
The script will die if _ENGINE is not defined.
If you want to use database.php in a script outside of the thought of engine you'll need to define the _ENGINE constant before includeing the file.
define ('_ENGINE', 1);
...
$db_conf = include ('database.php');
echo $array['params']['password'];
Give this a try. :-)