11

I'm trying to pass a complex URL as a url parameter but the problem occurs if the url contains & for example I want to pass the following link as a parameter

http://www.google.ps/search?hl=en&client=firefox-a&hs=42F&rls=org.mozilla%3Aen-US%3Aofficial&q=The+type+%27Microsoft.Practices.ObjectBuilder.Locator%27+is+defined+in+an+assembly+that+is+not+referenced.+You+must+add+a+reference+to+assembly+&aq=f&aqi=&aql=&oq=

I'm trying to get a URL as a parameter from a user and redirect user to this URL.

How could I handle this in PHP?


The whole Story:

I'm trying to make some ads analytics on flash files so user submit flash ads to a website which contains a link to the required webpage.

Now,my client needs to know how many times this flash file was clicked.To solve this I 'll till every one who submits flash to write a link to my client webpage and pass the required URL as a parameter as follows

http://myclientwebpage.com/disp.php?link=www.google.com&id=16

by this way I can update my database and get a count for how many times this link was clicked

Feras Odeh
  • 9,136
  • 20
  • 77
  • 121
  • Can you explain further what you actually want to do with it? – Andre Backlund May 05 '11 at 12:20
  • If I understand correctly, "www.google.com&id=16" in your example is the link that needs to be encoded. So, instead of `echo "href='http://myclientwebpage.com/disp.php?link=" . $link . "'"` write `echo "href='http://myclientwebpage.com/disp.php?link=" . rawurlencode($link) . "'"`. Then, in `disp.php` code, you'll have `$_GET['link']` which will contain "www.google.com&id=16" (not just "www.google.com"). – binaryLV May 05 '11 at 16:51
  • I think the only problem should be the ampersand `&` which should be written as `%26` in the original URI. All other characters can be introduced as is. – Brethlosze Jan 17 '19 at 19:54
  • [Escaping ampersand in URL](https://stackoverflow.com/questions/16622504/escaping-ampersand-in-url) – Brethlosze Jan 17 '19 at 19:57

2 Answers2

16

Use urlencode() or rawurlencode().


You wrote:

I'm trying to get a URL as a parameter from a user and redirect user to this URL.

But you didn't answer the question - how do you get that URL? How does user provide you with it? Is it written in <input type='text'/>? Or does user click some link that contains URL as one of parameters? Or is passed as ID of some URL that is stored in DB?

One case that comes into my mind - replacing URLs in plain text and sending user to some "redirecting page" before opening real page, so final page does not see HTTP referrer which might contain some secure data (e.g., session ID). In this case, you would write

<a href='redirect.php?link=<?php echo rawurlencode($url); ?>'>
    <?php echo htmlspecialchars($url); ?>
</a>

instead of

<a href='redirect.php?link=<?php echo $url; ?>'>
    <?php echo htmlspecialchars($url); ?>
</a>
binaryLV
  • 9,002
  • 2
  • 40
  • 42
  • 1
    The problem is that I got this URL from $_GET – Feras Odeh May 05 '11 at 12:18
  • 1
    And what is the problem? If you get this URL from `$_GET['url']` and want to put it into `href` attribute of `a` tag, then it would be something like `'>next link`, then in "next page" you would get it via `$_GET['link']`. – binaryLV May 05 '11 at 12:20
  • @binaryLV I suspect the problem here is that the URL isn't being urlencoded properly into the URL – James C May 05 '11 at 12:22
  • array 'link' => string 'www.google.ps/search?hl=en' (length=26) 'id' => string '13' (length=2) – Feras Odeh May 05 '11 at 12:24
  • @Feras Odeh, what exactly are you doing with that URL? Step by step. How do you get it and what you need to do with it? – binaryLV May 05 '11 at 12:29
  • @Feras Odeh, I've edited my post to include an example, though I'm not sure if it helps, as you still haven't answered, how exactly do you get that URL. "Parameter from a user" is not enough, as there are many ways how user might give you that parameter... – binaryLV May 05 '11 at 12:49
  • I think the only problem should be the ampersand `&` which should be written as `%26` in the original URI. All other characters can be introduced as is. – Brethlosze Jan 17 '19 at 19:55
0

From what I understand, you'll need to replace & into &amp;.

$url = str_replace('&', '&amp;', $url);

& is reserved, used for separating GET parameters, &amp; is for writing a literal ampersand in a URL.

Andre Backlund
  • 6,843
  • 3
  • 20
  • 27
  • 1
    Andre Backlund, I'd advise to use `rawurlencode()` or `urlencode()` (rather than to replace single character), as URL may contain not only '&', but also other special characters (e.g., '#' is the first that comes into my mind). – binaryLV May 05 '11 at 12:53
  • I think the only problem should be the ampersand `&` which should be written as `%26` in the original URI. All other characters can be introduced as is. – Brethlosze Jan 17 '19 at 19:55