PHP - Is $_GET still functional with spaces in the URL? - php

I've got this simple question... If you've got spaces in the URL $_GET, lets say
"page=about us", and if you then use this code IF($_GET['page']=='about us'). Would that work? Or do you have to use IF($_GET['page']=='about%20us')?
Thanks for your time!

Your browser converts the literal space in URLs into a + sign - before sending a HTTP request. When PHP sees that very + sign, it will become a space again for $_GET.
So, yes, it will work when comparing it against == "about us". Don't compare against the %20. (That's a valid, but unlikely encoding in URLs anyway.)
Sidenode. It's best not to rely on browser magic. When outputting the link, embed the + in place of spaces yourself.

Look at url_decode.

Related

The url get replaced by space character %20

I have this URL https://plus.google.com/+pitneybowes, where + sign in url. I have used this url in my code anchor tage
but when i run my code and view it in browser it get replaced by this https://plus.google.com/%20pitneybowes
I want + sign in URL. it is necessary. How to solve this issue?
According to the URL specification, %20 and + are equivalent representations of an escaped space (ASCII 32). Whether a browser presents a %20 or + is implementation-dependent.
Your site should be designed to handle both. Or really, it should not be working with the raw URL, but the unescaped and parsed URL-- which in either case would contain a space.
If you really are coding your web site to require that spaces be represented as a + specifically, and this is somehow mission critical, you are setting yourself up for compatibility issues.
If you actually need a + in the URL (unescaped), the hex code for that is %2B.
Thus
Print(UrlEncode("This is a test")); = "This+is+a+test"
or "This%20is%20a%20test"
Print(UrlDecode("This%20is+a%20test")) = "This is a test"
Print(UrlEncode("What is 2+2?") = "What+is+2%2B2%3F"
You can use urlencode to encode your url. The decoding will happen automatically, and can get the value from $_GET.

large link when using $_get

i am processing a activation link.which looks like this.
localhost/actvte/validate.php?type=activate&geo=define&
value=227755RYQBENU5G8WE7RFPO6CD6Z#MJ1H1FA#G#IZWZ53903
&target=loaded&resrc=G6MYMI2R67727229911380184297841084713071U8VUYIGR
&master=user#gmail.com
but when i use $_get['value'] i get o/p only "227755RYQBENU5G8WE7RFPO6CD6Z".after this whole link becomes useless.if i do
echo $_get['target']; or echo $_get['master'];
it says undefined variable 'target' or 'master'.
so how can i process this large link.
What you should do is use the urlencode() function in PHP on the string before putting it in the GET. This way your string becomes 227755RYQBENU5G8WE7RFPO6CD6Z%23MJ1H1FA%40G%23IZWZ53903 and not 227755RYQBENU5G8WE7RFPO6CD6Z#MJ1H1FA#G#IZWZ53903 as special characters cannot be used in the query string.
Hashes for example will never even be send by the browser to the server, so everything behind that will not reach you.
Please look at RFC 3986 for more information about the URI syntaxing (including hashes).
By not using or properly encoding the fragment identifier (#).
You should not use # in the URL but encode it some way, or use another character.
The first # in a URL indicates the start of the fragment identifier. If you want to send it as data rather then a separator component of a URL then you need to express it as %23.

Escaping white space : %20 or + - smarty

I am using smarty as a template engine. I have to escape an image file path {$filepath|urlencode}, the problem is that the white space are converted into a '+', which prevent the image to be reached on the server : %20 would work, how to escape correctly my path ?
Edit : more precisely, I use the facebook share link
I use a facebook share as so and it doesn't display the image when shared :
``
The final code looks like for my specific usage :
<a href="http://www.facebook.com/dialog/feed?app_id=...&link=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18&picture=http%3A%2F%2Fmysite.org%2Ffiles%2Fcampaign%2Fimage%2Foriginals%2F18%2FSans+titre-3.jpg&name=Some text "Text d'Text", Text&description=Rejoignez%20la%20campagne%21&redirect_uri=http%3A%2F%2Fmysite.org%2Findex.php%3Fpage%3Dcampaign%26campaign_id%3D18"onclick="window.open(this.href);return false;">
on the same site, all the facebook share link works perfectly and the image displays well ! Reason why I thought it was the link of that specific image that is not working
escape is what you're searching for. Take a look at:
http://www.smarty.net/docsv2/en/language.modifier.escape.tpl
{$filepath|escape:"url"}
urlencode is used to encode (not escape!) a string to be used as a query part inside an URL passed as GET var: http://php.net/manual/en/function.urlencode.php
URL encoded space is either a plus sign or %20. They are equivalent, and are both interpreted as a space on the server.
If you see either in the URL, then the server will see a space.
You say that the plus sign is preventing the image from being loaded. This sounds like a deeper problem than simply using the wrong encoding. Possibly it's being double-encoded?
What is the actual URL being requested in the browser? Open the dev tools/Firebug, and look at the requests to find out. If the URL includes %2B then the plus sign is being double-encoded. This is the problem you need to solve.
The other solution, of course, is not to use spaces in filenames on the web. The only reason one would want spaces in filenames is for readability, but since the web requires spaces to be urlencoded, it removes that readability anyway. Take away the spaces, and the problem will go away by itself.

Zend Framework removes chars from get parameters?

Does Zend Framework hijack the raw $_GET and remove chars? I can't figure out whats going on with a script I am writing.
right now I am passing a variable "email" via a param in the url. Which its working, however. Its stripping out a character and leaving a space.
trying it like $_GET['email'] or like $params = $this->getRequest()->getParams(); $params['email'] on an email string that has a + in it the + gets removed and is replaced with a space. Yet I'm not applying anything that would cause that, that I am aware of anywhere.
So, does anyone else know what the issue may be?
Php does that, see this question:
When to encode space to plus (+) or %20?
To get a "+" use %2B in the url

escaping json string with a forward slash?

I am having a problem passing a json string back to a php script to process.
I have a json string that's been created by using dojo.toJson() that contains a / and looks like this:
[{"id":"2","company":"My Company / Corporation","jobrole":"Consultant","jobtitle":"System Integration Engineer"}]
When I pass the string back to the php script it get's chopped at the / and creates a malformed json string, which then means I can't convert it into a php array.
What is the best way of escaping the / in this string? I was looking at regular expressions and doing a string.replace() however my regex isn't that strong, and I'm not sure if there are better ways of doing this?
Many thanks
You shouldn't need to do anything special to represent a / in JSON - a string can contain any character except a " or (when not used to start an escape sequence) \.
The problem is possibly therefore in:
the way you parse the JSON server side
the way your parse the HTTP data to get the JSON string
the way you encode the string before making the HTTP request
(I'd bet on it being the last of those options).
I would start by using a tool such as LiveHttpHeaders or Charles Proxy to see exactly what data is sent to the server.
(I'd also expand the question with the code you use to make the request, and the code you use to parse it at the other end).
\/. Take a look here. The documentation is really easy to read, concise and clear. But unescaped / should still be valid in JSON's string so maybe your bug is somewhere else?
Ok. Anyway.
When passing variables to PHP don't use JSON - it's good for passing variables other way.
Instead you better use http://api.dojotoolkit.org/jsdoc/1.3/dojo.objectToQuery method and on PHP side parse standard PHP $_GET variables.
EDIT: Ok, I'm 'lost in the woods' here also, but here's a tip - check if you don't have some mod_rewrite rules in action here. Kind of seems like that.
Also, if you can send me the URL which gave you 404 (you can cut out domain part, i'm interested in script filename and all afterwards) maybe I can give you more detailed answer.
To be clear, whether you choose to send JSON to PHP or use regular form values is a matter of preference. It /should/ work either way. It sounds like you aren't url-encoding the JSON at the client-side so the server-side is treating / as a path delimiter. In which case its borked before json_decode gets to it.
so, try encodeURIComponent( dojo.toJson(stuff) )
json_encode() used to escape forward slashes. like this:
prompt> json_encode(json_decode('"A/B"'));
string(6) ""A\/B""
JSON_UNESCAPED_SLASHES was added in PHP5.4 to suppress this behavior.

Categories