如何让自己的本地APACHE服务器支持.htaccess

解决问题:

如何让自己的本地APACHE服务器支持”.htaccess”呢?其实只要简单修改一下apache的httpd.conf设置就可以让APACHE支持.htaccess了,来看看操作

打开httpd.conf文件(在那里? APACHE目录的CONF目录里面),用文本编辑器打开后,查找
(1)
Options FollowSymLinks
AllowOverride None

改为
Options FollowSymLinks
AllowOverride All
(2)去掉下面的注释
LoadModule rewrite_module modules/mod_rewrite.so

就可以了


关注公众号,了解更多it技术(it问答网

PHP获得文件创建、修改、访问时间

PHP filectime() 函数
定义和用法
filectime() 函数返回指定文件的上次 inode 修改时间。

该函数返回文件上次 inode 被修改的时间。如果出错则返回 false。时间以 Unix 时间戳的方式返回。
语法
fileatime(filename) filename 必需。规定要检查的文件。
提示和注释
提示:本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。

注意:在大多数 Unix 文件系统中,当一个文件的 inode 数据被改变时则该文件被认为是修改了。也就是说,当文件的权限,所有者,所有组或其它 inode 中的元数据被更新时。参见 filemtime()(这才是你想用于在 Web 页面中建立“最后更新时间”脚注的函数)和 fileatime()。

注释:某些 Unix 说明文本中把 ctime 说成是该文件建立的时间,这是错的。在大多数 Unix 文件系统中,没有 Unix 文件的建立时间。
PHP filemtime()函数
定义和用法
filemtime() 函数返回文件内容上次的修改时间。若成功,则时间以 Unix 时间戳的方式返回。若失败,则返回 false。
语法
filemtime(filename) filename 必需。规定要检查的文件。
说明
本函数返回文件中的数据块上次被写入的时间,也就是说,文件的内容上次被修改的时间。
提示和注释
提示:本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。
PHP fileatime()函数
定义和用法
fileatime() 函数返回指定文件的上次访问时间。

该函数返回文件上次被访问的时间。如果出错则返回 false。时间以 Unix 时间戳的方式返回。
语法
fileatime(filename) filename 必需。规定要检查的文件。
提示和注释
提示:本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。

注释:文件的 atime 应该在不论何时读取了该文件中的数据块时被更改。当一个应用程序定期访问大量文件或目录时很影响性能。有些 Unix 文件系统可以在加载时关闭 atime 的更新以提高这类程序的性能。USENET 新闻组假脱机是一个常见的例子。在这种文件系统下,本函数没有用处。

实例

< ?php
$a=filectime(“log.txt”);
echo “创建时间:”.date(“Y-m-d H:i:s”,$a).”
“;
$a=filemtime(“log.txt”);
echo “修改时间:”.date(“Y-m-d H:i:s”,$a).”
“;
$a=fileatime(“log.txt”);
echo “访问时间:”.date(“Y-m-d”,$a).”
“;
?>


关注公众号,了解更多it技术(it问答网

基于PHP的cURL快速入门

cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP、FTP、TELNET等。最爽的是,PHP也支持 cURL 库。本文将介绍 cURL 的一些高级特性,以及在PHP中如何运用它。

为什么要用 cURL?

是的,我们可以通过其他办法获取网页内容。大多数时候,我因为想偷懒,都直接用简单的PHP函数:

以下为引用的内容:

$content = file_get_contents(“http://www.nettuts.com”);
// or
$lines = file(“http://www.nettuts.com”);
// or
readfile(http://www.nettuts.com);

不过,这种做法缺乏灵活性和有效的错误处理。而且,你也不能用它完成一些高难度任务——比如处理coockies、验证、表单提交、文件上传等等。

引用:
cURL 是一种功能强大的库,支持很多不同的协议、选项,能提供 URL 请求相关的各种细节信息。

基本结构

在学习更为复杂的功能之前,先来看一下在PHP中建立cURL请求的基本步骤:

初始化 设置变量 执行并获取结果 释放cURL句柄

以下为引用的内容:

// 1. 初始化
$ch = curl_init();
// 2. 设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, “http://www.nettuts.com”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// 3. 执行并获取HTML文档内容
$output = curl_exec($ch);
// 4. 释放curl句柄
curl_close($ch);

第二步(也就是 curl_setopt() )最为重要,一切玄妙均在此。有一长串cURL参数可供设置,它们能指定URL请求的各个细节。要一次性全部看完并理解可能比较困难,所以今天我们只试一下那些更常用也更有用的选项。

检查错误

你可以加一段检查错误的语句(虽然这并不是必需的):

以下为引用的内容:

// …
$output = curl_exec($ch);
if ($output === FALSE) {
echo “cURL Error: ” . curl_error($ch);
}
// …

请注意,比较的时候我们用的是“=== FALSE”,而非“== FALSE”。因为我们得区分 空输出 和 布尔值FALSE,后者才是真正的错误。

 


关注公众号,了解更多it技术(it问答网

经典,完美抓取页面程序原理

<?php
/*
描述:从0a上抓取站点信息
作者:xuduowei
日期:2011.02.12
*/
class getInfoController extends tcoaBaseController{

protected $getInfoModel;
public function set_getInfoModel($getInfoModel){
$this->getInfoModel=$getInfoModel;
}
//载入checkSessionModel模型,方便后面调用里面的方法,如:域名列表显示
protected $checkSessionModel;
public function set_checkSessionModel($checkSessionModel){
$this->checkSessionModel=$checkSessionModel;
}

//jquery调用方法
public function getInfo(){
$domain=trim($_POST[‘domain’]);
$domainarr=$this->getInfoModel->listsite($domain);
$old_timeout=ini_get(‘default_socket_timeout’);
ini_set(‘default_socket_timeout’,15); //设置file_get_contents 取值超时,当前是15秒

foreach($domainarr as $domainname){
$ftpdomain=$domainname[‘ftp_domain’];
$siteipold=$domainname[‘siteip’];
$ftp_userold=$domainname[‘ftp_user’];
$ftp_passold=$domainname[‘ftp_pass’];
$db_hostold=$domainname[‘db_host’];
$db_userold=$domainname[‘db_user’];
$db_passold=$domainname[‘db_pass’];
$serveripold=$domainname[‘serverip’];

$domainarr=explode(“www.”,$ftpdomain);
$begainurl=Project::singleton()->getConfig(‘customConfig.getInterfaceURL’);//其中getInterfaceURL为tcoaBaseController中的一个自定义变量(接口路径)。
$url=$begainurl.$domainarr[1];

$jsoncon=””;
$i=0;
if(function_exists(“file_get_contents”)){
while($i<3&&empty($jsoncon)){
$i++;
$jsoncon=file_get_contents($url);
}
}elseif(function_exists(‘curl_init’)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$jsoncon = curl_exec ($ch);
curl_close ($ch);
}
if(!empty($jsoncon)){
// $jsoncon=file_get_contents($url);//若直接这样写的话,不是很完美。参看行28-39
$ans = json_decode($jsoncon,true);
$siteip=$ans[“ip”];
$ftpuser=$ans[“ftpuser”];
$ftppwd=$ans[“ftppwd”];

$dbuser=$ans[“dbuser”];
$dbpwd=$ans[“dbpwd”];
$db=$ans[“db”];
$siteip2=$siteip;
$serverip=$ans[“server”];
//从oa上获得信息后,按域名更新相应的信息。
$this->getInfoModel->updateinfo($siteip2,$ftpuser,$ftppwd,$siteip2,$dbuser,$dbpwd,$db,$serverip,$ftpdomain);
if(!empty($ftpdomain)){
if($siteipold==$siteip){
$str.=”成功更改:”.$ftpdomain.”–原子站ip:”.$siteipold.”–<font color=red>更改后:”.$siteip.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp用户名:”.$ftp_userold.”–<font color=red>更改后:”.$ftpuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp密码:”.$ftp_passold.”–<font color=red>更改后:”.$ftppwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库用户名:”.$db_userold.”–<font color=red>更改后:”.$dbuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库密码:”.$db_passold.”–<font color=red>更改后:”.$dbpwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原服务器ip:”.$serveripold.”–<font color=red>更改后:”.$serverip.”</font><br/>”;
echo $str;
}else{
$str.=”成功更改:”.$ftpdomain.”–原子站ip:<font color=blue>”.$siteipold.”</font>–<font color=red>更改后:”.$siteip.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp用户名:<font color=blue>”.$ftp_userold.”</font>–<font color=red>更改后:”.$ftpuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原ftp密码:<font color=blue>”.$ftp_passold.”</font>–<font color=red>更改后:”.$ftppwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库用户名:<font color=blue>”.$db_userold.”</font>–<font color=red>更改后:”.$dbuser.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原数据库密码:<font color=blue>”.$db_passold.”</font>–<font color=red>更改后:”.$dbpwd.”</font><br/>”;
$str.=”成功更改:”.$ftpdomain.”–原服务器ip:<font color=blue>”.$serveripold.”</font>–<font color=red>更改后:”.$serverip.”</font><br/>”;
echo $str;
}
}
}else{
$str=”<font color=red>更改失败!!!!!!!</font>”.$ftpdomain.”–原ip:”.$siteipold.”<br/>”;
echo $str;
}
if(!empty($ftpdomain)){
$this->insertLog($ftpdomain,$str);
}
}//结束foreach
ini_set(‘default_socket_timeout’,$old_timeout);
}//结束function

//视图方法
public function showGetInfo(){
$this->getInfo();
$View=ViewDepository::singleton()->imLoad(‘./tcoaView/getInfoView.php’);
$View->showtypename=$this->checkSessionModel->listtypename();
echo $View->render();
}

//写日志方法
public function insertLog($domain,$str){
$time=time();
$group_id= $this->userName.”_”.date(“YnjHis”);
//$tj=” group_id=’$group_id’,domain=’$domain’,description=’$str’,checktime=’$time'”;
$arrdomain=$this->getInfoModel->insertlog($group_id,$domain,$str,$time);
}
}
?>


关注公众号,了解更多it技术(it问答网

preg_replace只替换一次

<?php
function sst($result,$keys){
$len=substr_count($result,$keys);
for($i=1;$i<=$len;$i++){
$num=$i.”、”;
$result=preg_replace(“/$keys/”,$num,$result,1);
echo $result.”<br/>”;
}

return $result;
}

sst(“<xdw>十分士大夫发生发生<xdw>123544++;<xdw>545″,”<xdw>”);

?>


关注公众号,了解更多it技术(it问答网

addslashes,mysql_real_escape_string ,mysql_escape_string的区别

本文介绍的是用 mysql_real_escape_string对用户提交数据进行整理处理和通过addslashes以及mysql_escape_string这3个类似的功能函数的区别。经过转义的数据可以直接插入到数据库中。

很好的说明了addslashes和mysql_real_escape_string的区别,虽然国内很多PHP coder仍在依靠addslashes防止SQL注入(包括我在内),我还是建议大家加强中文防止SQL注入的检查。addslashes的问题在于黑客可以用0xbf27来代替单引号,而addslashes只是将0xbf27修改为0xbf5c27,成为一个有效的多字节字符,其中的0xbf5c仍会被看作是单引号,所以addslashes无法成功拦截。

当然addslashes也不是毫无用处,它是用于单字节字符串的处理,多字节字符还是用mysql_real_escape_string吧。

另外对于php手册中get_magic_quotes_gpc的举例:
if (!get_magic_quotes_gpc()) {
$lastname = addslashes($_POST[‘lastname’]);
} else {
$lastname = $_POST[‘lastname’];
}
最好对magic_quotes_gpc已经开放的情况下,还是对$_POST[’lastname’]进行检查一下。

再说下mysql_real_escape_string和mysql_escape_string这2个函数的区别:
mysql_real_escape_string 必须在(PHP 4 >= 4.3.0 PHP 5)的情况下才能使用。否则只能用 mysql_escape_string ,两者的区别是:
mysql_real_escape_string 考虑到连接的当前字符集,而mysql_escape_string 不考虑。

总结一下:

addslashes() 是强行加;

mysql_real_escape_string() 会判断字符集,但是对PHP版本有要求;

mysql_escape_string不考虑连接的当前字符集。

mysql_real_escape_string()函数用于转义SQL语句中的特殊字符,该函数的语法格式如下:

string mysql_real_escape_string ( string $unescaped_string[, resource $link_identifier ] )

在上述语法中涉及到的参数说明如下。

unescaped_string:未转义的字符串。

link_identifier:MySQL的连接标识符。

mysql_real_escape_string()函数不转义”%” 和 “_”这两个符号。

使用函数mysql_real_escape_string()函数的示例代码如下:

代码23-19 光盘\codes\第23章\23.4\mysql_real_escape_string.php

<?php $connection=mysql_connect(“localhost”,”root”,”root”) ordie(“连接服务器失败”); $person= “Jone’s and Bobo’s teacher”; $escaped_person= mysql_real_escape_string($person); echo$escaped_person; ?>

上面代码的输出结果如图23-18所示。

图23-18 转义后的字符


关注公众号,了解更多it技术(it问答网

php 正则表达式匹配非链接文字

//$str='<a href=”aaaaa”>aaa百度aaaaa</a><div>test</div><p>crumbs</p></a>百度知道<a href=”bbbb”>bbbbbbb</a><p>crumbs</p><p>crumbs</p>百度<p>crumbs</p><p>crumbs</p>贴吧<p>crumbs</p><p>crumbs</p><a href=”ccccc”>ccccccc</a>’;

$str='<a href=”aaaaa”>aaa百度aaaaa</a><div>test</div><p>crumbs</p></a>百度知道’;

//$str = preg_replace(‘/(?<=<\/a>)(.*?)(百度)(.*?)(?=(?<!(<a))<a)/i’,’\1<a href=\”baidu.com\”>\2</a>\3`’,$str);
$str = preg_replace(‘/(?<=<\/a>)(.*?)(百度)(.*?)(?=(?<!(<a)))/i’,’\1<a href=\”baidu.com\”>\2</a>\3`’,$str);
echo $str;


关注公众号,了解更多it技术(it问答网