2

I need to pass a url to a php page. the url may or may not contain special characters and encoded variables. for example consider the ajax request.

var url = 'http://siteaddr.com/abc%20cdf%202012%20movies%20software/';
$.get('check_link.php',{url:url},function(){
    //some function
});

the echo result of variable $_GET['url'] in the php page is http://siteaddr.com/abc cdf 2012 movies software/ the %20's are converted into spaces. I need to receive the url as it is. tried encoding and decoding the url. but I didn't got the result as i need.

  • 1
    [URL encode](http://php.net/manual/en/function.urlencode.php) it! Or is it what you already did? – Jørgen R Dec 06 '12 at 07:35
  • by encoding it, I will receive the whole url encoded right. so i must decode it at the php page. by decoding, it decodes the %20 in the url too... thats y I need some alternative.. – Feroz Noushad Dec 06 '12 at 07:42
  • http://stackoverflow.com/questions/1174189/php-get-url-with-special-characters-without-urlencodeing-them – Jørgen R Dec 06 '12 at 07:44
  • that is url encode of `http://siteaddr.com/abc%20cdf%202012%20movies%20software/` results `http%3A%2F%2Fsiteaddr.com%2Fabc%20cdf%202012%20movies%20software%2F` while decoding it results `http://siteaddr.com/abc cdf 2012 movies software/` which loses the %20. – Feroz Noushad Dec 06 '12 at 07:45
  • What if you do `$url = urlencode($_GET['url']);`? Alternatively, you could use `preg_replace()` – Jørgen R Dec 06 '12 at 07:47
  • %20 is just a single condition... there must be many more special encoded variables will be there in the url right? so its practically possible to do that for all? – Feroz Noushad Dec 06 '12 at 08:46
  • You want the `%20` and other entities kept intact within your submitted url ? May we know the specific reason for keeping the encodings at server side? If so, we might be able to suggest something else. – Akhilesh B Chandran Dec 06 '12 at 09:17
  • i need to just check whether the entered url is there in my database. – Feroz Noushad Dec 06 '12 at 09:37

1 Answers1

0
  1. You're not supposed to define a variable with the name that will be used as a key.
  2. Encoding the URL again, before sending it will make PHP decode the double encoded URL.

    var link = 'http://siteaddr.com/abc%20cdf%202012%20movies%20software/';
    $.get('check_link.php',{url: encodeURIComponent(link)}, function(){
        //some function
    });
    // Double encoded URL: Note that % has become %25
    // http%3A%2F%2Fsiteaddr.com%2Fabc%2520cdf%25202012%2520movies%2520software%2F
    

http://jsfiddle.net/zNqac/

mccbala
  • 183
  • 1
  • 7