Strange value when using Base64enconde and decode - php

I m building an website and i'm using the base64encode() and base64decode().
I send the base64encode(value) through and url and i receive and then i decode the value. On the page that i have the decoded value i have one button that goes to the previous page and i use the encode to encode again the value and send it. the previous page i decode that value and i encode again to send to the same page.
At the first time i receive the correct values but when i do more than 1 i get strange value. It is my code:
The first page:
Here i receive one value and i decode it.
$tablename = base64_decode($_GET['tablename']);
In the end of the page i have on list with this code, here i send the encoded value:
$descricao = base64_encode($row["descricao"]);
$tablename = base64_encode($tablename);
?>
<a href="./gestaoalarme.php?descricao=<?=$descricao?>&tablename=<?=$tablename?>">
In the seconde page i receive the encoded value
$tablename=base64_decode($_GET['tablename']);
and i have that button that goes back and send the encode value.
<a href="./verdispositivos.php?tablename=<?=base64_encode($tablename)?>" class="btn Back btn-lg">
The first time results, but then not.

Base64 encoded values are not URL safe due to the + / and = characters.
You will also need to use urlencode() before using the string in a url.
E.g.
$value_to_encode = 'some=string/here+foobar';
$encoded = urlencode(base64_encode($value_to_encode));
$decoded = base64_decode(urldecode($encoded));

Related

How to remove %0D%0A from end of a parameter in PHP

I am trying to hit a URL after generating the data to be filled for the parameters that are passed in URL using Python in back end. So the flow is:
User lands on a page with a form having some drop downs.
Python code in the backend reads the content from a file and returns single output based on some conditions for each of the dropdown.
User hits the submit button with the data.
The data gets generated correctly but when I hit submit button, I get %0D%0A characters at the end of the parameter values in the URL
E.g., sample.php?param1=20%0D%0A&param2=50%0D%0A
How do I get rid of these values as this is causing trouble with the other code where I am using these values?
I take it you read the data from a file, so probably reading the file causes the line endings to be read as well.
In any case, try using strip() or rstrip() in your Python code to remove all/trailing whitespace before your assemble the target URL.
I understand that it's actually a PHP script that assembles the URL. In that case, use PHP's trim() function on the variables you use to assemble the URL.
For example: Assume that $val1 and $val2 are read from a file or some other place. Then the following line assembles above URL stripping whitespace from $val1 and $val2.
$url = "sample.php?param1=" . trim($val1) . "&param2=" . trim($val2);
Some browsers do that automatically, you can try decoding it back using urldecode()
http://php.net/manual/en/function.urldecode.php
Try this :
<?Php
$str = "Your Inputed Value or string"
$url = str_replace(" ","-", $str);
?>
Link Menu

PHP - GET url encode #, \ and & sign

This PHP code:
$test = '#&\\';
echo rawurlencode($test);
Results in this:
http://example.com/test=%40%26%5C
Which in a $_GET request in a url becomes something like this:
test=#&\
The above is not an allowed $_GET variable.
How can I still use a $_GET variable with these values, encode them and decode them back to get these values?
The URL will most certainly be http://example.com/test=%40%26%5C if you create the URL as http://example.com/test=%40%26%5C. When PHP receives a request for this URL, PHP automatically decodes the query string and puts it into $_GET. The URL didn't change, you simply have the decoded value of the query string available to your PHP script in the $_GET variable.
Additionally the browser may decide to display the URL in a decoded form in the address bar for usability/readability. It does not mean that the actual URL contains #&\ characters.

save url in MySql databse via PHP getting failed

I have a array of URL which I am saving in database after iteration on PHP side.
I am sending the array with Ajax and saving it with PHP.
Data send via Ajax
linksString=http://localhost/phpmyadmin/index.php?db=testdb&token=42d0dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=98604a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=9864dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=986042d0dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=986042d0dde57469a9a23&q=save
Not getting all values in $linksPieces , Getting only one value
But I am not getting this all string in PHP side; only getting first sub string which is before first comma(,).
I.E
http://localhost/phpmyadmin/index.php?db=testdb&token=42d0dde57469a9aa4b6a2f7e0741
PHP
$linksPieces = array();
$links = $_POST['linksString'];
$linksPieces = explode(",", $links);
foreach($linksPieces as $link)
{
//operation
}
I need to get all the string in array on PHP side.
If I am sending these type of url which have not any = then working fine.
http://in.yahoo.com/
http://www.hotmail.com/
http://www.google.com/
http://www.blah.com/
http://www.blah1.com/
You can try using encodeURIComponent() in Javascript before sending this string and using urldecode() in PHP before using this string.

