dsfsfsfsfs55656 2016-10-20 08:08
浏览 155

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

I am sending a confirmation mail to user to confirm his account. The confirmation mail is styled using HTML and has an href element which points the users to a PHP file where I do the confirmation process. This href link also needs to have a PHP randomstring attached, the same randomstring is saved in Database and is also sent to user so that the cross-checking can be done in PHP once the user clicks on it.

  <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>

The PHP code includes the above HTML as follows.

<?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" . "
";
$headers .= "Content-type:text/html;charset=UTF-8" . "
";

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


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

?> 

However, the PHP variable is't getting available in the link.

  • 写回答

1条回答 默认 最新

  • dongyuedaochen8415 2016-10-20 09:23
    关注

    Please try this. Remove file_get_contents and use include with output buffer function. I think this will insert your variable to your template. Please check the below updated code. Anyway your method is not a secure. Please create more complicated random code

            <?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" . "
    ";
        $headers .= "Content-type:text/html;charset=UTF-8" . "
    ";
    
        // Additional headers
        $headers .= 'From: Alien creative control<alien@alien.com>' . "
    ";
    
    
        // Send email
        if(mail($to,$subject,$htmlContent,$headers)):
            $successMsg = 'Email has sent successfully.';
        else:
            $errorMsg = 'Some problem occurred, please try again.';
        endif;
    
        ?> 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答