Base 64 encode - twice - php

I need to base 64 encode parts of a URL for an S3 URL.
I'm left with something like:
http://d111111abcdef8.cloudfront.net/image.jpg?color=red&size=medium
&Expires=1357034400
&Signature=nitfHRCrtziwO2HwPfWw~yYDhUF5EwRunQA-j19DzZrvDh6hQ73lDx~-ar3UocvvRQVw6EkC~GdpGQyyOSKQim-TxAnW7d8F5Kkai9HVx0FIu- 5jcQb0UEmatEXAMPLE3ReXySpLSMj0yCd3ZAB4UcBCAqEijkytL6f3fVYNGQI6
&Key-Pair-Id=APKA9ONS7QCOWEXAMPL
As you can see Signature and Key Pair ID are encoded.
I need to use the above URL as a param in another URL.
I have base64 encoded (as to mask the domain, makes it a liitle prettier) and then URL encoded this.
My question is, with having certain params base 64 encoded, then base 64 encoding the entire string again, upon decode, will the original params such as Signature and Key Pair ID be readable?

Simple question, simple answer: Yes.

If you are going to do this, you will need to use "&" instead of "&" in the string you are encoding. Also, base64_decoding the entire encoded string, will only decode the last encoding.
An example:
$string1 = "This is a string";
$string2 = "This is another String";
$string1 = base64_encode( $string1 );
$string2 = base64_encode( $string2 );
echo $string1 . "<br />";
echo $string2 . "<br />";
$entity = "HTTP://www.google.com/?param1=" . $string1 . "&param2=" . $string2;
$encoded_entity = base64_encode( $entity );
echo $encoded_entity . "<br />";
$decoded_entity = base64_decode( $encoded_entity );
echo $decoded_entity . "<br />";
This will output:
VGhpcyBpcyBhIHN0cmluZw==
VGhpcyBpcyBhbm90aGVyIFN0cmluZw==
SFRUUDovL3d3dy5nb29nbGUuY29tLz9wYXJhbTE9VkdocGN5QnBjeUJoSUhOMGNtbHVadz09JmFtcDtwYXJhbTI9VkdocGN5QnBjeUJoYm05MGFHVnlJRk4wY21sdVp3PT0=
HTTP://www.google.com/?param1=VGhpcyBpcyBhIHN0cmluZw==&param2=VGhpcyBpcyBhbm90aGVyIFN0cmluZw==
So as you can see, you can decode the entire string, but only the string you encoded will be decoded. Not all the levels of the encoded string. For that you will first have to decode the string, and then after that, decode the parameters.

Related

Can't read string variables after using base64_decode() in PHP

I've been pulling my hair out all day with some broken code. I posted a question early, but I've narrowed down the problem to the specific issue.
Previous question: Can't assess an array element after building array - a var_dump confirms element is there
Now the issue is with the base64 decode function. It is meant to return a string, but whenever you try to read the string it doesn't work.
See this simple test code...
error_reporting(E_ALL);
$encoded = base64_encode('my encrypted text');
$decoded = base64_decode($encoded, true);
echo "<br />";
printf('my encrypted text -> encoded to base64 = %s', $encoded);
echo "<br />";
printf('%s from base64 = %s', $endcoded, $decoded);
echo "<br />";
printf('calling $decoded to read string: result = %s', $decoded);
Here is the result
my encrypted text -> encoded to base64 = bXkgZW5jcnlwdGVkIHRleHQ=
Notice: Undefined variable: endcoded in /home/website/base64test.php on line 10
from base64 = my encrypted text
calling $decoded to read string: result = my encrypted text
Line 10 is this:
printf('%s from base64 = %s', $endcoded, $decoded);
Is this a bug? Or am I missing something.
How are you meant to read the result from the base64_decode() function after it has been stored in a variable.
You have a typo: endcoded:
printf('%s from base64 = %s', $endcoded, $decoded);
//-------------------------------^
Should be
printf('%s from base64 = %s', $encoded, $decoded);
//-------------------------------^
Check typo of $endcoded,
printf('%s from base64 = %s', $encoded, $decoded);

