简体   繁体   English

没有解析存储在MySQL中的PHP变量

[英]PHP variables stored in MySQL are not getting parsed

Good Day, 美好的一天,

I have got a problem with my script. 我的脚本有问题。 What I'm doing is: 我正在做的是:

I want to send an email to user. 我想向用户发送电子邮件。 There are different templates for different type of mails like one design for SignUp confirmation, one for recent news etc. So, what I did is, I created a table and stored each design in column called content with its complete HTML code. 对于不同类型的邮件,有不同的模板,例如一种用于SignUp确认的设计,一种用于近期新闻的设计等。因此,我要做的是,创建了一个表格,并将每种设计都存储在名为content的列中,并带有完整的HTML代码。

My database design is pretty simple for this purpose: 为此,我的数据库设计非常简单:

ID    |    type    |    content

1     |   signup   |    <html> ... <body> .. content of email ... </body></html>

So, right above is my database. 因此,上面就是我的数据库。 field with ID 1 Now, when someone signs up, I am trying to send him an email whose content is fetched from table above. ID为1的字段现在,当有人注册时,我正尝试向他发送一封电子邮件,该邮件的内容是从上表中获取的。 Text under content field is like: 内容字段下的文本如下:

<html>
.
.
<body>
Good Day,

You have successfully created your account.
Please activate your account now by clicking below:

<a href="activate.php?md5=$md5&code=$code">ACTIVATE</a>

</body>
</html>

In above code, if you checkout the activate link, I have used PHP variables $md5 and $code, thinking that their corresponding values would be printed in actual email. 在上面的代码中,如果签出激活链接,则我使用了PHP变量$ md5和$ code,以为它们的相应值将打印在实际的电子邮件中。 But they are printed as it is, like $md5 and $code . 但是它们按原样打印,如$ md5$ code

Well, I did little research and from few forums, I got to know that, 好吧,我进行了很少的研究,并且从很少的论坛中得知,

1st: While we added content of HTML template to database along with these PHP variables, they have become a value of that database column itself and would not be treated as PHP variables anymore. 第一个:虽然我们将HTML模板的内容以及这些PHP变量一起添加到了数据库中,但它们已成为该数据库列本身的值,不再被视为PHP变量。

2nd: One person claims that he have made it's working using eval() function. 第二个:一个人声称他已经使用eval()函数使其正常工作。 I can use eval since, im not taking any value from user but database, from point of security. 我可以使用eval,因为从安全角度来看,即时通讯不会从用户那里获取任何价值,而是从数据库中获取任何价值。 I tried it, but still nothing. 我试过了,但还是没有。

Below is code which I tried with eval(): 下面是我尝试使用eval()的代码:

$md5    =   $row['md5'];  // These variables values I'm expecting to come in template content
$code   =   $row['activ_code'];  // These variables values I'm expecting to come in template content

ob_start();
eval("\$template_content = \"$template_content\";");
$message    =   $template_content;
ob_end_clean();

Can someone help me out with this? 有人可以帮我这个忙吗? I don't want to put complete template (HTML) on the same page where I'm using the mail function. 我不想将完整的模板(HTML)放在使用邮件功能的同一页面上。 It makes code look ugly instead of keeping it short and neat. 它使代码看起来很丑陋,而不是使其简洁明了。

Please help! 请帮忙!

Use str_replace http://php.net/manual/en/function.str-replace.php and replace those variables to tags, ie something like %MD5%. 使用str_replace http://php.net/manual/en/function.str-replace.php并将这些变量替换为标签,例如%MD5%。 You could still use $code but it is just no this readable and may result in wrong text being substituded (consider a string "this was $codeveloped") 您仍然可以使用$ code,但是它不是可读性强的,并且可能导致错误的文本被降级(考虑字符串“ this is $ codeveloped”)

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) This function returns a string or an array with all occurrences of search in subject replaced with the given replace value. 混合str_replace(混合$ search,混合$ replace,混合$ subject [,int&$ count])此函数返回一个字符串或一个数组,其中主题中所有出现的搜索都替换为给定的替换值。

Then, 然后,

$template_content = '<a href="activate.php?md5=%MD5%&code=%CODE%">ACTIVATE</a>';
$template_content = str_replace("%MD5%", $md5, $template_content);
$template_content = str_replace("%CODE%", $code, $template_content);

str_replace also accepts arrays as arguments, so this may be a better one str_replace也接受数组作为参数,因此这可能是一个更好的选择

$placeholders = array("%MD5%", "%CODE%");
$values = array($md5, $code);
$template_content = str_replace($placeholders, $values, $template_content);

Eval is a potentially dangerous and generally an overkill 评估是一个潜在的危险,通常是一个过大的杀伤力

You could try something like this instead. 您可以尝试这样的方法。

$md5    =   $row['md5'];  // These variables values I'm expecting to come in template content
$code   =   $row['activ_code'];  // These variables values I'm expecting to come in template content

$message = str_replace( array('$md5','$code'), array($row['md5'], $row['activ_code']),  $template_content);

Of course it might make better sense and readability to make the fields that need replacing look like this instead of using $xx like variable names. 当然,使需要替换的字段看起来像这样而不是像变量名那样使用$ xx可能更有意义和更易读。

$template_content = '..... {md5code}..... {activecode}...';
$message = str_replace( array('{md5code}','{activecode}'), array($row['md5'], $row['activ_code']),  $template_content);

Just a suggestion. 只是一个建议。

Your problem is the double quotes in the text you are evaluating. 您的问题是您要评估的文本中的双引号。

To get it working, you must escape them: 要使其正常工作,您必须使它们逃脱:

eval("\$template_content = \"" . addslashes($template_content) . "\";");
$message = $template_content;

Then it will work just fine. 然后它将正常工作。

Here is a complete working example: 这是一个完整的工作示例:

$template_content = '<html>
.
.
<body>
Good Day,

You have successfully created your account.
Please activate your account now by clicking below:

<a href="activate.php?md5=$md5&code=$code">ACTIVATE</a>

</body>
</html>';


$md5    =   "HEREGOESMD5";
$code   =   "CodeCodeCodeCode";

eval("\$template_content = \"" . addslashes($template_content) . "\";");
$message = $template_content;

echo $message;
<php
echo '<a href="activate.php?md5='.$md5.'&code='.$code.'">ACTIVATE</a>';
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM