当前位置: 移动技术网 > IT编程>开发语言>PHP > php实现图形显示Ip地址的代码及注释

php实现图形显示Ip地址的代码及注释

2019年03月31日  | 移动技术网IT编程  | 我要评论

使用图形显示ip,文件 sunip.php

复制代码 代码如下:

<?php
header("content-type: image/gif");
$im = imagecreate(130,15);
$background_color = imagecolorallocate ($im, 255, 255, 255);
unset($ip);
if($_server['http_client_ip']){
$ip=$_server['http_client_ip'];
} else if($_server['http_x_forwarded_for']){
$ip=$_server['http_x_forwarded_for'];
} else{
$ip=$_server['remote_addr'];
} // www.jb51.net
$col = imagecolorallocate($im, 0, 51, 102);
imagestring($im, 3, 5, 1, $ip , $col);
imagegif($im);
imagedestroy($im);
?>

1. <?php

2. header("content-type: image/gif");
第二行 声明浏览器标头 输出为gif图形
3. $im = imagecreate(130,15);
建立一个图形 imagecreate(130,15)括号内130,15分别代表宽度和高度
4. $background_color = imagecolorallocate ($im, 255, 255, 255);
设置背景颜色 imagecolorallocate 为一幅图片分配颜色 ($im, 255, 255, 255)im代表前面提到的新建图形 后面的3个255则代表颜色表ffffff的10进制字符
5. unset($ip);
无用
6.if($_server['http_client_ip']){
$ip=$_server['http_client_ip'];
} else if($_server['http_x_forwarded_for']){
$ip=$_server['http_x_forwarded_for'];
} else{
$ip=$_server['remote_addr'];
}
如果$_server['http_client_ip']可以使用则使用$_server['http_client_ip']下面类似 为判断 此段是为了兼容多种服务器设置
7. $col = imagecolorallocate($im, 0, 51, 102);
定义文字颜色
8. imagestring($im, 3, 5, 1, $ip , $col);
将获取到的ip画到新建的画布上 imagestring($im, 3, 5, 1, $ip , $col); 分别代表imagestring(图形表示,字符尺寸1-5,x坐标,y坐标,输出的ip,颜色)
9. imagegif($im);
输出gif图形
10. imagedestroy($im);
释放内存
11. ?>
程序结束

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网