The url get replaced by space character %20 - php

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.

Related

why GET method didn't decode attributes in case url has attributes have spaces?

kindly I have two links,
when using both of the links in another page, the first link is decoded automatically by GET Method and the second didn't.
the problem is that if there is a space in any attribute, the get don't decode automatically the URL and if there are no spaces, the get automatically decoding the URL which is the correct behaviour
tip : the only encoded attribute is BodyStr and encoded via URLENCODE PHP function.
another tip: the difference between both is the space in subjectStR Attribute
I want to know why spaces in URL prevent GET Global Variable from automatically decoding all the attributes
$message=urlencode($message);
http://localhost/test4.php?me=ahmed&y=1&clientid=55&default=1&Subjectstr=**Email From Contactuspage`**&BodyStr=$message
http://localhost/test4.php?me=ahmed&y=
1&clientid=55&default=1&Subjectstr=**EmailFromContactuspage**&BodyStr=$message
Space isn't allowed in URL query strings. If you put an unencoded space in SubjectStr, the URL ends at that point, so the server never sees the BodyStr parameter.
You need to URL-encode SubjectStr. Replace the spaces with + or %20.
$message=urlencode($message);
$url = "http://localhost/test4.php?me=ahmed&y=1&clientid=55&default=1&Subjectstr=Email+From+Contactuspage&BodyStr=$message"
The reason why it stops at space is because of the HTTP protocol. The client sends:
GET <url> HTTP/1.1
This request line is parsed by looking for the space between the URL and the HTTP version token. If there's a space in the URL, that will be treated as the end of the URL.

Query string (with space in between) in php html

I am trying to do a query string in html.
String that I want to pass is "Book Cover".
But I only managed to get Book.
How should I go about doing it?
Below is my code:
<a href=book.php?category=Book Cover>Book Cover</a>
You need to encode all your query string vars, For example with rawurlencode / rawurldecode
Book Cover
And in PHP:
$category = rawurldecode($_POST['category']);
In HTML the value stops at the space:
<a href=book.php?category=Book Cover>Book Cover</a>
^
If you want to include a space inside a value in HTML you need to add quotes:
Book Cover
^ ^
In HTML both single and double quotes are allowed.
Now the value itself has a problem, too:
book.php?category=Book Cover
`- URL stops here.
This is a relative HTTP URL and as for any HTTP URL the space character is a special value. It can normally not be part of the URL, therefore you need to encode it. This can be done as with any other special character in a HTTP URL with triplet encoding / percentage-encoding replacing the binary value of the character(s) with their hexadecimal number:
book.php?category=Book%20Cover
For the space you have, historically it is even a special-case, you can also encode it with the plus sign.
The later problem is often dealt with by the user agents, but the quotes in HTML are needed otherwise the value gets cut.
And it is generally good practice to place attribute values in HTML inside (double) quotes. So I suggest you to do that.
Why not convert to UTF-8 before encoding?
urlencode(utf8_encode($string));
Looks like you are missing double quotes
Book Cover

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.

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

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.

# character in request string

When a PHP site is requested like e.g. somesite.php?a=some text # some more
Currently it seems like $_REQUEST["a"] returns the string "some text ", how can I get "some text # some more". Are there other characters which get similar treatment by PHP?
The hash (#) is a "Fragment identifier" (also informally known as an "anchor") and refers to a location within the page - and you're right, it doesn't get sent to the server.
It's the only URL character that behaves like this.
If you need to include a hash character in a URL, you need to encode it as %23
somesite.php?a=<?=urlencode( "some text # some more" )?>
Turns it into:
somesite.php?a=some+text+%23+some+more
The "#" character is special in the URL specification; it refers to a location on the page (named by an 'a' tag like: <a name='top'>this is the top of the page</a>). It, and everything after it, is not passed to the server (php).
You can't. The browser doesn't send the part of the url after the # to the server.
So, it's not PHP that removes that part of the URL, it never sees it.
You can use urlencode() & urldecode() functions
it will be %23 instead of # symbol
If you have to have it, you can write javascript to re-post it for you:
<script>
function checkHash(){
var hash = window.location.hash;
if(hash){
// see if we've already included this
var loc = window.location.pathname;
if(!loc.indexOf('hashData=') >= 0){
// hashData param not included
window.location.href = loc + '&hashData=' + encodeURIComponent(hash) + hash;
}
}
};
checkHash();
</script>
There are some obvious issues with this (like it double-submits items). Note - if someone clicks on a hash link in the page, the code won't re-run, so you would need to monitor the hash for changes. You may or may not care about that case.
The # character, and any characters that follow it, specify and anchor.
This is a point within the page that the browser will scroll to.
As far as I know, this is for the browser's use only, and is never transmitted to the server side - presumably because it means nothing to the server,

Categories