duanna2026 2012-08-18 19:04
浏览 364
已采纳

用php删除<script>和其他标签

For a small one page CMS I want to replace script and other tags that people could use for bad intentions.

I've tried out strip_tags and preg_replace but it's not working for some reason.

The one page CMS has 6 fields to edit. Those are saved in a plain text file. When I edit one of those, I need it to remove all tags like script, embed, object, <iframe> and others.

I've checked out HTML Purifier, but I don't get it how this should work as I'm not well known with php. Looks a bit too big for my needs I guess.

This is the code (here I try to remove tags from the script tag from text area named newscontent):

<?php
if (isset($_POST['edit'])) {

    $newscontent = preg_replace('/<script.+?<\/script>/im', '', $newscontent);

    if (file_put_contents('title.txt',          utf8_encode($_POST['title']))       !== FALSE &&
        file_put_contents('subtitle.txt',       utf8_encode($_POST['subtitle']))    !== FALSE &&
        file_put_contents('datum.txt',          utf8_encode($_POST['datum']))       !== FALSE &&
        file_put_contents('time.txt',           utf8_encode($_POST['time']))        !== FALSE &&
        file_put_contents('timemin.txt',        utf8_encode($_POST['timemin']))     !== FALSE &&
        file_put_contents('newscontent.txt',    utf8_encode($_POST['newscontent'])) !== FALSE
    )
        echo '<p class="succes">Your changes are saved</p>', "
";
}
$title          = utf8_decode(file_get_contents('title.txt'));
$subtitle       = utf8_decode(file_get_contents('subtitle.txt'));
$datum          = utf8_decode(file_get_contents('datum.txt'));
$time           = utf8_decode(file_get_contents('time.txt'));
$timemin        = utf8_decode(file_get_contents('timemin.txt'));
$newscontent    = utf8_decode(file_get_contents('newscontent.txt'));
?>
  • 写回答

2条回答 默认 最新

  • dongyong1400 2012-08-18 19:12
    关注

    Your code doesn't work because you are performing the replacement on the variable $newscontent, but writing $_POST['newscontent'] to the file. I guess you have register globals switched on (which is bad) or this would generate an error.

    I would recommend you persevere with HTMLPurifier. There are many, many bad things people could add to text if they have 'bad intentions', and your approach does not even scratch the surface. For example, if you were to fix your code, it doesn't prevent people adding something like this:

    <img src="http://www.google.com/logo.gif" onload="javascript:bad stuff here" />
    

    not to mention the complications of different character sets.

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

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等