I have a file which is a small place_config.php file.
Take this as an example where i am setting my variables
<?php
//config file
$place_config = array(
'credentials' => array(
'sid' => 'some_value',
'token' => 'some_token'
)
?>
I want to change the sid and token from the admin panel of the user for the ease. How can i effectively achieve this. One solution which i understand is to make the content of the file in a string with the variables of $_REQUEST placed after the post request write that whole string to the file? Is it a effective approach?
Submit a form with the proper inputs and when submitted call update_place_config():
function update_place_config() {
include('place_config.php');
$place_config['credentials']['sid'] = $_POST['sid'];
$place_config['credentials']['token'] = $_POST['token'];
$output = '<?php $place_config = ' . var_export($place_config, true) . '; ?>';
file_put_contents('place_config.php', $output);
return $place_config; //if you want to get the new config
}
Another option:
$content = file_get_contents('place_config.php');
$content = preg_replace("/('sid' =>) '[^']+'/", "$1 '{$_POST['sid']}'", $content);
file_put_contents('place_config.php', $content);
I personally would store in a database or use JSON if it needs to be a file.
Instead of storing the configuration data in a php file, I'll recommend storing them in a json file which can be easily read/edited through php.
Create a json file, let's say config.json. Then you can load the configuration using $conf = json_decode(file_get_contents("config.json")). You can make changes to the $conf object and save back the configurations as file_put_contents("config.json", json_encode($conf)).
Related
I can't read my json file inside codeigniter controller, it returns an error.
My code:
$curYear = date("Y");
$string = file_get_contents(base_url("assets/data/".$curYear."_cal.json"));
Error Message return by my controller trying to read json file using:
file_get_contents(//localhost/EBR_Labeling/assets/data/2018_cal.json): failed to open stream: No such file or directory
Solved
Using this syntax:
file_get_contents("./assets/data/".$curYear."_cal.json")
Please use this code --
<?php
$json = file_get_contents(FILE_PATH);
$obj = json_decode($json);
echo '<pre>' . print_r($obj) . '</pre>';
?>
And let me know If you need any other help.
Thanks
We can simply store all json in views folder and can access it directly as follow:-
<?php
$countries = $this->load->view('countries.json', '', true) //this will load countries.json
$countries = json_decode($countries);
?>
Points to be noted : -
$this->load->view('countries') is same as writing $this->load->view('countries.php')
And this loads the view in browser and doesn't store it in variable. When we want some another type of file to be loaded, then we can simply change the extension.
$this->load->view('countries', $data, true);
Passing third parameter as true allows us to store the view in variable. If we don't want to pass second parameter then we can pass empty string as $this->load->view('countries', '', true);
I'm able to retrieve the full URL like: http://www-click08-co-uk/wonga.php
and I need to retrieve the script name "wonga" from it.
The url will be changing depending on what page the user is on and I will always need the word or phrase after the / and not including the .php, in the example above I would like to create a variable with the value of this being wonga
This is the code I currently have, where "argos" is, is where the database is searched and responds with the information I need, this is where the vaiable would be used
<?php
//-----------------------------------------------------
// Include files and set Classes
//-----------------------------------------------------
require_once $_SERVER["DOCUMENT_ROOT"] . "/includes/common.php";
$db = new dbConnection();
$directorydata = new directorydata();
$phoneDirectory = new phoneDirectory();
$conn = $db->pdoConnect();
// Load the directorydata row via the row ID - 543 is "best buy"
//$directorydata->get($db, 543);
// Load the directorydata row via the url alias field
$directorydata->get($db, "Argos");
// Phone number isn't formatted coming out the DB
$formattedPhoneNumber = $phoneDirectory->formatPhoneNumber($directorydata->Number1);
?>
Just because you didn't provide any code, I provide you a way to solve this on your own:
$url = "http://www-click08-co-uk/wonga.php";
// SEARCH and replace
// FIRST_FUNCTION => google => php trailing name component of path
// SECOND_FUNCTION => google => php explode a string by string
// THIRD_FUNCTION => google => php pop first element of array
$urlParts = SECOND_FUNCTION( ".", FIRST_FUNCTION( $url ) );
echo THIRD_FUNCTION( $urlParts );
OUTPUT:
wonga
An example use of parse_url could be:
$url = 'http://www-click08-co-uk/wonga.php?page=74';
$route = parse_url($url, PHP_URL_PATH);
$routeTokens = explode('/', $route);
$scriptName = array_pop($routeTokens);
echo $scriptName;
which in this case outputs wonga.php.
Just note that this is a very rare task that you would have to take care of yourself. So instead of parse_url you might look at the bigger picture here and start looking for some good MVC framework.
I have some php file and it contains some comments.
<?php
$test = array(
'LBL_TEXT' => 'text',//test comment
'LBL_FOO' => 'foo'
);
Now I need to update 'LBL_TEXT' value(text) above file without removing comment('//test comment'). Is it possible using fwrite() or some other way.
So you will need something like
<?php
$data = file_get_contents("foo.php");
$data = so something clever to automatically change string as desired;
file_put_contents("foo.php",$data);
?>
I'm a newbie with drupal.
I was trying to send data from drupal to node.js function and save the data from node into mongo.
I wrote a function in php
function sample_working_submit($form, &$form_state) { //Function to connect with node and save into Mongo using node
$Name = check_plain($form_state['values']['Name']);
$Age = check_plain($form_state['values']['Age']);
$request_url = "http://10.20.5.112:3001/save/$Name/$Age ";
$response = drupal_http_request($request_url);
}
This is working as long as there is no 'space' between the names(input). How can save the input with spaces.Why does this issue came?
How can i rewrite the function as post?
<?php
$url = http://localhost:8080/myservlet;
$data = array (
'name' => check_plain($form_state['values']['Name']),
'age' => check_plain($form_state['values']['Age'])
);
$response = drupal_http_request($url, $headers, 'POST', json_encode($data));
?>
This would POST data to URL. Note that you need to change logic in server side as well to receive POST data instead of GET data.
Refer here for more info
If the space between the names is the issue, try using urlencode().
The code will be something like:
$Name = urlencode(check_plain($form_state['values']['Name']));
For POST requests I use SimpleBrowser instead of drupal_http_request(). It's easy to use and you'll be able to pass strings in any form.
Okey im pretty new to php/javascript but what i would need to do is read cookie and then parse some part of it. How can i do it easily? php or javascript or okey.
Cookie has something like this info: ssf.2313.1333 and I would need to get middle numrbers (2313).
You can do it using PHP easily.
$pieces = explode('.',$_COOKIE['yourcookiename']);
echo $pieces[1];
If you have the ability to create the cookie, I would suggest serializing an array with data. This way, you have far more control when reading the cookie.
Example:
$information = array(
'one' => 'data1',
'two' => 'data2'
);
$ck = setcookie('cookiename', serialize($information), time()+3600);
When fetching the cookie, use the PHP function unserialize, like so:
$ck = unserialize($_COOKIE['cookiename']);
echo $ck['one']; // returns 'data1'