Archive for the ‘PHP’ Category

Fckeditor 修改上传路径以及上传自动更改名称

星期二, 08月 5th, 2008

测试版本:Fckeditor 2.6.3 beta

1.一般情况下,fckeditor上传文件是指定目录(我们这里假设上传到uploads目录中),但是在实际开发中,可能程序会运行在二级目录,http://www.domain.com/app/ 下,在一级目录文件以及目录不能增加或修改的情况下,那我们又得去修改fckeditor里的config.php文件设置上传文件夹,这在很多开源程序中意义重大,拿一个程序来说,我上传的文件永远会在这个目录里中的 uploads 文件夹中。不管我们把这个程序移动到任何子目录中都不需要修改,当然如果你要把uploads文件夹改成其他名字,也很方便。代码如下:

1
2
3
4
5
6
$tmp_workroot = '../../../../../../'; //设置此虚拟目录根路径
$tmp_path = realpath($tmp_workroot).'\uploads\\'; //得到绝对路径和合并上传目录
 
//$tmp_abs_path = str_replace('\\','\\\\',$tmp_path); //得到fckeditor接受的绝对路径地址,如果要使用绝对路径上传,程序运行在windows操作系统要用到此代码
$tmp_siteroot = str_replace('/','\\',$_SERVER['DOCUMENT_ROOT']);	//得到根目录并转换
$tmp_relative_path = str_replace('\\','/',str_replace($tmp_siteroot,'',$tmp_path)); //得到相对上传路径

把上面这段代码添加到 fckeditor\editor\filemanager\connectors\php\config.php 顶部,然后把
$Config['UserFilesPath'] = ‘/uploads/’ ;
改成:$Config['UserFilesPath'] = $tmp_relative_path ;
工作就完成了

2.自动更名
打开fckeditor\editor\filemanager\connectors\php\commands.php 文件
修改$sFileName = time().mt_rand().’.’.$sExtension; //这是我的文件名,格式是:unix时间戳+随机数+扩展名
你可以展开你天马行空的想象力去命令,例如按年、月、日目录分类

把Fckeditor整合到Zend Framework View_Helper里

星期一, 08月 4th, 2008

看到过把fckeditor放进Controller和直接放在模板里的,虽然能成功运行,但总感觉都和Zend Framework规范有点不太相符,个人觉得放在视图助手里科学一点。

Zend Framework v1.52
Fckeditor v2.6.3 beta

其中fckeditor放在public/js/fckeditor目录下

代码如下:

[-]View Code LANGUAGE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
< ?php
/**************************
@Author  : evila
@Update  :
@Content : Fckeditor 视图辅助类
**************************/
 
class Zend_View_Helper_Fckeditor
{
 function fckeditor($InstanceName='',$value='',$width=800,$height=500,$toolbar='Default')
 {
  require_once 'js/fckeditor/fckeditor.php'
  $iFCKeditor = new FCKeditor($InstanceName)
  $iFCKeditor-&gtBasePath = './js/fckeditor/'
  $iFCKeditor-&gtWidth  = "$width"
  $iFCKeditor-&gtHeight = "$height"
        $iFCKeditor-&gtValue = "$value"
  $iFCKeditor-&gtToolbarSet = "$toolbar"
 
  return $iFCKeditor-&gtCreateHtml()
 }
}

