I have a PHP script with this content:
$request_data='teststring';
var_dump($request_data);
$bd = base64_decode($request_data);
var_dump($bd);
die()
When I load the page, it outputs
string(10) "teststring" string(7) "��-���"
But, if I pass a GET string to the page - any string at all - it outputs this instead:
string(10) "teststring" string(7) "µë-²Úâž"
What is going on? How can passing a GET string to the page change the functionality of base64_decode? If I set $request_data to valid base64 encoded data, then the GET argument does nothing, and it decodes it correctly. But when it's an invalid string, it decodes it differently depending on the argument. Why is this? How can I fix it?
I am trying to pass in base64 encoded encrypted data, which sometimes decodes to non-ascii characters, and am finding that when that happens I get the wrong values back. I need a way to safely base64_decode any data, whether or not it is an alphanumeric string.
Related
i created a simple modifier to decode a string that is encoded in json.
function smarty_modifier_json_decode($string)
{
return json_decode($string, true);
}
Now i have somewhere into my database a string like this:
[{"foo": "bar bar", "cool": "attr"},{"foo": "bar", "cool": "attr"},{"foo": "bar", "cool": "attr"}]
The problem is the following if i store the above string locally inside the php file in a string variable and after i run the json_decode i get back the array i should get back and works fine.
But for some reason that i dont know and idont udnertand the string that is stored into the database is probably not correctly saved in the correct character encoding, because i always get back NULL when i try to execute the fucntion on smarty:
{$data.custom6|json_decode|var_dump}
So my question is if i have this string, but i dont know how my software save it into the database what should i do to convert it into the correct encodinh and after process it with json_decode ?
Thanks
NOTE 1: i dont know how the backend is saving data into the database and i cnnot influence this step. i can only using a web interface insert my JSON string in the custom field and press a SAVE button –
NOTE 2: sorry for the mistake copying the file here, i placed $json instead $string. But it doesnt work.
I have this php file resizeImage.php which can be called like this -
http://<domain>/fam/resizeImage.php?&srcImg=<url encoded URL of a remote image>&width=<width>&height=<height>
However, a different module calls the htmlentities encoded version of this URL, in this way -
htmlentities(http://<domain>/fam/resizeImage.php?srcImg=<url encoded url>&width=<width>&height=<height>)
So, following is a sample URL that is called -
http://<domain>/fam/resizeImage.php?srcImg=https%3A%2F%2Flh3.googleusercontent.com%2FVRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0%3Dh256-rw&width=640&height=960
Now, the request is received by resizeImage.php, but I am unable to get the parameter width using $_REQUEST['width'], but I can do the following -
get htmlentities_decode($_SERVER['REQUEST_URI'])
explode it using & to get the parameter-value pairs.
explode using = to get values against parameters.
So, two things -
I was wondering if this is the proper way to extract the parameters in this scenario.
I do not know the reason why the calling module calls the htmlentities encoded URL. Could there be a better way to suggest them?
You can use PHP's internal functions to parse and decode URLs:
parse_url - parse URL and get the needed components
html_entity_decode - decode html entities
url_decode - decode URL encoded characters
and finally parse_str - to parse parameter string into an associative array
So here's an example code what I'm come up with (you can try it out here):
$parsed = parse_url($url);
parse_str(urldecode(html_entity_decode($parsed['query'], ENT_HTML401)), $tmp);
var_dump($tmp);
...which renders your URL parameters into an associative array:
array(3) {
["srcImg"]=>
string(109) "https://lh3.googleusercontent.com/VRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0=h256-rw"
["width"]=>
string(3) "640"
["height"]=>
string(3) "960"
}
As for the second part, I think the second module's approach is a little bit safer, since you're placing an URL in an URL's parameter. If you don't want to hassle with parsing and stripping unnessesary parts from the parameter, then encoding the whole is a simple and safe way to keep your URL's out of syntax errors.
In some cases, when people send html code in get parameters htmlentities on single parameters might be ok when it comes to the label, but not for the link itself - they should use urlencode for that:
<a href="htpp://yourdomain.tld/?param1=<?php echo urlencode('<somehtmltag>'); ?>>htpp://yourdomain.tld/?param1=<?php echo htmlentities('<somehtmltag>'); ?></a>
The site I am working on is using a .net Soap web service for getting data. The initial call returns XML that contains a base64 encoded string. I am able to isolate that string by using $lastResponse = $client->__getLastResponse(); When I use var_dump on the $last response variable I get some thing like
string(10139) "77u/PD94bWwgdmV...=="
When I use echo I get 77u/PD94bWwgdmV...== without the string(10139) to start. I have tried to place the $lastResponse variable into the base64_decode function but it returns nothing at all, not even NULL. I have also tried to split the string first to remove the 77u/ from the start and that does not work at all.
$lastResponse = $client->__getLastResponse();
$splitResponse = preg_split("#/#", $lastResponse);
echo base64_decode($lastResponse);
echo base64_decode($splitResponse[1]);
var_dump($splitResponse[1]);
var_dump(base64_decode($splitResponse[1]));
echo $lastResponse;
The code above returns this to the browser:
string(0) "" string(0) "" 77u/PD94bWwgdmVyc2lvbj0....
But when I copy/paste everything after the 77u/ into an only decoder I get the decoded xml that I am supposed to have returned to me. I am very confused as to what I am missing here any help will be greatly appreciated.
I'm very new to PHP but have a good understanding of C,
When I want to access some post data on an API i'm creating in PHP I use:
$_POST['date_set']
to fetch a value being passed for date - This all works perfectly, however I read I should be fetching it like this:
$date_set = trim(urldecode($_POST['date_set']));
This always returns a 00:00:00 value for the date after it's stored in my DB.
When I access directly using $_POST['date_set'] I get whatever value was posted, for example: 2013-08-28 10:31:03
Can someone tell me what I'm messing up?
You should try it like,
$date_set = $_POST['date_set'].explode(' ');//('2013-08-28 10:31:03').explode(' ')
echo $date_set[1];
or
echo date('H:i:s',strtotime($_POST['date_set'])));
//echo date('H:i:s',strtotime('2013-08-28 10:31:03'));
If you are very new in php the Read date()
You only run urldecode over data is URL encoded. PHP will have decoded it before populating $_POST, so you certainly shouldn't be using that. (You might have to if you are dealing with double-encoded data, but the right solution there should be to not double encode the data).
trim removes leading and trailing white-space. It is useful if you have a free form input in which rogue spaces might be typed. You will need to do further sanity checking afterwards.
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
urldecode: is used only for GET requests. you should be fine using $_POST['date_set'] only.
http://php.net/manual/en/function.urldecode.php
You'd better do this way
if(isset($_POST['date_set'])){
$date_set = $_POST['date_set'];
}
then you can use $date_set how you want.
If you still get 00:00:00 for $date_set, the problem is coming from the code which provide you the $_POST value.
I am building a request in Flash that contains a JSON encoded Array of Objects. This is encoded using as3corelib. The request is then passed to JavaScript via ExternalInterface and a jquery ajax call sends the request off to the server.
In PHP, the incoming data is parsed, but the json_decode returns null, giving a Malformed JSON error. var_dump results in comments:
<?php
(isset($_POST['gdata']) && !empty($_POST['gdata'])) ? $gamedata = $_POST['gdata'] : returnError("game data not specified");
var_dump($gamedata); // (String) = string(37) "[{\"duration\":1,\"id\":\"game2\"}]"
$gamedata = json_decode(utf8_encode(trim($gamedata)),true);
var_dump($gamedata); // null
$gamedata = json_decode("[{\"duration\":1,\"id\":\"game2\"}]",true);
var_dump($gamedata);
/*
array(1) {
[0]=>
object(stdClass)#1 (2) {
["duration"]=>
int(1)
["id"]=>
string(7) "game2"
}
}
*/
?>
What I don't understand is that attempting to decode the variable returns null, but the same text decoded from a literal string works fine. What can I do to clean up the incoming data and make it readable for json_decode?
Edit: php_info() says that magic_quotes_gpc is enabled. Could that be the issue?
magic_quotes_gpc could be the issue, yes. And if you re-encode blindly w/o knowing the charset could be an issue as well.
So if you know magic_quotes_gpc is enabled, you need to strip slashes first.
For the charset, take care you know in which charset the incomming data is encoded, not that it's already utf-8 encoded and you assume it's latin-1 and convert it again.