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

4
  • Can you explain further what you actually want to do with it? Commented May 5, 2011 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"). Commented May 5, 2011 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. Commented Jan 17, 2019 at 19:54
  • Escaping ampersand in URL Commented Jan 17, 2019 at 19:57

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

7 Comments

The problem is that I got this URL from $_GET
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 <a href='?link=<?php echo rawurlencode($_GET['url']); ?>'>next link</a>, then in "next page" you would get it via $_GET['link'].
@binaryLV I suspect the problem here is that the URL isn't being urlencoded properly into the URL
array 'link' => string 'www.google.ps/search?hl=en' (length=26) 'id' => string '13' (length=2)
@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?
|
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.

2 Comments

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).
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.