I am building an installer script in my application. I am declaring some constants in one file for example
define(DB, '');
define(USER, '');
define(PASS, '');
define(HOST, '');
Now I want to access this DB variable first and update the value of DB and so on.
I have tried so far is this
define(DB, '%dbname');
define(USER, '%dbuser');
define(PASS, '%dbpass');
define(HOST, '%dbhost');
$file_path = "constant.php";
$dbname = $_POST['dbname'];
$value = "%dbname";
$replace_with = $value;
$content = file_get_contents($file_path);
$content_chunks = explode($string_to_replace, $content);
$content = implode($replace_with, $content_chunks);
file_put_contents($file_path, $content);
This script works fine. But the only issue is that once its update the value it can't update the value again. The value cannot be updated twice because the script was checking this value %dbname. Now since it has been changed to some value. I can't be able to update the value again. I want to do something like detect the variable DB then update the value in front of it.
Related
I have two variables:
$getSkill = $_GET['skill'];
$avg = avg;
I have an 'averages.php' file included that has all of the necessary averages preloaded but this current PHP file is defined by $_GET['skill'], each average in there is loaded as $skillnameAvg and I am trying to echo the relevant $skillnameAvg to each dynamic page correctly.
I've tried $getSkillAvg = $.$getSkill.$avg; and a few others and I can't seem to find a solution.
This can be done using Variables variables in PHP
$getSkill = $_GET['skill'];
$avg = $getSkill . 'avg';
echo $$avg;
Example
$chippyavg = '20%'; // create a testing value eg your included file
$getSkill = 'chippy'
$avg = $getSkill . 'avg';
echo $$avg;
Result
20%
This is not a big deal, but for you I can share some information. First of all you need to concatenate two variable $_GET['skill'] and $avg, and make them also another variable by using a $sign prefix of the resultant value.
Now use echo $getSkillAvg = ${$getSkill.$avg};, what it shows? If you don't define the variable that is generated here then E_NOTICE : type 8 -- Undefined variable: skillavg -- at line 8. Or if defined then the value will be display.
Now what you are trying to do make that value a variable, so do that you need to use another $ sign before the resultant value. Then the result says undefined variable, so you need to make that variable as string to show as output as i do in below code.
$skillavg = 'Smith'; //assignment for work
$_GET['skill'] = 'skill';//assignment for work
$getSkill = $_GET['skill'];
$avg = "avg";
echo $getSkillAvg = "$${$getSkill.$avg}"; //$Smith
I am writing some code to create fields automatically, which will save me a load of time. I have got most of my code working, but I have came across one error with the code, which is preventing me from achieving my final goal.
The code is as follows:
while ($i <= $numFields) {
$type = "\$field{$i}_Data['type']";
$name = "\$field{$i}_Data['name']";
$placeholder = "\$field{$i}_Data['placeholder']";
$value = "\$field{$i}_Data['value']";
echo '<input type="'.$type.'" name="'.$name.'" placeholder="'.$placeholder.'" value="'.$value.'">';
$i++;
}
The $numFields variable is defined at the top of my script, and I have worked out that it is something to do with how I am setting the variables $type, $name etc.
The end result is to create inputs depending on properties set in variables at the top of the script, The only issue I am having is with the settings of the variables, as said above.
If any extra code/information is needed, feel free to ask.
Thank you.
NOTE - There is no physical PHP error, it's purely an error with this:
"\$field{$i}_Data['value']";
There are a few ways we could write this one out, but they are all extensions of variable expansion and/or variable-variables.
Basically, we just need to put the variable name in a string and then use that string as the variable (much like you're currently doing with $i inside the string):
$type = ${"field{$i}_Data"}['type'];
$name = ${"field{$i}_Data"}['name'];
// ...
However, if you don't mind an extra variable, this can be written more cleanly by saving it like so:
$data = ${"field{$i}_Data"};
$type = $data['type'];
$name = $data['name'];
// ...
I'm trying to edit a config file using a html form. The edit (settings.php) file looks like this:
$config['foo'] = FALSE;
$config['maintenance'] = FALSE; //this line is that what it matters
$config['bar'] = FALSE;
The idea here is change the of $config['maintenance'], so once the form is submitted (there is a checkbox named maintenance in order to set the status to true or false according to its state), I get the checkbox value as:
$status = ($_POST['maintenance'] === 'on')? "TRUE" : "FALSE";
I have debugged $status var value and everything goes fine to here. Now, I am using the regex below to find the correct line at file:
\$config\[(\s+)?(\'|")maintenance(\'|")(\s+)?\](\s+)?=(\s+)?(false|FALSE|true|TRUE);/
Initially "works" good, because I am not sure, but let me finish the explanation...
According with the code above, now I proceed to do the replacement:
//read the content and replace it
$content = preg_replace(
'/\$config\[(\s+)?(\'|")maintenance(\'|")(\s+)?\](\s+)?=(\s+)?(false|FALSE|true|TRUE);/',
'$config["maintenance"] = ' . $status . ';',
file_get_contents($file)
);
//set the new content
file_put_contents($file, $content);
When I run it the first time with the checkbox checked it works and the result is as follow:
$config['foo'] = FALSE;
$config["maintenance"] = TRUE;
$config['bar'] = FALSE;
However, no matter what I select in the checkbox, the file does not show any changes. Can you guide me to the right direction to find the bug? Thank you
Edit.
This is the html markup
<label>
<input type="checkbox" name="maintenance" /> in maintenance mode
</label>
Try this:
$status = (isset($_POST['maintenance'])) ? 'TRUE' : 'FALSE';
and:
$content = preg_replace(
'/\$config\[\s*[\'"]maintenance[\'"]\s*\]\s*=\s*(false|true);/i',
'$config["maintenance"] = ' . $status . ';',
file_get_contents($file)
);
However the code you posted works fine for me, you should do more debugging like:
error_reporting(-1);
or checking $content before and after replace. Check your error logs (or search for error message if you have display_errors set to on). There can be anything wrong. (e.g. file permissions).
Also consider:
full rewriting of config file instead of just replacing one line - it might be prone to errors.
acquiring locks while writing/read to/from the file
I have the 'path' to an array value stored in a variable, and I am trying to set the value. What I am trying to do is this:
$array['Breaks'][1]['In'] = "XXX";
However, I have ['Breaks'][1]['In'] stored in a variable. So I am essentially trying to do something like this:
$path = "['Breaks'][1]['In']";
$array.$path = "XXX";
This doesn't work though, and I'm not exactly sure how to go about making this work correctly.
Any suggestions?
As long as the path string is not modifiable by users or parsed through previously you could just do:
eval("\$array".$path." = 'Value';");
Try doing this,
//This solution works if you are sure the length of $path_arr is going to be 3
$path = "['Breaks'][1]['In']";
$path_arr = explode(']', str_replace(array("['", "'", "["), '', $path));
$array[$path_arr[0]][$path_arr[1]][$path_arr[2]] = "XXX";
var_dump($array);
Demo
Try extracting each component of the $path variable into its own variable, or into an array ($pathArray below). So, if you had $pathArray[0] set to 'Breaks', $pathArray[1] set to 1, etc., you could do something like:
$array[$pathArray[0]][$[pathArray[1]][$[pathArray[2]] = "XXX";
Although this seems like an unusual way to go about things, and it might be worth re-thinking your approach entirely.
Depending on the current page, I would like to change the css of the chosen menu module and make others different, etc. All that while building dynamically the page before loading.
My problem is that I have a serie of variables going by this structure :
$css_(NAME_OF_MODULE)
To know what variable must be set , I have another variable I received in parameters of this functions, called
$chosen_menu
Say $chosen_Menu = "C1", I would like to add content to $css_C1. Thus, what I want is to create a variable name out of 2 variables, named $css_C1
I have tried :
${$css_.$chosen_menu} = "value";
But it doesnt seem to work. Any clue ?
That probably won't just work. PHP supports full indirection though, so something like this will work.
$varName = $css."_".$chosen_menu;
$$varName = "value";
If not, it will probably be attempting to interpret $css_ as a variable name in your second code sample, so just change that to $css."_".$chosen_menu.
$nr = 1;
${'name' . $nr} = 20 ;
var_dump($name1);
Check out http://php.net/manual/en/language.variables.php
You should be able to use:
$menu = $css . '_' .$chosen_menu;
$$menu = 'some value';
or
${$menu} = 'some value';
I'm not sure if I get you right, but how about this:
${$css."_".$chosen_menu} = "value";