我需要一些关于如何最好地处理代码以保持有效运行同时仍然向用户返回动态字符串的建议。
在我的特定情况下,我有一组字符串响应,它们根据一组六个变量而变化。在项目早期,只需使用嵌套的if和switch语句就很容易。但是,随着项目的进展,这组代码变得越来越庞大。我非常想找到一种方法将这些信息存储在数据库中,但我不知道这样做的方法是不会将数据库打开到漏洞中。
示例脚本可能如下所示:
printf("Let's say for a moment here, that I want to have a script that is something like this where, ". $yourname ." is printed as well as other pieces of information stored from a database such as: ".$yourfavoritefood." or ".$timesinceloggedon.". Clearly this output will be different from person to person.");
现在想象一下,我有数百个这些变量驱动的脚本。有没有办法在mysql数据库中安全存储脚本,以便我只能查询当时给出程序驱动变量所需的脚本?
答案 0 :(得分:0)
您可以将您创建的文本存储在数据库或文件中,并使用带参数的printf输出文本http://php.net/manual/en/function.printf.php,
printf("Let's say for a moment here, that I want to have a script that is something like this where, :s is printed as well as other pieces of information stored from a database such as: :s or :s. Clearly this output will be different from person to person.", [$yourname, $yourfavoritefood, $timesinceloggedon);
通过将不同用户的所有不同部分纳入参数,您可以找到一个可以存储翻译的常用字符串