here's the code i'm using:
$from=urldecode($_GET['from']);
$str =urldecode("%2B");
echo "$str<br>";
echo "$from<br>";
and here's part of the URL: from=%2B995594262653
why does this echo
+
995594262653
? (note, there's a space in front of the number).
i am using $str to check if the function works at all. apparently, it works for a simple %2B. What could be an issue?
It is working, but you need to realise that PHP will automatically urldecode the data it in puts in the $_GET array. You are doing it a second time and transforming the input even more.
When your script runs, $_GET['from'] contains +995594262653
When you run that value through urldecode, the + gets transformed to a space.
Related
Trying to post JSON data using PHP, but this text is displayed - {"message":"cjson decoding error","code":500,"error":true}
This is the data which is submitted via browser (used Live HTTP Headers plugin to get it):
{"nodes":[{"id":"fbm:n#1445506477266","type":"bookmark","parentId":"ROOT","refId":null,"href":"","text":"[New Bookmark]","desc":"","tags":"","cr":1445506584}],"revision":1,"token":"MTQ0NTUwNjQ3Ny4yOS1O9m2hacygwtDuox8/fHtN/efVUtNC9RvMlQbPAS+gwg=="}
This is my data:
json_encode(array("nodes"=>array(array("id"=>"fbm:n#1445506477266", "type"=>"bookmark", "parentId"=>"ROOT","refId"=>null,"href"=>"","text"=>"[New Bookmark]","desc"=>"","tags"=>"","cr"=>1445506584)), "revision"=>1,"token"=>"MTQ0NTUwNjQ3Ny4yOS1O9m2hacygwtDuox8/fHtN/efVUtNC9RvMlQbPAS+gwg=="))
When I print my data, I get exactly the same (except that json_encode function escapes / with \, but I read it doesn't matter in JSON). So here's the output of my data
{"nodes":[{"id":"fbm:n#1445506477266","type":"bookmark","parentId":"ROOT","refId":null,"href":"","text":"[New Bookmark]","desc":"","tags":"","cr":1445506584}],"revision":1,"token":"MTQ0NTUwNjQ3Ny4yOS1O9m2hacygwtDuox8/fHtN/efVUtNC9RvMlQbPAS+gwg=="}
As you see, everything is identical (except ). Even used Beyond Compare utility to compare every character of of the code between these, no differences.
How it comes the code still returns decode error? Tried using JSON_UNESCAPED_SLASHES as well (so no slashes are added), no result.
It seems your token is not true.
you can check with this code ;
$a = json_encode(array("nodes"=>array(array("id"=>"fbm:n#1445506477266", "type"=>"bookmark", "parentId"=>"ROOT","refId"=>null,"href"=>"","text"=>"[New Bookmark]","desc"=>"","tags"=>"","cr"=>1445506584)), "revision"=>1,"token"=>"MTQ0NTUwNjQ3Ny4yOS1O9m2hacygwtDuox8/fHtN/efVUtNC9RvMlQbPAS+gwg=="));
print_r($a);
echo '<br>';
echo '{"nodes":[{"id":"fbm:n#1445506477266","type":"bookmark","parentId":"ROOT","refId":null,"href":"","text":"[New Bookmark]","desc":"","tags":"","cr":1445506584}],"revision":1,"token":"MTQ0NTUwNjQ3Ny4yOS1O9m2hacygwtDuox8/fHtN/efVUtNC9RvMlQbPAS+gwg=="}';
Edit: JSON_UNESCAPED_SLASHES is working for me.
$a = array("nodes"=>array(array("id"=>"fbm:n#1445506477266", "type"=>"bookmark", "parentId"=>"ROOT","refId"=>null,"href"=>"","text"=>"[New Bookmark]","desc"=>"","tags"=>"","cr"=>1445506584)), "revision"=>1,"token"=>"MTQ0NTUwNjQ3Ny4yOS1O9m2hacygwtDuox8/fHtN/efVUtNC9RvMlQbPAS+gwg==");
json_encode($a,JSON_UNESCAPED_SLASHES);
print_r($a);
echo '<br><br><br>';
echo '{"nodes":[{"id":"fbm:n#1445506477266","type":"bookmark","parentId":"ROOT","refId":null,"href":"","text":"[New Bookmark]","desc":"","tags":"","cr":1445506584}],"revision":1,"token":"MTQ0NTUwNjQ3Ny4yOS1O9m2hacygwtDuox8/fHtN/efVUtNC9RvMlQbPAS+gwg=="}';
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 have a download page that take arguments like the download URL, the download-counter data file url, and the page to return to after downloading.
It is arranged like so:
start.php?url=...&page=...&file=...
(Download url, redirect page, counter file)
The problem is, when the redirect page contains PHP arguments with ? and & symbols, the URL becomes a confusing mess for PHP to work with.
Example:
start.php?url=URLTEXT&page=page?test1=x&test2=xx&file=FILETEXT
What should happen:
url=URLTEXT
page=page?test1=x&test2=xx
file=FILETEXT
what happens:
url=URLTEXT
page=page?test1=x
test2=xx
file=FILETEXT
How could I substitute characters or somehow make these arguments pass correctly in php?
Thanks for any help you can give.
Well, I'm not sure how your "messed up" URL looks like. However the string after the "?" is called Query String, and you can decode/encode it with
urlencode($normalString); //will be encoded for use in URL
urldeocde($queryString); //will be decoded for "normal" use
EDIT:
Here is some short example:
echo "Encode for use in URL: ";
echo urlencode("this is a string & üäöllasdlk<bbb2");
echo "<br />";
echo "Decode to use it in your script: ";
echo urldecode($_SERVER['QUERY_STRING']);
Output:
Encode for use in URL:
this+is+a+string+%26+%C3%BC%C3%A4%C3%B6llasdlk%3Cbbb2
Decode to use it in your script: test=12
(Assuming you have a Querystring containing the variable test=12)
Just use htmlspecialchars function on your URL string:
http://php.net/manual/en/function.htmlspecialchars.php
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 )
I format text with javascript asigning + to every emtpy space like this
var ft = text.replace(/ /g,"+");
Then I pass ft to a php script via jquery ajax as an get argument.
But
print $_GET['text'];
gives me the text with empty spaces instead +.
Any ideas?
You should get familiar with the concept of URL encoding.
PHP's urldecode function will run against all $_GET variables by default, so if you want to see raw input, use rawurldecode:
$encoded = array_map('rawurldecode', $_GET);
echo $encoded['text']; //blah+blah
Also, it's a good idea to use JSON to pass data from javascript to PHP.