中山php|最优网络中山做网站 中山php建站

最优良人

2013/05/13 at 12:56

用新浪搜狐腾讯jS接口取客户端IP及省份城市

新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42

搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson

搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8

搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip

腾讯的接口:http://fw.qq.com/ipaddress (腾讯的接口原本是最好用的,可现在打不开啦)

上面的接口中新浪的是js接口说明:

<script type="text/javascript" src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" charset="gb2312"></script>
<script type="text/javascript">
function ipmsg() {
var ss = remote_ip_info['country']  + remote_ip_info['province']+ remote_ip_info['city'];
document.getElementByIdx_x_x("<%=HiddenField1.ClientID %>").value = ss;

}
</script>

 

comments Comments (689)    -
2013/03/30 at 14:48

php压缩html : 清除换行符,清除制表符,去掉注释标记

/**
* 压缩html : 清除换行符,清除制表符,去掉注释标记
* @param $string
* @return 压缩后的$string
* */
function compress_html($string) {
$string = str_replace("\r\n", '', $string); //清除换行符
$string = str_replace("\n", '', $string); //清除换行符
$string = str_replace("\t", '', $string); //清除制表符
$pattern = array (
"/> *([^ ]*) *</", //去掉注释标记
"/[\s]+/",
"/<!--[^!]*-->/",
"/\" /",
"/ \"/",
"'/\*[^*]*\*/'"
);
$replace = array (
">\\1<",
" ",
"",
"\"",
"\"",
""
);
return preg_replace($pattern, $replace, $string);
}

comments Comments (689)    -
2013/03/29 at 18:55

php防止刷流量攻击

<?php
//查询禁止IP
$ip =$_SERVER['REMOTE_ADDR'];
$fileht=".htaccess2";
if(!file_exists($fileht))file_put_contents($fileht,"");
$filehtarr=@file($fileht);
if(in_array($ip."\r\n",$filehtarr))die("Warning:"."<br>"."Your IP address are forbided by some reason, IF you have any question Pls emill to shop@mydalle.com!");

//加入禁止IP
$time=time();
$fileforbid="log/forbidchk.dat";
if(file_exists($fileforbid))
{ if($time-filemtime($fileforbid)>60)unlink($fileforbid);
else{
$fileforbidarr=@file($fileforbid);
if($ip==substr($fileforbidarr[0],0,strlen($ip)))
{
if($time-substr($fileforbidarr[1],0,strlen($time))>600)unlink($fileforbid);
elseif($fileforbidarr[2]>600){file_put_contents($fileht,$ip."\r\n",FILE_APPEND);unlink($fileforbid);}
else{$fileforbidarr[2]++;file_put_contents($fileforbid,$fileforbidarr);}
}
}
}
//防刷新
$str="";
$file="log/ipdate.dat";
if(!file_exists("log")&&!is_dir("log"))mkdir("log",0777);
if(!file_exists($file))file_put_contents($file,"");
$allowTime = 120;//防刷新时间
$allowNum=10;//防刷新次数
$uri=$_SERVER['REQUEST_URI'];
$checkip=md5($ip);
$checkuri=md5($uri);
$yesno=true;
$ipdate=@file($file);
foreach($ipdate as $k=>$v)
{ $iptem=substr($v,0,32);
$uritem=substr($v,32,32);
$timetem=substr($v,64,10);
$numtem=substr($v,74);
if($time-$timetem<$allowTime){
if($iptem!=$checkip)$str.=$v;
else{
$yesno=false;
if($uritem!=$checkuri)$str.=$iptem.$checkuri.$time."1\r\n";
elseif($numtem<$allowNum)$str.=$iptem.$uritem.$timetem.($numtem+1)."\r\n";
else
{
if(!file_exists($fileforbid)){$addforbidarr=array($ip."\r\n",time()."\r\n",1);file_put_contents($fileforbid,$addforbidarr);}
file_put_contents("log/forbided_ip.log",$ip."--".date("Y-m-d H:i:s",time())."--".$uri."\r\n",FILE_APPEND);
$timepass=$timetem+$allowTime-$time;
die("Warning:"."<br>"."Sorry,you are forbided by refreshing frequently too much, Pls wait for ".$timepass." seconds to continue!");
}
}
}
}
if($yesno) $str.=$checkip.$checkuri.$time."1\r\n";
file_put_contents($file,$str);

comments Comments (0)    -
2012/12/17 at 20:48

php多维数组的搜索

1 php搜索多维数组的键值

如下面例子:

$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo[3]['c']['dd'] = 'bar 3';
$foo[3]['f']['gg'] = 'bar 3';
$foo['info'][1] = 'bar 5';

如果要查找 bar 3 怎么进行查找呢。有三个结果,而这三个结果都要,看下面的函数:
-------------------------------------------------------------------------------------------------------------------------------
function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){
global $nodes_found;
$a++;
foreach ($haystack as $key1=>$value1) {
    $nodes_temp[$a] = $key1;
    if (is_array($value1)){   
      array_search_re($needle, $value1, $a, $nodes_temp);
    }
    else if ($value1 === $needle){
      $nodes_found[] = $nodes_temp;
    }
}
return $nodes_found;
}
---------------------------------------------------------------------------------------------------------------------------------
这个函数就可以把上面要查找到的内容全部返回出键名来
$result = array_search_re('bar 3', $foo);

print_r($result);

输出结果为如下:
Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb )
          [1] => Array ( [1] => 3 [2] => c [3] => dd )
          [2] => Array ( [1] => 3 [2] => f [3] => gg )
        )

1 php搜索多维数组的键名

