retrieve a character encoded portion of the uri? - php

I am using codeigniter php framework. I have a url that looks like this:
www.mydomain.com/controller/function/param
however, when the param value has encoded characters such as 'plus%2b' all that I can see (by checking the parameter) is 'plus'. So basically I am loosing all encoded characters and anything that may occur after them. I tried accessing the uri string directly using $this->uri->segment(n) but got the same issue.
How can I preserve encoded values so that if the url looks like: www.mydomain.com/controller/function/plus%2b, I get the string 'plus+'?

I think in config.php there is a whitelist of allowed characters (permitted_uri_chars), so you probably would need to add the additional characters here.

Related

How do I sent an encrypted hash over URL in CakePHP 3?

I'm working on a reset password function that matches hashes to allow for the user to reset. The only trouble is the hash that was created cannot be passed over URL without breaking the page. For example, my hash is this:
http://localhost/users/changeResetPassword/e0b4ab1d2cdc5742c7b5f72ef6c2935dadfe458dc275b7419d9f1ac66461aa20%0F5%3A%C6%5C%26%2A%E4%D5%ACA%94%ADV%BF%EB%CAz%97O%1F%7D%F0h~%E3-.%FF%B4z%5E%1AQ%B8%8Ca%BC500%2A%EC%7B%FA%AF8%E3%2A%7F%BA%A4y%03%AE%29%94%09%26%9E%29e%E5%DEn%1At%C1%EC%F7%D4x%EAvlA%BE%5B%0D%CF
All of these % seem to break the page, because I get this error:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If I take away everything up to the last % and try it again, the page loads fine. It's just some characters in the URL seem to bug everything out:
http://localhost/users/changeResetPassword/e0b4ab1d2cdc5742c7b5f72ef6c2935dadfe458dc275b7419d9f1ac66461aa20
This loads fine, so I know it's just a URL problem. Any ideas?
Base64 encode the hash and pass it along, then decode when received. Cake URI parsing likely breaks because it'll try to interpret those %xx as html encoded values but it does not appear that is what they are since you have stuff like %03 and from the link: "The ASCII device control characters %00-%1f were originally designed to control hardware devices. Control characters have nothing to do inside a URL"
With regards to ndm's comment indicating you may additionally need to URL encode the base64 string since base64 can contain the characters + = / I'd recommend you also look at url encoded forward slash is breaking url to see why simply encoding the problematic characters might also be problematic. If the current solution of passing unencoded base64 strings is not causing any issues with your rewrite rules I would recommend you keep it as it.

Angled brackets being escaped from url in PHP

I am trying to call the USPS API that takes in the zip code and returns XML containing the City Name of the given zip code.
Here is the URL they require:
http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup
&XML=<CityStateLookupRequest USERID="xxxxxxx"><ZipCode ID= "0">
<Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>
In my PHP file, when I echo out the above URL, this is what I get:
http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=90007
All the XML part of the URL is missing. I need to get curl data from the URL.
Anyone know what I could be missing?
Anyone know what I could be missing?
Probably. Maybe, yes. What you describe in your posting sounds like an encoding problem. So you are missing the right encoding.
As you are talking about an URL that is likely URL encoding. Some characters - like space - have a special meaning inside an URL so you can not just use any character as you like, but you need to encode all characters properly.
The exact description how you need to formulate an URL incl. the exact description how URL encoding works is outlined in 2. Characters in the internet standard RFC3986.
PHP functions related to URL encoding are urlencode() and rawurlencode and more likely useful in your case http_build_query().
http://php.net/manual/en/function.urlencode.php should get you started if you want to encode the xml into the URL.

Posting json string which contains £

I have a VB6 application that is creating a JSON string and posting it to a website (PHP5). It may look like this:
data=thisisthejsonstringitcontainsthe£hmtlcharacter&code=123&api_key=321
This is an issue because the £ is thought to be the start of a new variable so the json string is being cut.
Does this need to be encoded somehow at the VB source? Or can I do something with this when it arrives at the website? If it needs encoded by VB can anyone suggest a suitable function?
I'm using the application/x-www-form-urlencoded content type when posting.
This might seem far fetched, but have you tried sending &pound urlencoded beforehand? being %26pound
If you send url arguments to a webserver, you'll need to urlencode them. That's true for all arguments in formats that don't escape and can contain problematic characters themselves, which includes JSON.

URL as URL's get parameters - problem with "&"

There is script that receives another url as GET parameter:
script.php?file=http://www.google.com&id=123
The problem is:
when url has parameter itself, it is used as script's parameter, not that url's parameter:
script.php?file=http://www.google.com?q=adsf&lang=en&id=123
URL is http://www.google.com?q=adsf&lang=en, but it is chopped after &, because it is viewed as related to script.php itself.
What can I do about this?
I tired to replace & with %26, but url get broken with it.
You need to encode the value with the percent-encoding.
If you’re using PHP, use rawurlencode (or urlencode if application/x-www-form-urlencoded is expected):
$url = 'http://www.google.com?q=adsf&lang=en';
echo 'script.php?file='.rawurlencode($url);
You need to URL encode the entire URL that you are passing as a parameter to another url (your script). %26 is the correct encoding for an &. Just make sure you decode it server-side before using it. You don't say what language(s) you're using, but most, inc javascript and php have native URL encoding functions.
Try to encode every special character like this:
script.php?file=http%3a%2f%2fwww.google.com%3fq%3dadsf%26lang%3den&id=123
although it might be better and easier to use rawurlencode().
Also, read this about URL encoding.

Is it possible to output the '&' sign in an xml file?

I have a php which generates an xml file and prints it on screen.
Amongst other variables, it prints an Image link.
The problem is that if this Image link has an '&' character in it, I get an xml error because it isn't encoded properly.
So I solve it by replacing the & sign with &.
Atleast I thought it was solved, now the link to the image is for example like this:
www.domain.com/phones & equipment/img1.jpg
which causes a 404 file not found.
The real path is
www.domain.com/phones & equipment/img1.jpg
So how can I solve this then?
I would prefer not to change the folder names, I simply didn't know this when I created the folders.
Thanks
If it's a URL, you might want to URL encode it instead:
www.domain.com/phones%20%26%20equipment/img1.jpg
try to
www.domain.com/phones+%26+equipment/img1.jpg
You should url encode the link using php urlencode() function. The code for '&' is "%26".
Additionally, if you check IANA RFC regarding URL/URI, you will see that space character is not a valid character and shouldn't be present inside REQUEST URI. Having your URL like www.domain.com/phones-and-equipment/img1.jpg would be much beneficial from SEO standpoint as well.
ADDENDUM: For example, check page 2, section "Unsafe" of RFC 1738 and see why non-printable and non-US-ASCII characters are not safe.
use html_entity_decode to convert & back into an &
see http://php.net/manual/en/function.html-entity-decode.php
The correct representation of the ampersand in an XML file is &. If that isn't working, the problem is with the code that is reading the XML file and deferencing the URI.

Categories