12

I am trying to make a bookmarklet that uses the user's current URL, kind of like the tinyURL bookmarklet that uses this javascript code

javascript:void(location.href='http://tinyurl.com/create.php?url='+location.href)

So I copied that same thing and made

javascript:void(location.href='http://mywebsite.com/create.php?url='+location.href)

Then I use:

$url=$_GET['url']; 

to retrieve it. The problem is, if I am on a url that already has some get style info in the url, it messes everything up.

Example, If I am on:

http://www.google.ca/webhp?um=1&hl=en&safe=off

The '_GET' code sets $url to be

http://www.google.ca/webhp?um=1

So I think the info in google URL is messing up all of my URL parsing, I imagine I am doing something very incorrectly or someone has a very elegant solution for this. What should I do? please help

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
MikeD
  • 151
  • 1
  • 2
  • 7

5 Answers5

18

URL has a specified format. That part after ?, or to be more exactly between ? and # if exists, is called query string. It contains a list of key-value pairs - a variable name, = character and the value. Variables are separated by &:

key1=value1&key2=value2&key3=value3&key4=value4

You should escape location.href as it can contains some special characters like ?, & or #.

To escape string in JavaScript use encodeURIComponent() function like so:

location.href = "http://tinyurl.com/create.php?url=" + encodeURIComponent(location.href)

It will replace characters like & into %26. That sequence of characters isn't treated as a variable separator so it will be attached as a variable's value.

Crozin
  • 43,890
  • 13
  • 88
  • 135
  • Thanks, seems easy enough. What is the proper way to decode this in PHP then? – MikeD Aug 22 '10 at 05:01
  • 1
    You don't have to. `$_GET['url']` will contain the proper value, ie. `http://..../...html?abc=def&ghi=123`. However PHP comes just like JavaScript does with several functions for URL escaping: [`urlencode()`](http://pl.php.net/manual/en/function.urlencode.php). – Crozin Aug 22 '10 at 05:07
  • 1
    @MikeD I know it is a little bit old, but I think you should accept this answer :) – AbdelHady Aug 03 '13 at 02:17
1

Try

javascript:void(location.href='http://mywebsite.com/create.php?url='+encodeURIComponent(location.href));
Greg
  • 7,782
  • 7
  • 43
  • 69
0

To pass url in $_GET parameter like this:

http://yoursite.com/page.php?myurl=http://google.com/bla.php?sdsd=123&dsada=4323

then you need to use encode function:

echo 'http://yoursite.com/page.php?url='.urlencode($mylink);

//so, your output (url parameter) will get like this
//http://yoursite.com/page.php?url=http%3A%2F%2Fgoogle.com%2Flink.php%3Fname%3Dsta%26car%3Dsaab

after that, you need to decode the parameter with:

$variab = $_GET['url'];
$variab = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($variab)); 
$variab = html_entity_decode($variab,null,'UTF-8');
echo $variab;

such way, you can pass the correct link as a parameter.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
0
javascript:void(location.href='http://mywebsite.com/create.php?url='+encodeURIComponent(location.href))

You need to escape the characters.

Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
0

so what are you wanting, just the url without the query string?

$url = explode('?',$_GET['url']);
$url = $url[0];
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79