function array_search_key($needle, $haystack){
global $nodes_found;

foreach ($haystack as $key1=>$value1) {
 
 if ($key1=== $needle){
 
  $nodes_found[] = $value1;
       
   }
    if (is_array($value1)){   
      array_search_key($needle, $value1);
    }
   
   
}

return $nodes_found;
}
$result = array_search_key('a', $foo);

print_r($result);

输出结果为如下:
 

Array
(
    [0] => Array
        (
            [xx] => bar 1
        )

    [1] => Array
        (
            [bb] => bar 3
        )

    [2] => Array
        (
            [yy] => bar 4
        )

)

标签:,
comments Comments (743)    -
2012/12/04 at 14:34

网站排名下降时是否是修改关键词的时机

最近网站排名出现变动,几个排名一直稳定在百度前三的关键词出现下降,甚至完全被k的情况,经过分析,发现是收到友情链接的牵连,有三个友链网站被k,对方链回我的那个关键词排名直接没有。

刚好我也想把 中山网站建设 这个关键词设为主关键词,以前由于其他词的排名都比较稳定,不敢轻易修改,怕其他的收到影响,这次趁此机会一起改了,看看效果如果,验证一下  网站排名下降时是否是修改关键词的时机,也给其他站长做一个参考。

comments Comments (310)    -
2012/11/24 at 17:16

php过滤客户提交参数,防注入

以下代码实现过滤php的$_GET 和$_POST参数

/**
* 安全防范
*/
function Add_S($array)
{
foreach($array as $key=>$value)
{
if(!is_array($value))
{
$value = get_magic_quotes_gpc()?$value:addslashes($value);
$array[$key]=filterHtml($value);
}
Else
{
Add_S($array[$key]);
}
}
return $array;
}
function glstr($var) {

if (is_array($var)) {
return Add_S($var);
}
elseif(strlen($var)){
$var = get_magic_quotes_gpc()?$var:addslashes($var);

$var = filterHtml($var);
}
return $var;
}
function filterHtml($html)
{
$farr = array(
"/<!DOCTYPE([^>]*?)>/eis",
"/<(\/?)(html|body|head|link|meta|base|input)([^>]*?)>/eis",
"/<(script|i?frame|style|title|form)(.*?)<\/\\1>/eis",
"/(<[^>]*?\s+)on[a-z]+\s*?=(\"|')([^\\2]*)\\2([^>]*?>)/isU",//过滤javascript的on事件
"/\s+/",//过滤多余的空白
);
$tarr = array(
"",
"",
"",
"\\1\\4",
" ",
);
$html = preg_replace( $farr,$tarr,$html);
return $html;
}
if (sizeof($_GET)) {
foreach($_GET as $key => $value) {
$_GET[$key] = glstr($value); //
}

}
if (sizeof($_POST)) {
foreach($_POST as $key => $value) {
$_POST[$key] = glstr($value); //
}
}

标签:,
comments Comments (343)    -
2012/11/14 at 16:28

php计算代码运行时间和使用内存

<?php

//开始计时


$HeaderTime =
microtime(true);//参数true表示返回浮点数值

//代码

//...

printf(" total run: %.2f s<br>".
"memory usage: %.2f M<br> ",
microtime(true)-$HeaderTime,
memory_get_usage() / 1024 / 1024 );
?>
结果:

total runtime: 1.47 s

memory usage: 77.09 M

标签:
comments Comments (454)    -
2012/10/07 at 16:47

select下拉菜单实现友情链接跳转

<select name="select" class="home_left_select" onchange="window.location=this.options[this.selectedIndex].value">
<option value="#">友情链接</option>
<option value="http://www.baidu.com">2</option>
<option value="http://www.baidu.com">2</option>
<option value="http://www.baidu.com">2</option>
<option value="http://www.baidu.com">2</option>
<option value="http://www.baidu.com">2</option>
</select>

以上代码实现选中项跳转到指定链接

标签:
comments Comments (399)    -
2012/09/26 at 11:42

mysql STRICT_TRANS_TABLES严格模式下提示Field 'id' doesn't have a default value

in: 数据库

在别的服务器运行我的网站程序的时候,出现了Field 'id' doesn't have a default value 的提示,意思是这个值我没有提交数据,并且数据库结构没有设置默认值,由于对方的mysql服务器开启了STRICT_TRANS_TABLES严格模式,所以报错了

解决方法是:

如果自己的服务器,有权限修改my.ini的话,打开my.ini,查找
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

修改为

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

然后重启MYSQL

在别人的虚拟空间上当然不可能实现,所以根本的解决方法还是修改自己的数据结构,把非空的字段加上默认值,以后设计数据库要注意这一点,方便程序的移植

标签:,
comments Comments (431)    -
2012/09/22 at 11:54

smarty模版使用php标签,如何获取模版变量

已经assign一个模版变量$assign,由于要做特殊的循环输出,使用for循环,因此使用到了php标签,但是php语句和模版语句的变量作用域是不同的,因此不能直接获取到

{{php}}

for($i=0;$i<count($assign);$i=$i+2){
echo '
<ul>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title="">'.$assign[$i][title].'</a></span> </li>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title="">'.$assign[$i+1][title].'</a></span> </li>i>

</ul>';}
{{/php}}

解决的方法是:模版变量全部存在smarty的一个对象里面;只要在for之前进行赋值:$assign = $this->_tpl_vars[assign];

{{php}}
$assign = $this->_tpl_vars[assign];
for($i=0;$i<count($assign);$i=$i+2){
echo '
<ul>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title="">'.$assign[$i][title].'</a></span> </li>
<li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title="">'.$assign[$i+1][title].'</a></span> </li>i>

</ul>';}
{{/php}}

标签:,
comments Comments (502)    -