-3

一个很好的例子就是脸书。在有人评论另一个人之后,所有人都可以看到更新弹出,而不仅仅是您自己的页面并且不需要刷新。我正在制作一个监控或过滤掉不良评论的项目。所以如果插入了不良评论数据库然后它给我通知并弹出数据..请任何帮助..谢谢我使用php作为服务器端语言。

4

4 回答 4

1

是的,总会有办法的。您可以使用 ajax 和 php 使用彗星编程和长轮询。

好的缺点是使用彗星你的其他http请求将是霍尔特所以对于彗星你必须需要重写最好的会话。

根据您的问题,我建议您使用 myisam 引擎,因为它会跟踪上次修改数据库时的时间戳,因此您将知道数据库何时更新。

示例彗星代码如下。

索引.php

<div id="content"></div>

<p id="form_container">
    <form action="" method="get" onsubmit="comet.doRequest($('#word').val());$('#word').val('');return false;">
        <input type="text" name="word" id="word" value="" />
        <input type="submit" name="submit" value="Send" />
    </form>
</p>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">

    var Comet = function (data_url)
    {
        this.timestamp = 0;
        this.url = data_url;  
        this.noerror = true;

        this.connect = function()
        {
            var self = this;

            $.ajax(
            {
                type : 'post',
                url : this.url,
                dataType : 'json', 
                data :
                {
                    'timestamp' : self.timestamp
                },
                success : function(response)
                {
                    self.timestamp = response.timestamp;
                    self.handleResponse(response);
                    self.noerror = true;          
                },
                complete : function(response)
                {
                    if (!self.noerror)
                    {
                        setTimeout(function(){ comet.connect(); }, 5000);           
                    }
                    else
                    {
                        self.connect(); 
                    }
                    self.noerror = false; 
                }
            });
        }

        this.disconnect = function() {}

        this.handleResponse = function(response)
        {
            $('#content').append('<div>' + response.msg + '</div>');
        }

        this.doRequest = function(request) {
            $.ajax(
            {
                type : 'post',
                url : this.url,
                data :
                {
                    'msg' : request
                }
            });
        }
    }

    var comet = new Comet('./backend.php');
    comet.connect();
</script>

后端.php

function get_date()
{
    $query = mysql_query("show table status from weefavr like 'request_responses'")or die(mysql_error());

    $row = array();
    while($result = mysql_fetch_assoc($query))
    {
        $row[] = $result;
    }
    return strtotime($row[0]['Update_time']);
}

$lastmodif    = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
$currentmodif = get_date();

while ($currentmodif <= $lastmodif)
{
    usleep(10000);
    clearstatcache();
    $currentmodif = get_date();
}

$query_db = mysql_query("SELECT UNIX_TIMESTAMP(last_update.created) AS DATE,last_update.* FROM last_update WHERE UNIX_TIMESTAMP(last_update.created) BETWEEN '".$lastmodif."' AND '".$currentmodif."'")or die(mysql_error());

$row_db = array();
$response = array();
while($result_db = mysql_fetch_assoc($query_db))
{
    $response['msg'][]       = $result_db['title'];
}

$response['timestamp'] = $currentmodif;
$response['lastmodif'] = $lastmodif;
echo json_encode($response);
flush();

这个例子很有帮助,但正如我告诉你的那样,你的其他 HTTP 请求将不起作用。所以你需要对会话等做一些修改。

是的,Facebook、g mail、asana 和 zaarly 都是使用彗星编程的巨头。

希望这会有所帮助。随意问干杯...

于 2013-01-03T09:52:38.193 回答
0

您需要从您的页面发出 AJAX 请求来实现这一点。

当用户执行操作时,您可以提交 ajax 请求并动态更新该用户的页面。其他人也会看到这一点。

于 2013-01-03T09:46:12.337 回答
0

这里有一个不错的 PHP/MySQL/AJAX 教程和下载,应该可以帮助您入门。将此与您的服务器端过滤逻辑和定期 ajax 回调相结合,以保持每个人的页面更新,您应该能够实现您正在寻找的内容。

于 2013-01-03T09:48:07.490 回答
0

它将是 PHP 和 Ajax。如果您需要它,这里有一个基本介绍。基本上,您需要有一个网页,通过 Javascript 异步轮询您的后端(PHP 页面)来提供新内容。你可以有一个 Javascript 计时器,或者更复杂的设置,比如后端“推送”到你的页面。我建议您查看此其他页面以获取示例。

于 2013-01-03T10:00:13.823 回答