存档

2014年12月 的存档

【免费】比较不错的php获取ip地址,城市,国家源文件

2014年12月29日 没有评论

发现比较不错的php获取ip地址,城市,国家源文件,如下

http://dev.maxmind.com/zh-hans/geoip/legacy/geolite/

php官方介绍:http://php.net/manual/zh/book.geoip.php

 

需要注意的是maxmind是geoip数据库的提供方,

同时也提供了很多语言的sample和api说明文档。

比如php,和php的geoip库是有很大区别的,包含使用方式,接口函数。

php官方的geoip需要配置php环境,加载geoip.dll库,并在php.ini中指定GEOIP库地址。

maxmind提供一系列 .inc 和 .php 对环境无依赖,只要支持php,直接require后即可使用。

一、GEOIP数据库

http://dev.maxmind.com/geoip/geolite

细分到国家:GeoLite Country
细分到城市:GeoLite City

二、php官方geoip.dll库

下载dll http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/
修改php.ini,启用geoip库:

1
extension=php_geoip.dll

追加geoip段,指定数据库位置:

1
2
[geoip]
geoip.custom_directory = "D:\php5.3\geo\"

测试代码
注意geoip.dll中使用的 GeoIPCity.dat 即 GeoLiteCity.dat,用的时候注意看提示

1
2
3
4
echo geoip_country_name_by_name( "8.8.8.8" ) . "\n";
print_r( geoip_record_by_name( "8.8.8.8" ) );
echo geoip_country_name_by_name( "61.139.2.69" ). "\n";
print_r( geoip_record_by_name(  "61.139.2.69" ) );

三、MaxMind官方php文件函数库

文档和示例:http://dev.maxmind.com/geoip/downloadable

修改maxmind示例中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路径为你自己的路径
同一目录用 “./GeoIP.dat” 或 “./GeoLiteCity.dat” 即可。
详细到国家

1
2
3
4
5
include("geoip.inc");
$gi = geoip_open( "./GeoIP.dat", GEOIP_STANDARD );
echo geoip_country_code_by_addr($gi, "8.8.8.8") . "\t" . geoip_country_name_by_addr($gi, "8.8.8.8") . "\n";
echo geoip_country_code_by_addr($gi, "61.139.2.69") . "\t" . geoip_country_name_by_addr($gi, "61.139.2.69") . "\n";
geoip_close($gi);

详细到国家城市

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("./GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,"8.8.8.8");
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n";
print $record->city . "\n";
print $record->postal_code . "\n";
print $record->latitude . "\n";
print $record->longitude . "\n";
print $record->metro_code . "\n";
print $record->area_code . "\n";
print $record->continent_code . "\n";
print "\n-----\n";
$record = geoip_record_by_addr($gi,"61.139.2.69");
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n";
print $record->city . "\n";
print $record->postal_code . "\n";
print $record->latitude . "\n";
print $record->longitude . "\n";
print $record->metro_code . "\n";
print $record->area_code . "\n";
print $record->continent_code . "\n";
geoip_close($gi);

看自己开发环境和具体情况决定用哪种。

 

———————————

自己写的简单php代码:

<?php
require(‘geoip.inc’);
$gi=geoip_open(‘geoip_country.dat’,GEOIP_STANDARD);
function getip($isInt=0){
$ip=FALSE;
if(!empty($_SERVER[‘HTTP_CLIENT_IP’])){$ip=$_SERVER[‘HTTP_CLIENT_IP’];}
if(!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])){
$ips=explode(‘, ‘,$_SERVER[‘HTTP_X_FORWARDED_FOR’]);
if($ip){array_unshift($ips,$ip);$ip=FALSE;}
for($i=0;$i<count($ips);$i++) {if(!eregi(“^(10|172\.16|192\.168)\.”,$ips[$i])){$ip=$ips[$i];break;}}
}
$ip=$ip?$ip:$_SERVER[‘REMOTE_ADDR’];
return $isInt?bindec(decbin(ip2long($ip))):$ip;
}
$country=geoip_country_code_by_addr($gi,getip());
geoip_close($gi);
?>

分类: php学习 标签: , ,

discuz论坛地址栏带端口号的解决方法【nginx+apache验证码不显示】

2014年12月24日 没有评论

我们往往会使用nginx+apache来作为服务器环境,nginx使用80端口,Apache使用82端口,那么当我们安装discuz的时候,在加入了nginx映射到Apache端口后,往往discuz的链接地址栏上会出现端口号,如82

那么我们如何解决这个问题呢?

x2寻找文件source/class/class_core.php,寻找:$_G[‘siteport’]

将此行注释即可,或者强制在此文件的头部,加上:$_SERVER[‘HTTP_HOST’]=”www.域名.com”;

到此就可以解决discuz论坛地址栏带端口号的解决方法【nginx+apache反射】,但是x2.5是修改:

source/class/discuz/discuz_application.php

————————–

但是你会发现另外一个问题,就是验证码不能正确显示,解决办法如下:

直接在config/config_global.php下增加$_SERVER[‘HTTP_HOST’]=”www.域名.com”;即可解决所有问题

研究半天实在搞不定,原来还有此绝招~

 

php生成验证码类【还不错】

2014年12月11日 没有评论

