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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