在模板文件里只需 <?php echo $this->fckeditor(’InstanceName’,'test_text’);?> 调用即可(参数自行输入)。
插件fckeditor引导文件 require_once ‘js/fckeditor/fckeditor.php’; 也可用Zend::Loader()加载,但是根据官方手册介绍,效率是一样的

用PHP现实J2ee持久化访问模式-DAO

星期五, 04月 4th, 2008

通过“用户”来举例:

< ?php
#模型
class Member
{
private $uid;
private $username;
private $password;

public function setUid($_uid)
{
$this->uid = $_uid;
}

public function getUid()
{
return $this->uid;
}

public function setUsername($_username)
{
$this->username = $_username;
}
public function getUsername()
{
return $this->username;
}

public function setPassword($_password)
{
$this->password = $_password;
}
public function getPassword()
{
return $this->password;
}
}

#DAO
class MemberDAO
{
#基本变量
public $dbhost;
public $dbuser;
public $dbpassword;
public $dbname;

//构造函数为连接数据库
public function __construct($_dbhost,$_dbuser,$_dbpassword,$_dbname)
{
$this->dbhost=$_dbhost;
$this->dbuser=$_dbuser;
$this->dbpassword=$_dbpassword;
$this->dbname=$_dbname;
$this->conn= mysql_connect($this->dbhost,$this->dbuser,$this->dbpassword) or die(’连接数据库失败!’);
mysql_select_db($this->dbname);
}

//查询用户
public function getMember($_uid=”)
{
$members = array();

//如果参数uid为空,则查询全部
if(empty($_uid))
{
$res=mysql_query(”select * From members”);
}
else
{
$res=mysql_query(”select * From members WHERE uid = $uid”);
}

while($rows=mysql_fetch_assoc($res))
{
$member = new Member();
$member->setUid($row['uid']);
$member->setUsername($row['username']);
$member->setPassword($row['password']);

//追加到成员列表,如果不考虑查询多个用户则可不必这么麻烦
array_push($members,$member);
}
return $members;
}

//添加用户
public function addMember(Member $_member)
{
$uid = $_member->getUid();
$username = $_member->getUsername();
$password = md5($_member->getPassword());

$sql = “INSERT INTO members(uid,username,password) VALUES($uid,’$username’,'$password’)”;
mysql_query($sql_str) or die(mysql_error());
}

//修改资料
public function updateMember(Member $_member);
{
$uid = $_member->getUid();
$username = $_member->getUsername();
$password = md5($_member->getPassword());

$sql = “UPDATE members SET username=’$username’,password=’$password’ WHERE uid = $uid limit 1″;
mysql_query($sql_str) or die(mysql_error());
}

//删除用户………..
//其他操作用户的方法,不一一列举了
}

下面开始使用DAO

include ‘member.php’;
include ‘memberDAO.php’;
#创建DAO
$memberDAO = new memberDAO(’localhost’,'root’,'159753′,’test’);

#创建一个用户
$member = $new Member();
$member->setUid(911);
$member->setUsername(’tester’);
$member->setPassword(’321′);

#test 1
#添加到数据库
$memberDAO->addMember($member);

#test 2
#修改这个用户的密码
$member->setPassword(’123′);
$memberDAO->updateMember($member);

#test 3
#得到所有用户的列表
$members = $memberDAO->getMember();
#显示,这里假如不使用任何模版,只是测试显示数据,如果使用smrty模版引擎,更加方便,直接把$members传过去

foreach($members as $TmpMember)
{
echo ‘Uid:’.$TmpMember->getUid().’
‘;
echo ‘Username:’.$TmpMember->getUsername().’
‘;
echo ‘Password:’.$TmpMember->getPassword().’

‘;
}
?>

注意,示例代码仅供参考测试用,并不能直接使用在生产用途中。
推荐阅读:J2ee中DAO设计模式

网站程序轻松集成Discuz

星期四, 04月 3rd, 2008

Discuz是款优秀的BBS论坛程序,很多网站需要和论坛集成,例如显现通行证,但总感觉通过discuz自已提供的的passport现实有点不方便,发了点时间,把各个检测登陆、登陆、退出,做了个概述,希望对各位开发者有所帮助。这里不多说原理了,请看代码里的注释,请注意,此分解是基于Discuz 6.0正式版,如果你使用的其他版本,可能行号不太正确,不过我集成过discuz 5.5,除了行号,其他没什么变化。


/*————————-基础变量————————*/
#用户key,由论坛程序的KEY加上HTTP AGENT组合加密得到的,authcode函数里有引用
#include/common.inc.php 127行
$discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);

#得到并分解cookie,如果没有设置默认值
#include/common.inc.php 128行
list($discuz_pw, $discuz_secques, $discuz_uid) = empty($_DCOOKIE['auth']) ? array(”, ”, 0) : daddslashes(explode(”\t”, authcode($_DCOOKIE['auth'], ‘DECODE’)), 1);
#FORMHASH 全局变量,用来识别用户用的,防止伪造信息
#include/common.inc.php 219行
define(’FORMHASH’, formhash());

#安全问题加密
#logging.php 93行
$secques = quescrypt($questionid, $answer);
/*————————-登陆系统————————*/
#logging.php 104行起
#自己DIY的登陆表单,然后验证,注意,这里默认关闭了安全问题验证,可自行加上

$formUsername = trim($_POST['$username']); // 接受POST传来的$username
$formPassword = trim($_POST['password']); // 接受POST传来的$password
$sql = $db->query(”SELECT uid,password,secques FROM cdb_members WHERE username=’$formUsername’”);
$member = $db->fetch_array($sql);
#验证用户密码是否匹配
if($member['password'] == md5($formPassword))
{
#登陆成功
dsetcookie(’sid’,”,-2423234234); // 注销掉sid
dsetcookie(’auth’, authcode(”$formPasswordt$$member['secques']t$member['uid']“, ‘ENCODE’), ‘1234243′); //加密登陆信息
}
else
{
#登陆失败
}

/*————————-退出系统————————*/
#自己diy退出
<a href=”discuz/logging.php?action=logout&formhash=<?php echo FORMHASH ?>&forward=http://localhost/test/discuz.php”>退出系统</a>
/*————————-检测是否登陆状态————————*/
#检测登陆成功后
#自己diy检测
header(’Content-Type: text/html; charset=utf-8′);
include ‘discuz/include/common.inc.php’; //插入discuz核心文件,这是必须,要不然无法判断

if($discuz_uid)
{
echo ‘你已经登陆<br>’;
echo ‘<a href=”discuz/logging.php?action=logout&formhash=’.formhash().’&forward=http://localhost/test/discuz.php”>退出系统</a><br>’;
}
else
{
echo ‘你还没有登陆<br>’;
echo ‘<a href=”discuz/logging.php?action=login&forward=http://localhost/test/discuz.php”>登陆系统</a><br>’;
}

/*————————-用到的几个函数————————*/
#加密:include/global.func.php
function authcode($string, $operation, $key = ”) {

$key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
$key_length = strlen($key);

$string = $operation == ‘DECODE’ ? base64_decode($string) : substr(md5($string.$key), 0, 8).$string;
$string_length = strlen($string);

$rndkey = $box = array();
$result = ”;

for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($key[$i % $key_length]);
$box[$i] = $i;
}

for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}

