douzhi2988 2011-07-24 20:35
浏览 76
已采纳

将html / php代码存储到php变量中

What is the best/safest way to store html mark up with php code in it into a php variable? Or is there a better solution rather than storing it into a variable?

EDIT: Sorry for not including what I am trying to do! I have a an article template that pulls all information (title/date/content/etc) from a database and loads it. I have a page where you submit an article and what I am trying to do is automate the file creation process. I want to create a file that is named the title of the article, then write the template code to it (hence the variable containing the template code and using fwrite()). I know I could just keep the template file on the website and copy it over/rename it, but if someone stumbles upon the template it is a complete mess, and I don't want to store it in plain text either.

  • 写回答

4条回答 默认 最新

  • doufei6456 2011-07-24 21:37
    关注

    If you're working with a public website, I would not recommend storing things like articles in files on your server. It's messy, security-iffy, memory-inefficient, and otherwise unorthodox.

    This would probably be a great situation in which to use a MySQL database. I'll assume you know how to work with one using PHP, but let me know if you don't know how.

    In this way, you could store the HTML in the database using PHP's htmlentities() function:

    $var = htmlentities($var, ENT_QUOTES);
    

    This way, all html characters (ex. "<", ">" and quotes as well) are encoded into the database safely. For example,

    <strong>Here is HTML</strong> becomes &lt;strong&gt;Here is HTML&lt;/strong&gt;

    That way, if your templates have any HTML, they can be easily and safely retrieved through mysql_query() and displayed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog