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();.
Related
I need help understanding the vague instructions on https://packagist.org/packages/orhanerday/open-ai
I downloaded the package from https://github.com/orhanerday/open-ai
I installed the package by running "composer require orhanerday/open-ai" in my Command Prompt
Instructions stop making sense from there.....
What does the "use Orhanerday\OpenAi\OpenAi;" code mean and where is it applied?
Am I to create a php file say index.php with content:
<?php
use Orhanerday\OpenAi\OpenAi;
$complete = $open_ai->complete([
'engine' => 'davinci',
'prompt' => 'Hello',
'temperature' => 0.9,
'max_tokens' => 150,
'frequency_penalty' => 0,
'presence_penalty' => 0.6,
]
?>
how and where do I add my api key? Do I create a file Orhanerday\OpenAi\OpenAi.php and enter my api key there?
i.e. OPENAI_API_KEY=sk-**********************************************
You should define the $open_ai variable as an OpenAI object by passing your private KEY value, like; new OpenAi('Your-OPENAI-KEY');
An Example;
<?php
use Orhanerday\OpenAi\OpenAi;
$open_ai = new OpenAi('OPEN-AI-KEY');// <- define the variable.
$complete = $open_ai->complete([
'engine' => 'davinci',
'prompt' => 'Hello',
'temperature' => 0.9,
'max_tokens' => 150,
'frequency_penalty' => 0,
'presence_penalty' => 0.6,
]);
I also add the Quick Start Part to orhanerday/OpenAI readme.
First you have you include thé 'autoload'file. 'use' doesn't mean that you have to create a file yourself.
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';
I am using the CSRFProtector library for PHP (https://github.com/mebjas/CSRF-Protector-PHP) and as soon as I turn it on all forms stop submitting and I get an error:
403 Access Forbidden by CSRFProtector!
My config file contains:
return array(
"CSRFP_TOKEN" => "ctkn",
"logDirectory" => "log",
"failedAuthAction" => array(
"GET" => 0,
"POST" => 0),
"errorRedirectionPage" => "",
"customErrorMessage" => "",
"jsPath" => "js/csrfprotector.js",
"jsUrl" => "ABS PATH TO js/csrfprotector.js",
"tokenLength" => 10,
"disabledJavascriptMessage" => "",
"verifyGetFor" => array()
);
and the log file for CSRF shows:
{"timestamp":1433842328,"HOST":"MYDOMAIN.COM","REQUEST_URI":"\/add","requestType":"POST",
"query":{"title":"234","date":"08\/06\/2015","submit":""},
"cookie":{"ctkn":"3c8c7dfebc","__insp_uid":"1173752613",
"PHPSESSID":"SESSION_ID","_ok":"3274-991-10-2674","__utma":"98457856.2008088440.1420840286.1431630284.1432025914.10",
"__utmc":"98457856","__utmz":"98457856.1429793890.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)","__insp_wid":"801086210",
"__insp_nv":"false","__insp_ref":"aHR0cHM6Ly93d3cubmF0dXJhbGhyLm5ldC9sb2dpbg==",
"__insp_identity":"33101","__insp_norec_sess":"true","__insp_slim":"1433237660812",
"mp_3a3715a3d0ca553aea5e93832d489346_mixpanel":"{\"distinct_id\": \"14adaecc111146-0576c4449-63161675-232800-14adaecc1122e6\",
\"$initial_referrer\": \"$direct\",\"$initial_referring_domain\": \"$direct\"}",
"_okbk":"cd5=available,cd4=true,vi5=0,vi4=1433789710017,vi3=active,vi2=false,vi1=false,
cd8=chat,cd6=0,cd3=false,cd2=0,cd1=0,","_gat":"1","_ga":"GA1.2.2008088440.1420840286",
"_oklv":"1433842325615,1q2TBnOvCYwsP8Vp452Bb0OMFOHE0QN6","olfsk":"olfsk31493824627250433",
"_okac":"96642aff6954f6ce73ba12dfbb632117","_okla":"1","wcsid":"1q2TBnOvCYwsP8Vp452Bb0OMFOHE0QN6",
"hblid":"BpAXj2n915tZ7ULn452Bb0OMFPI1k134"}}
Can anyone tell me why this is failing? What do I need to do to allow this genuine request but still stop any illicit ones?
For me, the problem was that I'm using a bootstrap confirmation modal to confirm before submitting the forum. When I checked the $_POST object, I found that it doesn't include the csrf_token.
You might have the same problem.
you have changes CSRFP_TOKEN in config file, so you need to change in js file too..
i.e.
CSRFP_TOKEN: 'ctkn'
I'm working on a drupal site and when debugging, I am always having to read through long, nested arrays. As a result, a large portion of my life is spent using the arrow, return, and tab keys, to split up 1000+ character strings into a nested, readable format.
For drupal devs, I can't use devel's dsm(), as I'm working with multi-step #ahah/#ajax forms, and I can only output the arrays to the error log, not to the screen.
Visual example:
Evil:
array ( 'form_wrapper' => array ( '#tree' => true, '#type' => 'fieldset', '#prefix' => '', '#suffix' => '', '#value' => '', 'name' => array ( '#type' => 'textfield', '#title' => NULL, '#size' => 60, '#maxlength' => 60, '#required' => false, '#description' => NULL, '#attributes' => array ( 'placeholder' => 'Email', ), '#post' => array ( 'form_wrapper' => array ( 'name' => '', 'pass' => '', ), ...
Good:
array (
'form_wrapper' => array (
'#tree' => true,
'#type' => 'fieldset',
'#prefix' => '<div>',
'#suffix' => '</div>',
'#value' => '',
'name' => array (
'#type' => 'textfield',
'#title' => NULL,
'#size' => 60,
'#maxlength' => 60,
'#required' => false,
'#description' => NULL,
'#attributes' => array (
'placeholder' => 'Email',
),
Edit: Sorry, by "not output to screen", I meant via drupal's system messages where it's possible to output arrays in a clickable, nested format (using devel.module).
If you need to log an error to Apache error log you can try this:
error_log( print_r($multidimensionalarray, TRUE) );
http://php.net/manual/en/function.print-r.php
This function can be used to format output,
$output = print_r($array,1);
$output is a string variable, it can be logged like every other string. In pure php you can use trigger_error
Ex. trigger_error($output);
http://php.net/manual/en/function.trigger-error.php
if you need to format it also in html, you can use <pre> tag
I just wonder why nobody uses or recommends the way I prefer to debug an array:
error_log(json_encode($array));
Next to my browser I tail my server log in the console eg.
tail -f /var/log/apache2/error.log
Though it's debatable if the output is human-readable, but it's still my preferred way to read it and would look something like that:
[Tue Dec 13] [...] AH01071: Got error 'PHP message: {"form_wrapper":{"#tree":true,
"#type":"fieldset","#prefix":"","#suffix":"","#value":"","name":{"#type":
"textfield","#title":null,"#size":60,"#maxlength":60,"#required":false,
"#description":null,"#attributes":{"placeholder":"Email"},"#post":{
`"form_wrapper":{"name":"","pass":""}}}}}', referer: http://localhost/
Simple stuff:
Using print_r, var_dump or var_export should do it pretty nicely if you look at the result in view-source mode not in HTML mode or as #Joel Larson said if you wrap everything in a <pre> tag.
print_r is best for readability but it doesn't print null/false values.
var_dump is best for checking types of values and lengths and null/false values.
var_export is simmilar to var_dump but it can be used to get the dumped string.
The format returned by any of these is indented correctly in the source code and var_export can be used for logging since it can be used to return the dumped string.
Advanced stuff:
Use the xdebug plug-in for PHP this prints var_dumps as HTML formatted strings not as raw dump format and also allows you to supply a custom function you want to use for formatting.
Drupal's Devel module has other useful functions including ones that can print formatted arrays and objects to log files. See the guide at http://ratatosk.net/drupal/tutorials/debugging-drupal.html
dd()
Logs any variable to a file named “drupal_debug.txt” in the
site’s temp directory. All output from this function is appended to
the log file, making it easy to see how the contents of a variable
change as you modify your code.
If you’re using Mac OS X you can use the Logging Console to monitor
the contents of the log file.
If you’re using a flavor of Linux you can use the command “tail -f
drupal_debug.txt” to watch the data being logged to the file.
This will help you
echo '<pre>';
$output = print_r($array,1);
echo '</pre>';
EDIT
using echo '<pre>'; is useless, but var_export($var); will do the thing which you are expecting.
Syntax
print_r(variable, return);
variable Required. Specifies the variable to return information about
return Optional. When set to true, this function will return the information (not print it). Default is false
Example
error_log( print_r(<array Variable>, TRUE) );
You should be able to use a var_dump() within a pre tag. Otherwise you could look into using a library like dump_r.php: https://github.com/leeoniya/dump_r.php
My solution is incorrect. OP was looking for a solution formatted with spaces to store in a log file.
A solution might be to use output buffering with var_dump, then str_replace() all the tabs with spaces to format it in the log file.
I am using libtidy with php. I am using xampp and it shows that tidy support is enabled. But when i use it in my code then it shows the following warning
repairString() [function.repairString]: Could not load configuration file tidy-file.php
i also tried using the object-oriented version but i again got the warning
tidy_repair_string() [function.tidy-repair-string]: Could not load configuration file tidy-file.php
i have kept the tidy code in a separate file called tidy-file.php. it looks like this
$options = array("output-xhtml" => true,"clean" => true, "drop-proprietary-attributes" => true,"drop-font-tags" => true,"drop-empty-paras" => true,"hide-comments" => true);
function getXHTML($html)
{
$xhtml=tidy_repair_string($html,$options);
return $xhtml;
}
what can be wrong
From what you've shown here, you're trying to use a variable that's defined outside the function. You need to pull it inside like so:
function getXHTML($html)
{
$options = array("output-xhtml" => true,"clean" => true, "drop-proprietary-attributes" => true,"drop-font-tags" => true,"drop-empty-paras" => true,"hide-comments" => true);
$xhtml=tidy_repair_string($html,$options);
return $xhtml;
}