if($operation == ‘DECODE’) {
if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) {
return substr($result, 8);
} else {
return ”;
}
} else {
return str_replace(’=', ”, base64_encode($result));
}

}

#清除cookie
function clearcookies() {
global $discuz_uid, $discuz_user, $discuz_pw, $discuz_secques, $adminid, $credits;
dsetcookie(’sid’, ”, -86400 * 365);
dsetcookie(’auth’, ”, -86400 * 365);
dsetcookie(’visitedfid’, ”, -86400 * 365);
dsetcookie(’onlinedetail’, ”, -86400 * 365, 0);

$discuz_uid = $adminid = $credits = 0;
$discuz_user = $discuz_pw = $discuz_secques = ”;
}

#设计cookie
function dsetcookie($var, $value, $life = 0, $prefix = 1) {
global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
setcookie(($prefix ? $cookiepre : ”).$var, $value,
$life ? $timestamp + $life : 0, $cookiepath,
$cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}

#加密安全问题
function quescrypt($questionid, $answer) {
return $questionid > 0 && $answer != ” ? substr(md5($answer.md5($questionid)), 16, 8) : ”;
}

用Jquery处理JSON数据

星期三, 04月 2nd, 2008

Jquery处理JSON的方法是:jQuery.getJSON( url, [data][callback] )

 

范例一:用jquery获取我在flickr相册的所有图片,不过考虑速度,只显示4张,完整代码请看DEMO源码。
DEMO:http://liuyami.com/labs/flickr/flickr.html

代码:

  $(function(){
    $.getJSON(”http://api.flickr.com/services/feeds/photos_public.gne?id=25255302@N03&tagmode=any&format=json&jsoncallback=?”,function(data){
          $.each(data.items, function(i,item){
            $(”<img/> “).attr(”src”, item.media.m).appendTo(”#images”);
            if ( i == 3 ) return false; //计数器,只显示4张
          });
        });
  });

示范二:处理PHP返回的JSON数据

$(function(){
         $.getJSON(”data.php”, function(json){
              for(var i = 0; i < json.length; i++)
              {
                  alert(”name:” + json[i].name);
                  alert(”age:” + json[i].age);
              }
         });
});