tags:

views:

140

answers:

4

I have a very weird problem. I am using php. In my php code, I wrote the email content and generate this link:

....
<a href="http://www.domain.com/act.php?id=' . $userid . '&key=' . $actkey . '">http://www.domain.com/act.php?id=' . $userid . '&key=' . $actkey . '</a>
....

Most of the times, it works fine. Then, I receive lots of complaints saying they cant activate, after checking their emails i found this:

<a href="http://www.domain.com/act.php?id=20090=hsdf87hsf89sd"&gt;http://www.domain.com/act.php?id=20090=hsdf87hsf89sd'&lt;/a&gt;    

The "&key" is missing. Anyone know WHY? Very weird bug!!!

EDIT:

The full PHP command:

$content = '<div style="font-family:Calibri; color:#333;">
    Hi there, <br><br>

    Thank you for register to our website, click the following link to activate your account:<br><br> 

<a href="http://www.domain.com/act.php?id=' . $userid . '&key=' . $actkey . '">http://www.domain.com/act.php?id=' . $userid . '&key=' . $actkey . '</a><br><br>

XXX Team</div>';

Gumbo could be right, my email content is html based:

$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
+6  A: 

You need to encode the & character with a character reference like &amp;:

'<a href="http://www.domain.com/act.php?id=' . $userid . '&amp;key=' . $actkey . '">http://www.domain.com/act.php?id=' . $userid . '&amp;key=' . $actkey . '</a>'

Or better:

'<a href="' . htmlspecialchars('http://www.domain.com/act.php?id=' . $userid . '&key=' . $actkey) . '">' . htmlspecialchars('http://www.domain.com/act.php?id=' . $userid . '&key=' . $actkey) . '</a>'
Gumbo
Pekka
Erik
Gumbo
Erik
@Erik: It may work for *key*. But it is invalid and may not work for other names.
Gumbo
Yep, its invalid; I just read the specs and tried it in a validator - I've used it wrong literally 100's of times without error, nice to know =x
Erik
phpnoob1001
A: 

&key used as string, eg. echo '&key';, doesn't mean anything special - it's just a normal string. Could you paste your whole code?

The problem might be that & character is special character in HTML.

Crozin
Probably u r right. My email content is "html"
phpnoob1001
A: 

Put key in the first place:

<a href="http://www.domain.com/act.php?key=' . $actkey . '&id=' . $userid . '">http://www.domain.com/act.php?key=' . $actkey . '&id=' . $userid . '</a>
Pentium10
A: 

Gumbo is right about encoding & as &amp; within an html document.
You can let php's http_build_query() do all the encoding necessary to build a valid query string.

/* testdata: */ $userid=12; $actkey='abc&def';
$params = http_build_query(array(
  'id'=>$userid,
  'key'=>$actkey
), '', '&amp;');
$url = 'http://www.domain.com/act.php?'.$params;
echo $url;

prints http://www.domain.com/act.php?id=12&amp;amp;key=abc%26def

VolkerK
Thanks for the answer. Very appreciated =)
phpnoob1001