<pre name="code" class="php"><h2><span style="white-space:pre"> </span>HTML页面</h2><pre name="code" class="php"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>搜索关键字</title>
</head>
<body>
<center>
关键字:<input type="text" name="sou">
<button>搜索</button>
<div></div>
</center>
</body>
</html>
<script type="text/javascript" src="../public/jquery.js"></script>
<script type="text/javascript">
$(function(){
$("button").click(function(){
var sou = $("input").val();
var url = "a.php";
var data = {'sou':sou};
var str='';
$.get(url,data,function(msg){
if(msg)
{
for(var i=0;i<msg.length;i++)
{
str += '<p>'+msg[i]+'</p>';
}
$("div").html(str);
}
},'json');
});
});
</script>
PHP页面
<?php
header("content-type:text/html;charset=utf-8");
//接收参数
$sou = $_GET['sou'];
//次数达到10次 $mem->set($sou,$num,0,0);
//取出次数 $mem->get($sou);
$mem = new Memcache();
$mem->connect('www.liuyang.com',11211);
//清空数据
// $mem->flush();die;
$num = $mem->get($sou);
//判断是否有热词已经存在memcache中
if($num)
{
$num++;
$mem->set($sou,$num,0,0);
if($num>=3)
{
//说明该搜索条件具备作为热词的资格
//热词的存储方式应该是 $mem->set('hot',$data,0,0);
//把新的热词放入到原来的数组中
$data = $mem->get('hot');
//判断热词是否已经存在memcache中
if(!in_array($sou, $data))
{
$data[] = $sou;
//重新存储
$mem->set('hot',$data,0,0);
}
$data = $mem->get('hot');
echo json_encode($data);
}
else
{
//返回其他的热词
$data = $mem->get('hot');
if($data)
{
echo json_encode($data);
}
}
}
else
{
$mem->set($sou,1,0,0);
//返回其他的热词
$data = $mem->get('hot');
if($data)
{
echo json_encode($data);
}
}