Reputation: 917
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.
Upvotes: 1
Views: 402
Reputation: 80
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();
.
Upvotes: 0
Reputation: 14269
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';
Upvotes: 0
Reputation: 17091
<?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',
)
Upvotes: 2