read and parse cookie - php

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'

Related

change variable value in a file in php

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)).

Is it possible rewrite php file using fwrite without removing php comments?

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);
?>

Read classic ASP's cookies with PHP

How do I obtain the asp cookie's name and value using PHP so i may assign it to a variable? PHP and ASP are on the same domain.
Classic Asp Create Cookie
response.cookies("apple")("red")="reddelicious"
response.cookies("apple")("yellow")="gingergold"
response.cookies("apple")("green")="grannysmith"
response.cookies("apple").expires = dateadd("d",2,Now())
Classic ASP Read Cookie
strRed = request.cookies("apple")("red")
strYellow = request.cookies("apple")("yellow")
strGreen = request.cookies("apple")("green")
Reading The ASP cookies with PHP echo
echo $_COOKIE["apple"];
In firebug, after expanding the 'apple' cookie within the console, 'echo $_COOKIE["apple"]' outputs:
red=reddelicious&yellow=gingergold&green=grannysmith
Tried:
$strRed = $_COOKIE["apple"]["red"]; //doesn't work
You could use the parse_str function in php
<?php
parse_str($_COOKIE["apple"], $apple);
echo($apple["red"]);
?>
To get the string red=reddelicious&yellow=gingergold&green=grannysmith to the format of a multi dimensional array try this:
$itemArray = explode('&', $_COOKIE['apple']); // Split variables at '&'
foreach($itemArray as $item){ // for each variable pair 'key=value'
$itemParts = explode('=', $item); // split string to '[key, value]'
$apple[$itemParts[0]] = $itemParts[1]; // set each item to $apple[key] = value;
}
Then you can use the variable like this:
$strRed = $apple["red"]; //should work :)

Sending arrays through PHP header redirects

Basically I need some help sending array information through redirects in PHP. I have the following code:
Page1: I created the query string and sent it to page 2, I can get the data in $_GET on page 2.
$qstr = http_build_query(
array(
'products_array' => $products,
'quantity' => $_POST['quantity']
)
);
header('Location: registration.php?' . $qstr);
Page 2/3 I get the query string from $_server and concatenate it with the redirect page
header('Location: login.php?' . $_SERVER['QUERY_STRING']);
I can send the data using normal href links, but if I try to send it like above I can't retrieve the data past page 2.
If you could give me some input on this problem it would really help.
Thanks.
Edit:
echoing $qtystr gives:
products_array%5B0%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F61Y-rF9tF8L.SL1100.jpg&products_array%5B0%5D%5Bbrand%5D=Charmander&products_array%5B0%5D%5Bprice%5D=25&products_array%5B1%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F61vgC3GDI2L.SL1100.jpg&products_array%5B1%5D%5Bbrand%5D=Squirtle&products_array%5B1%5D%5Bprice%5D=15&products_array%5B2%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F51TnHKT4oML.SY300.jpg&products_array%5B2%5D%5Bbrand%5D=Bulbasaur&products_array%5B2%5D%5Bprice%5D=10&products_array%5B3%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F41MpzoPshAL.SX300.jpg&products_array%5B3%5D%5Bbrand%5D=Chikorita&products_array%5B3%5D%5Bprice%5D=20&products_array%5B4%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F51BIJR%252BIqDL.SX355.jpg&products_array%5B4%5D%5Bbrand%5D=Mudkip&products_array%5B4%5D%5Bprice%5D=20&quantity%5B0%5D=0&quantity%5B1%5D=0&quantity%5B2%5D=0&quantity%5B3%5D=03&quantity%5B4%5D=0
which is a stupid amount of information to pass in a string (just testing stuff out), sorry, but it's all the information from the two arrays I needed.
Edit2:
I managed to fix it, I didn't give you guys enough info sorry. The problem was actually that I did not set the form action of my pages to preserve the data string, so it kept getting lost as I was processing the form. Thanks for all the advice, I'll be sure to try it out next time, especially sessions.
Using only PHP
To encode the data
$products = "some product";
$qtd = 1;
$array = array('products_array' => $products, 'quantity' => $qtd);
$json_str = json_encode($array);
echo $json_str;
header('Location: registration.php?' . $qstr);
To decode the data
$data = json_decode($post_data); //to decode the string into an object
echo $data->products_array . "\r\n";
echo $data->quantity . "\r\n";

Send Data from Drupal to node.js

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.

Categories