What does "& key" mean in PHP?

I have a very strange problem. I use PHP, in my PHP code I wrote the content of the email and created 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> .... 

In most cases, it works fine. Then I get a lot of complaints saying that they cannot be activated. After checking their emails, I found this:

 <a href="http://www.domain.com/act.php?id=20090=hsdf87hsf89sd">http://www.domain.com/act.php?id=20090=hsdf87hsf89sd'</a> 

Missing "& key". What for? Very strange mistake !!!

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 may be right, my email content is HTML based:

 $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n"; 
+4
source share
4 answers

You need to encode the & character with a symbolic link , such as &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>' 
+7
source

&key used as a string, for example. echo '&key'; doesn’t mean anything special - it’s just a normal line. Could you insert all the code?

The problem may be that the & character is a special character in HTML.

0
source

Insert the key first:

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

Gumbo is right about encoding & like & in an HTML document. You can let PHP http_build_query () do all the encoding needed 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; 

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

0
source

Source: https://habr.com/ru/post/1299668/


All Articles