douxin8383 2014-01-21 15:29
浏览 13
已采纳

PHP表单以html格式提交。 总是负荷

I am trying to rename a html when i click on submit button. the problem is that it will rename the page while the page loads even thu i did not click on the submit button, is there any to avoid that. i wan it to load only on submit?

   <form method="post" action="<?php rename("log.html","OLD.LOG/log.html.bac.date");  $file=fopen("log.html","w+"); ?>">
            <input name="Rename" type="submit"  id="Rename" value="Clear Page" >
            <p class="logout"><a id="exit" href="#">Exit</a></p>
            </form></p>
  • 写回答

2条回答 默认 最新

  • doutuanxiao4619 2014-01-21 16:04
    关注

    If I understood your question correctly, you have a form and clicking of the "Rename" button should rename "log.html" to "OLD.LOG/log.html.bac.date" in the PHP server. I strongly discourage letting client side decide the name of the renamed file. With your current approach, the client side (browser) sends details of the renaming operation. This could be a security loop hole using which an attacker could rename an important file in the server.

    Assuming that you are doing a simple form submit (no fancy ajax required), change the action to point to a php file in the server.

       <form method="post" action="/rename">
            <input name="Rename" type="submit"  id="Rename" value="Clear Page" >
            <p class="logout"><a id="exit" href="#">Exit</a></p>
       </form></p>
    

    I assumed that you have setup clean URLs with some bootstrap/front controller to execute rename.php in the PHP server otherwise, use

    <form method="post" action="/rename.php">
    

    then in rename.php have code similar to the following

    <?php 
    rename("log.html","OLD.LOG/log.html.bac" . time());  
    $fh = fopen("log.html","w"); 
    fclose($fh);
    ?>
    

    I added the time() to prevent the back up file being overridden every single time. And always close the file handlers you open. I also assumed that you only want to create the empty log.html, and not to read it. so w is enough, w+ is not required. Also, I assume that you have some authentication/authorization measures in place to prevent a DOS attack on the server by executing the rename operation too many.

    If you want to use the same PHP file that outputs the form then pass an extra parameter in the POST request to the same URL. There are many ways you can do that, the simplest being that. But you can do it properly using proper HTTP headers, but having the same URL for displaying the form as well as for processing the form. There are many MVC frameworks like Zend framework which are capable of routing to different actions in the same controller by inspecting the HTTP method (GET, POST, DELETE, ect)

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line