PHP URL decode GET

I have been using URL decode on encoded URL variables from $_get.
The current problem I am facing is I have a URL encoded like this:
blah.php?url=http%3A%2F%2Fm.youtube.com%2F#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=%2Fwatch%3Fv%3Dzd7c5tQCs1I%26feature%3Dplayer_embedded
I'm not sure what kind of encoding this is, can someone help me? When I use just "urldecode" on this it just returns m.youtube.com
Edit: My problem is not that url decode isn't working, it works if I manually enter this encoded URL and use urldecode(), but when this encoded url is in the actual pages url and I use the _GET function then I try to decode it it stripes off everything after the "#" in the URL.
<?php print urldecode($_GET["url"]);?>
It returns
"http://m.youtube.com/"
instead of
"http://m.youtube.com/#/watch?feature=player_embedded&v=zd7c5tQCs1I&desktop_uri=/watch?v=zd7c5tQCs1I&feature=player_embedded"
I think the issue is that the pound sign is not encoded, if I refresh the page it strips away the pound sing and everything after it, so how do I get around this? Can I still retrieve the info from "GET" even though there is a pound sign? (#)
The problem is that the full link has multiple = signs, and browser cant determine, that the other = signs refer just to the url= parameter.
in your case, at first, you need to use function before link is given to url= parameter:
========================= 1) JAVASCRIPT ======================
<script type="text/javascript">
var mylink = encodeURIComponent('http://testest.com/link.php?name=sta&car=saab');
document.write("http://yoursite.com/url=" + mylink);
</script>
========================= 2)or PHP ===========================
<?php
$mylink = 'http://testest.com/link.php?name=sta&car=saab';
echo 'http://yoursite.com/url='.urlencode($mylink);
?>
so, your output (url parameter) will get like this
http://yoursite.com/url=http%3A%2F%2Ftest.com%2Flink.php%3Fname%3Dsta%
so, the url parameter will get the encoded url.
after that, your .php file needs to decode that "url" parameter-
<?php
$varr = $_GET['url'];
$varr = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($varr));
$varr = html_entity_decode($varr,null,'UTF-8');
echo $varr;
?>
that will give you the correct value
I read on php.net about urldecode function and they say that superglobal $_get is already decoded, ex: "The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."
It is encoded into ASCII format .
see http://www.w3schools.com/tags/ref_urlencode.asp
So here is the problem, the pound sign (#) (Hash) wasn't encoded... since I can't go back and re-encode it I have to use javascript (ex. alert(window.location.hash);) to send me the full URL after the hash then I append it to PHP's version of the URL, I THEN use a find and replace function in PHP to replace the "#" with "%23", then I use the urldecode method and it returns the full proper url decoded.
This encoding is called percent encoding or URL encoding. You can use urldecode for decoding it. (Example: http://phpfiddle.org/lite/code/0nj-198 )

How to encode, send in a query, and retrieve a url

I am trying to pass a url for creating an iframe as a parameter of a query string. Because the url that I am passing contains an ampersand, I encode the url with 'urlencode' then append it to the query string.
<?php
$url = "http://www.somesite.com/index.php?option=content&view=article&id=1234:some+article";
$url_encoded = urlencode($url);
?>
On the page where I want to create the iframe, I retrieve the url parameter using the $_GET variable.
<?php
$iframe_source = $_GET[$url];
?>
<iframe id="external-link-frame" src="<?php echo $iframe_source ?>"></iframe>
However $_GET only retrieves the part of the parameter value up to the encoded ampersand.
<?php echo $_GET[$url]; //outputs http://www.somesite.com/index.php?option=content ?>
What must I do in order to send the entire url including the parameters that are part of its own query string.
UPDATE: I am able to do it by encoding the url twice
urlencode(urlencode($url));
Take a look at: https://stackoverflow.com/a/2433211/1359529
I think rawurlencode() will encode the ampersands too.
http://us3.php.net/manual/en/function.rawurlencode.php
I believe, how you append it, the $_GET function thinks that the ampersand signs are signifying new values to get. I bet if you did after that $iframe_view = $_GET[$view] it will output article.
If you want it to get the full URL, I think it's best to encode by replacing & signs with something else and then once you get the url, then replace them back to & signs.

Categories