将PHP变量插入HTML中的href链接

时间:2016-10-20 08:08:44

标签: php html echo

我正在向用户发送确认邮件以确认其帐户。确认邮件使用HTML进行样式设置,并且有一个href元素,用于将用户指向PHP文件,我在该文件中执行确认过程。这个href链接还需要附加一个PHP randomstring,相同的randomstring保存在数据库中,也发送给用户,这样一旦用户点击它就可以在PHP中完成交叉检查。

  <td align="center" style="margin:0;text-align:center">
    <a href="http://aliencreative.wehubs.com/new_alien/email.php?code=<?php echo $randomString; ?>" 
    style="font-size:21px;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;
    border-radius:2px;background-color:#0096d3;padding:14px 40px;display:block;
    letter-spacing:1.2px" target="_blank">Confirm Alien account now!</a></td>

PHP代码包括以下HTML。

<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;

echo $random;
$to = 'sample@gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
$htmlContent = file_get_contents("email_template.php");

// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// Additional headers
$headers .= 'From: Alien creative control<alien@alien.com>' . "\r\n";


// Send email
if(mail($to,$subject,$htmlContent,$headers)):
    $successMsg = 'Email has sent successfully.';
else:
    $errorMsg = 'Some problem occurred, please try again.';
endif;

?> 

但是,PHP变量在链接中不可用。

1 个答案:

答案 0 :(得分:0)

请试试这个。删除file_get_contents并使用带有输出缓冲区功能的include。我认为这会将您的变量插入到模板中。请检查以下更新的代码。无论如何,你的方法并不安全。请创建更复杂的随机代码

        <?php
    $randomString=time();
    //$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;

    echo $random;
    $to = 'sample@gmail.com';
    $subject = "Confirmation mail";
    // Get HTML contents from file

    ob_start();
    include "email_template.php";
    $htmlContent=ob_get_contents();
    ob_end_clean();

    // Set content-type for sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // Additional headers
    $headers .= 'From: Alien creative control<alien@alien.com>' . "\r\n";


    // Send email
    if(mail($to,$subject,$htmlContent,$headers)):
        $successMsg = 'Email has sent successfully.';
    else:
        $errorMsg = 'Some problem occurred, please try again.';
    endif;

    ?>