1

I am stuck big time on this problem and google has been of no help to me so far. I am trying to find a way to preserve white space in a URL with moderate to no luck.

I have a form that needs to gather post data, mail it, and then append the post data to the URL as comma separated value and redirects them to a page where they download a product.

Once the user presses download that page reads the data in the URL and applies it to a billing invoice (the program is billed on time usage).

A simplified example:

$addressOne = $_POST['addressOne'];
$newURL = "http://subdomain.domain.com/connectnow=on?" . ", Address1=" . $addressOne;
If(mailSent) {
    header("Location: $newURL")
}

There are a lot more values obviously, but the address is one of the areas that I am having this issue.

I have tried doing something like:

$newURL = str_replace("  ", " ", $newURL);

That worked as far as preserving the whitespace in the URL visually, but when the program that gets downloaded reads the URL it replaces the   as %C2%.

I have also tried:

$newURL = str_replace("  ", " \40", $newURL);

That made the spaces in the URL convert back to %20.

Any guidance would be appreciated.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Phill M.
  • 11
  • 2
  • 3
    Have you tried `urlencode()` and `urldecode()`? – Rasclatt Apr 15 '15 at 18:54
  • better you should use `urlencode` and `decode` – Kamran Apr 15 '15 at 18:55
  • http://stackoverflow.com/questions/497908/is-a-url-allowed-to-contain-a-space – j08691 Apr 15 '15 at 18:55
  • Space and special characters are URL encoded while in browser if you read url in the redirected page using PHP you will get space back. It is normal behavior. – Durgesh Chaudhary Apr 15 '15 at 18:56
  • 1
    The reason you can't have literal whitespace in the path is that a HTTP request line has to be serialized as `GET␣/virt/path?params␣HTTP/1.1`. Extra spaces there would invalidate it. Which is why browsers substitute `+` or `%20` right away. -- Also please don't copy sample code from Word etc. Those typographic quotes make poor examples for any other users that accidentally come across this question. – mario Apr 15 '15 at 18:58
  • I have tried using both urlencode($newURL) and urldecode($newURL), Sorry I should have stated that earlier. – Phill M. Apr 15 '15 at 19:15

1 Answers1

2

URL:

www.site.com/my spaces preserved/

urlencode()

www.site.com%2Fmy+spaces+preserved%2F

urldecode()

www.site.com/my spaces preserved/
mend3
  • 506
  • 4
  • 15
  • I tried urlencode() previously and didn't mention it. I just tried the urldecode() and it didn't work either. I know that it is possible because this is a redesign of a site that was originally done (by someone else) five years ago using coldfusion. – Phill M. Apr 15 '15 at 19:27
  • what did you got (output) when trying the urldecode()? – mend3 Apr 15 '15 at 19:34
  • www.site.com/my%20space%20preserved/ – Phill M. Apr 15 '15 at 20:16