duanfei7508 2012-05-02 20:15
浏览 37
已采纳

想要在退出时与服务器连接。 我可以设置任何形式的Javascript / jQuery / AJAX陷阱吗?

First things first. I'm running CodeIgniter on a PHP/MySQL site.

I've been tasked with creating a "shell" application for a series of movies that a person can view for a training website that I'm building. The idea here is that a person would be able to log on to a page, click "Take Course" and have it log what time the course was taken. Then, when the person exits the page carrying the movie, I would be able to record what time the course was ended. This way I can compare the start and end times, and determine if the user had viewed most of the film in order to give the user credit. Well, the first part is easy. First, I've built a table in my database with the following fields:

  • intKey (intKey(10))
  • strHash (varchar(255))
  • dtBegan (datetime)
  • dtEnded (datetime)
  • varIpAddress (varchar(255))

I've put in a controller that does this:

$ip_address = $_SERVER['REMOTE_ADDR'];
$data['hash'] = md5(time() . "Proof of Concept!");
$this->db->query("INSERT INTO pocTime SET strHash = '" . $data['hash'] . "', dtBegan = now(), varIpAddress='$ip_address'");
$this->load->view('welcome_message',$data);

OK... easy enough, yes? I also know that when I am done, I want to launch a file that does this:

$ip_address = $_SERVER['REMOTE_ADDR'];
$this->db->query("UPDATE pocTime SET dtEnded = now() WHERE strHash = '" . $data['hash'] . "' AND varIpAddress='$ip_address'");

What I'm hoping to do is have a page that is called by the 1st block of code, and set a "trap" so to speak so that the 2nd block of code is run by some kind of "post back" if the rendered page is closed. Any suggestions? I know that I could put some sort of big "CLICK HERE TO END YOUR VIDEO" type button that would cover this, but I'm looking to run code on some form of exit from a page, because I don't know what the user would do after viewing the video. Any thoughts?

  • 写回答

3条回答 默认 最新

  • dou6495 2012-05-02 20:19
    关注

    Simple but should work, using jQuery

    $(window).unload(function(){
        $.get("test.php?dim1=turn&dim2=off");
    });
    

    Using unload which jQuery documentation tells what triggers this: http://api.jquery.com/unload/

    Using get which jQuery documentation is here: http://api.jquery.com/get/

    Or you can use post as well

    $(window).unload(function(){ 
        $.post("test.php", { dim1: "turn", dim2: "off" } ); 
    });
    

    http://api.jquery.com/jQuery.post/

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器