PHP获取网址ID PHP获取当前页面url的参数
这几天有位仁兄找我来做奇迹的地图啊,我稍微看了一下页面,扒下来看了一下,发现他是通过PHP+JS的方式来查询每个点,从而输出不同的英雄信息。
网址就不放了,给各位看一下图片
页面左边很多小点,页面右边这个”点位信息“这块发现点击一个小点就出现不同的内容,一开始以为是iframe,但是点击右键之后发现不是框架,查看代码之后才发现是JS代码通过PHP的API来完成查询,返回代码实现的。
该页的JS核心代码如下确切来说是Jquery:
$(".npc_dian").click(function(){
var npcids = $(this).attr("data-npcid");
htmlobj=$.ajax({url:"/api/get_mapspotinfo.php?id="+npcids+'&t=1',async:false});
$("#map_potinfo").html(htmlobj.responseText);
$(this).siblings().removeClass("cthis");
$(".n_weizhi").remove();$(".n_weizhi_npc").remove();$(".n_weizhi_men").remove();
$(this).addClass('cthis');
$(this).append('<div class="n_weizhi_npc"></div>');
$("#mcont2").siblings().removeClass("layui-show");
$("#mcont2").addClass("layui-show");
$("#mcont2_tit").siblings().removeClass("layui-this");
$("#mcont2_tit").addClass("layui-this");
})
$(".men_dian").click(function(){
var npcids = $(this).attr("alt");
$("#map_potinfo").html(npcids);
$(this).siblings().removeClass("cthis");
$(".n_weizhi").remove();$(".n_weizhi_npc").remove();$(".n_weizhi_men").remove();
$(this).addClass('cthis');
$(this).append('<div class="n_weizhi_men"></div>');
$("#mcont2").siblings().removeClass("layui-show");
$("#mcont2").addClass("layui-show");
$("#mcont2_tit").siblings().removeClass("layui-this");
$("#mcont2_tit").addClass("layui-this");
})
$("#map_art .monster_dian").click(function(){
var npcids = $(this).attr("data-mmid");
htmlobj=$.ajax({url:"/api/get_mapspotinfo.php?id="+npcids+'&t=2',async:false});
$("#map_potinfo").html(htmlobj.responseText);
$(this).siblings().removeClass("cthis");
$(".n_weizhi").remove();$(".n_weizhi_npc").remove();$(".n_weizhi_men").remove();
$(this).addClass('cthis');
$(this).append('<div class="n_weizhi"></div>');
$("#mcont2").siblings().removeClass("layui-show");
$("#mcont2").addClass("layui-show");
$("#mcont2_tit").siblings().removeClass("layui-this");
$("#mcont2_tit").addClass("layui-this");
})
我一看,偶哟这个实现方法有点意思啊,顿时来了研究的兴趣。
看看这个不就是一点id值是变量吗,事实确实如此,通过点击圆圈返回不同的id值,来查询,查询到的是直接返回table代码输出到页面的右边。原本想直接调用他的api,但是发现调用不了,估计加了header判断,那么我们还是自己写一个api,免得到时候对面的接口挂了。
关于返回内容的id页面代码:
<table width="100%" border="1" cellpadding="0" cellspacing="0" class="">
<tbody><tr>
<td class="tdtit" style="width: 150px;"><div align="center" >区域说明</div></td>
<td ><div align="center"><span > 普通怪物区域</span></div></td>
</tr>
<tr>
<td class="tdtit"><div align="center" >怪物数量 </div></td>
<td><div align="center"><span >4</span></div></td>
</tr>
<tr>
<td class="tdtit" ><div align="center">元素属性</div></td>
<td ><div align="center"></div></td>
</tr>
<tr>
<td class="tdtit"><div align="center" >坐标</div></td>
<td ><div align="center">162, 200 </div></td>
</tr>
<tr>
<td class="tdtit"><div align="center" >推荐属性 </div></td>
<td ><div align="center"> </div></td>
</tr>
</tbody>
</table>
<br>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#7A7058" class="tablemu">
<tbody><tr>
<td colspan="2" class="tdtit"><div align="center" >怪物数据</div></td>
</tr>
<tr>
<td style="width: 150px;" ><div class="monster_lia" align="center"><img src="/source/monster/lorencia_02.jpg" ></div></td>
<td >
<div class="monster_liname tdtit"><a href="/monster_info.php?id=2" target="_blank">幼龙 </a> </div><br><span >等级: </span><span >4</span><span ><br>
HP:</span><span > 60 </span><br>
<span >攻击力:</span> <span >10~13</span><br>
<span >防御力:</span> <span >3</span><br>
<span >元素攻击力:</span> <span >0~0</span><br>
<span >元素防御力:</span> 2</span>
</td>
</tr>
<tr>
<td style="width: 150px;" ><div class="monster_lia" align="center"><img src="/source/monster/lorencia_05.jpg" ></div></td>
<td >
<div class="monster_liname tdtit"><a href="/monster_info.php?id=6" target="_blank">黑巫师 </a> </div><br><span >等级: </span><span >14</span><span ><br>
HP:</span><span > 255 </span><br>
<span >攻击力:</span> <span >41~46</span><br>
<span >防御力:</span> <span >14</span><br>
<span >元素攻击力:</span> <span >0~0</span><br>
<span >元素防御力:</span> 10</span>
</td>
</tr>
</tbody>
</table>
OK,那就很简单了,我们先来看看常用的获取网址参数的代码:
测试网址: http://localhost/index.php?id=9
//获取域名或主机地址
echo $_SERVER['HTTP_HOST']."<br>"; #localhost
//获取网页地址
echo $_SERVER['PHP_SELF']."<br>"; #/index.php
//获取网址参数
echo $_SERVER["QUERY_STRING"]."<br>"; #id=9
//获取用户代理
echo $_SERVER['HTTP_REFERER']."<br>";
//获取完整的url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/index.php?id=9
//包含端口号的完整url
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://localhost:80/index.php?id=9
//只取路径
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
echo dirname($url);
我们只需要获取网址id,那么只用到这行:
//获取网址参数
echo $_SERVER["QUERY_STRING"]."<br>"; #id=9
输出的内容是 id=9
任何来段判断代码:
<?php
#echo $_SERVER["QUERY_STRING"]."<br>"; id=5
$id=$_SERVER["QUERY_STRING"];
#echo $id;
if($id=="id=1")
{
$file1 = file_get_contents('./jieshao/1.html');
echo $file1;
}elseif ($id=="id=2") {
$file2 = file_get_contents('./jieshao/2.html');
echo $file2;
} else {
echo '没有这个英雄相关内容';
}
?>
咱们判断id为1的时候输出这个文件里面的代码,比如id为1,输出/jieshao/1.html里面的代码,id为2就是/jieshao/2.html的代码,以此类推。如果都不是,那么就输出 “没有这个英雄相关内容” 。
注意点,这里$_SERVER["QUERY_STRING"];获取的是连id这个字一起获取输出出来的,所以判断的时候要写成:$id=="id=1" 而不是 $id=="1"
Demo下载:
蓝奏云:https://ityh.lanzoui.com/iWdOfx12r0d?w
百度网盘: https://pan.baidu.com/s/1vy2oUmT__H4bT6KZUnXs8Q 提取码: rqvb
Demo只有地图最上面的那个点有内容