views:

68

answers:

2

I am trying to output a simple link.

This works great:

$url = 'http://www.google.com';
echo $url;

This doesn't work great:

$url = 'http://www.google.com';
echo urlencode($url);

The second example tacks on "?SID=xxx" to the end of the URL for some reason. How do I prevent this from happening?

Note: The code to generate the URL has been changed to protect the innocent.

+2  A: 

This is not urlencode()s fault, it's PHP's automatic link rewriting that adds the session ID through a GET variable in absence of a session cookie.

I'm afraid this is necessary to persist sessions on the client side if they have cookies disabled.

The setting to do this automatically is session.use_trans_sid. More info here.

Pekka
+2  A: 

Don't use urlencode() to encode URL, you will end up an URL like this,

http%3A%2F%2Fwww.google.com

To PHP, this looks like a relative URL so it appends SID when cookie is missing.

urlencode() should be used to encode query string parameters but not the URL itself.

ZZ Coder
That's the trick. This IS a query parameter for a facebook like button.
retailevolved