dsh125986083 2014-04-10 12:28
浏览 31
已采纳

使用Javascript在Window / Tab上运行PHP代码

I need to run this code:

<?PHP
  $pin=$_GET["pin"];
  unlink("../users/$pin/host.php");
  unlink("../users/$pin/votes.php");
  unlink("../users/$pin/guest.php");
  rmdir("../users/$pin");
  echo "Session ended";
?>  

Which is located at php/endsesh.php

Basically, when you start a session it creates you a folder with a pin number, and it places a host, guest and votes file.

I need this PHP script to run when the tab is closed, so it can delete all those files (Otherwise I'm just overloading my server with files)

So far I'm trying this with no luck:

<script>
window.onbeforeunload = function() {
    alert("<?php include("http://musicdemo.hol.es/php/endsesh.php?pin=" . $_GET["pin"]; ?>");
    $.get("http://musicdemo.hol.es/php/endsesh.php?pin=<?php echo $_GET["pin"]; ?>");
    return false;
    return "If you exit this page your session will not end. Please either allow the pop-up, by staying in this page and closing again, or click the link saying 'Close this session'";
}
</script> 

neither the alert or the $.get commands work.

  • 写回答

3条回答 默认 最新

  • doukezi4606 2014-04-10 12:47
    关注

    onbeforeunload is a highly secured, sandboxed event. It's really designed solely to catch people with "are you sure you want to close this window" messages, so I'm pretty sure it doesn't allow more advanced features such as an ajax call. To do so would open up security holes that allow malicious sites to prevent you from closing the window.

    I think doing an ajax call to clean up server side session files is not the appropriate strategy anyway, since the browser could easily crash, laptop could lose power, user could lose network connectivity preventing the ajax call etc. You can't rely on that ajax call succeeding.

    PHP already has very good session handling capabilities via session_start() and the $_SESSION global variable, which already has a built in cleanup feature for expiring old sessions.

    If you absolutely must keep your current solution, what I would do is run a cron job every hour, day, week, whatever, that searches for any of your files that haven't been accessed in say 24 hours and deletes them.

    Also I should note that taking a $_GET and passing it directly to unlink is one of the worst, most insecure things you can do in php. If you have that running on a server right now, you need to fix it immediately, since a malicious user could potentially do something like http://musicdemo.hol.es/php/endsesh.php?pin=../../../../../../etc/passwd. if 'pin' is a number, you should at least do something like:

    <?PHP
    $pin=$_GET["pin"];
    $pin = (int) $pin; // ensure pin is converted to an integer
    unlink("../users/$pin/host.php");
    unlink("../users/$pin/votes.php");
    unlink("../users/$pin/guest.php");
    rmdir("../users/$pin");
    echo "Session ended";
    ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