PHP substr giving weird characters from xml feed

For some reason when using substr I am getting a weird character on output like this "".
All I am doing is this:
$price = substr($game->price, 1);
$dollars_original = substr($game->fullPrice, 1);
echo "Price: $" . $price . "<br />\n";
echo "Original Price: $" . $dollars_original . "<br />\n";
This comes from an XML feed here: http://itch.io/browse/platform-linux/price-sale.xml which i parse like so:
$url = 'http://itch.io/browse/platform-linux/price-sale.xml';
$xml = simplexml_load_string(file_get_contents($url));
So for example a price might be £0.89 but, when removing the £ sign it comes out as �0.89
What am I missing here?
It looks like you have a mixup in character encodings. What is Latin-1 or Windows-1252, and what is UTF-8? If you're working on UTF-8 data, you may have to use mb_substr(), which is UTF-8 aware.

How to encode and decode / character with PHP

In my dynamically generated website sometimes parts of my URLs contain a / character:
Serena Williams/Venus Williams-Andrea Hlavackova/Lucie Hradecka
And, naturally, the URL returns a 404 error as the / sign is considered a folder so the URL doesn't exist.
What would be the PHP function to encode and later decode the string which contains a / character?
You can use PHP: urlencode to encode and its counterpart PHP: urldecode to decode:
urlencode($stringinput)
You can user htmlentities() whit urlencode()
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="index.php?' . htmlentities($query_string) . '">';
?>

Why is this not making a plus sign?

I have:
$str = 'test%2B';
echo html_entity_decode($str);
I want it to return test +
What am I doing wrong?
NOTE: Sorry, the string cannot be modified. It's from an external source, I just need to make it replace the %2B with + signs somehow with PHP.
You didn't escape the space, and you should be using urldecode instead of html_entity_decode.
Try
$str = 'test%20%2B';
echo urldecode($str); // test +
If you wish to use html_entity_decode, use +:
$str = 'test +';
echo html_entity_decode($str); // test +
EDIT: If you need to decode a url that you cannot change yourself, urldecode should still work fine.
That string is encoded for a URL, not with HTML entities.
You need urldecode.
echo urldecode($str); // "test +"
An HTML-encoded string would look like this: test +, because none of those characters need HTML-encoding.
Try + instead. In your example, you are using URL encoding syntax and not HTML entity syntax.
In html a + is +. Try
$str = 'test +';
$str = "test %2B";
echo urldecode($str);

Using single 'smart quote' in my JSON data is breaking PHP script

I've got a PHP script that is reading in some JSON data provided by a client. The JSON data provided had a single 'smart quote' in it.
Example:
{
"title" : "Lorem Ipsum’s Dolar"
}
In my script I'm using a small function to get the json data:
public function getJson($url) {
$filePath = $url;
$fh = fopen($filePath, 'r') or die();
$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$json = json_decode($temp);
fclose($fh);
return $json;
}
If I utf8 encode the data, when I echo it out I see nothing where the quote should be. If I don't utf8 encode the data, when I echo it out I see the funny question mark symbol �
Any thoughts on how to actually see the proper character??
Thanks!
Is it possibe that the server is sending the json data in an encoding like windows-1252? That codepage has some smart code characters where iso-8859 has control characters. Could you try to use iconv("windows-1252", "utf-8", $temp) instead of utf8_encode. Even better would be if the server already sends utf-8 encoded json, since that is the recommended encoding per rfc4627.
The issue is more on the side, that generates the JSON file. There you should escape the ' by \'
If you can't modify this part, you should do it like this with addslashes:
$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$temp = addslashes($temp);
$json = json_decode($temp);
Can you possibly do a string replace assuming the data is all utf8?
$text = str_replace($find, $replace, $text);
Looking for the characters below?
'“' // left side double smart quote
'â€' // right side double smart quote
'‘' // left side single smart quote
'’' // right side single smart quote
'…' // elipsis
'—' // em dash
'–' // en dash

Categories