<?php
session_start();
header(‘Content-type:image/png’);
$serverTime = $_SERVER[‘REQUEST_TIME’];
if ($_SESSION[‘confirmCode’][‘errorTimes’] > 8 && $serverTime – $_SESSION[‘confirmCode’][‘time’] < 20) {
$_SESSION[‘confirmCode’][‘code’] = ‘whatabadman!’;
$img_file = ‘yzmBad.gif’;
$fp = fopen($img_file, ‘rb’);
$content = fread($fp, filesize($img_file));
fclose($fp);
echo $content;
} else {
if ($_SESSION[‘confirmCode’][‘errorTimes’] > 8) {
$_SESSION[‘confirmCode’][‘errorTimes’] = 1;
} else {
$_SESSION[‘confirmCode’][‘errorTimes’] = intval($_SESSION[‘confirmCode’][‘errorTimes’]) + 1;
}
}
class imageCaptcha
{
private $height;
private $width;
private $textNum;
private $textContent;
private $fontColor;
private $randFontColor;
private $fontSize;
private $fontFamily;
private $bgColor;
private $randBgColor;
private $textLang;
private $noisePoint;
private $noiseLine;
private $distortion;
private $distortionImage;
private $showBorder;
private $image;
public function imageCaptcha()
{
$this->textNum = 4;
$this->fontSize = 14;
$this->fontFamily = ”;
$this->textLang = ‘en’;
$this->noisePoint = 0;
$this->noiseLine = 0;
$this->distortion = false;
$this->showBorder = false;
}
public function set_show_mode($w, $h, $num, $fc, $fz, $ff_url, $lang, $bc, $m, $n, $b, $border)
{
$this->width = $w;
$this->height = $h;
$this->textNum = $num;
$this->fontColor = sscanf($fc, ‘#%2x%2x%2x’);
$this->fontSize = $fz;
$this->fontFamily = $ff_url;
$this->textLang = $lang;
$this->bgColor = sscanf($bc, ‘#%2x%2x%2x’);
$this->noisePoint = $m;
$this->noiseLine = $n;
$this->distortion = $b;
$this->showBorder = $border;
}
public function initImage()
{
if (empty($this->width)) {
$this->width = floor($this->fontSize * 1.3) * $this->textNum + 10;
}
if (empty($this->height)) {
$this->height = floor($this->fontSize * 2.5);
}
$this->image = imagecreatetruecolor($this->width, $this->height);
if (empty($this->bgColor)) {
$this->randBgColor = imagecolorallocate($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
} else {
$this->randBgColor = imagecolorallocate($this->image, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]);
}
imagefill($this->image, 0, 0, $this->randBgColor);
}
public function randText($type)
{
$string = ”;
switch ($type) {
case ‘en’:
$str = ‘ABCDEFGHJKLMNPQRSTVWXYabcdehkmnprsvwxy35678’;
for ($i = 0; $i < $this->textNum; $i++) {
$string = $string . ‘,’ . $str[mt_rand(0, 42)];
}
break;
}
return substr($string, 1);
}
public function createText()
{
$text_array = explode(‘,’, $this->randText($this->textLang));
$this->textContent = join(”, $text_array);
if (empty($this->fontColor)) {
$this->randFontColor = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
} else {
$this->randFontColor = imagecolorallocate($this->image, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]);
}
for ($i = 0; $i < $this->textNum; $i++) {
$angle = mt_rand(-1, 1) * mt_rand(1, 20);
imagettftext($this->image, $this->fontSize, $angle, 5 + $i * floor($this->fontSize * 1.3), floor($this->height * 0.75), $this->randFontColor, $this->fontFamily, $text_array[$i]);
}
}
public function createNoisePoint()
{
for ($i = 0; $i < $this->noisePoint; $i++) {
$pointColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $pointColor);
}
}
public function createNoiseLine()
{
for ($i = 0; $i < $this->noiseLine; $i++) {
$lineColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), 20);
imageline($this->image, 0, mt_rand(0, $this->width), $this->width, mt_rand(0, $this->height), $lineColor);
}
}
public function distortionText()
{
$this->distortionImage = imagecreatetruecolor($this->width, $this->height);
imagefill($this->distortionImage, 0, 0, $this->randBgColor);
for ($x = 0; $x < $this->width; $x++) {
for ($y = 0; $y < $this->height; $y++) {
$rgbColor = imagecolorat($this->image, $x, $y);
imagesetpixel($this->distortionImage, (int) ($x + sin($y / $this->height * 2 * M_PI – M_PI * 0.5) * 3), $y, $rgbColor);
}
}
$this->image = $this->distortionImage;
}
public function createImage()
{
$this->initImage();
$this->createText();
$this->createNoisePoint();
$this->createNoiseLine();
if ($this->distortion != false) {
$this->distortionText();
}
if ($this->showBorder) {
imagerectangle($this->image, 0, 0, $this->width – 1, $this->height – 1, $this->randFontColor);
}
imagepng($this->image);
imagedestroy($this->image);
if ($this->distortion != false) {
imagedestroy($this->distortionImage);
}
return $this->textContent;
}
}
$code_obj = new imageCaptcha();
$code_obj->set_show_mode(’85’, ’30’, ‘4’, ”, ’14’, ‘font/’ . rand(1, 6) . ‘.ttf’, ‘en’, ‘#fcfefb’, mt_rand(5, 10), mt_rand(1, 3), false, false);
$_SESSION[‘confirmCode’][‘code’] = $code_obj->createImage();
$_SESSION[‘confirmCode’][‘time’] = $serverTime;

?>

分类: php学习 标签:
css.php