我常用的幾個類
修改自hk的
PHP代碼:--------------------------------------------------------------------------------
/*
作用:取得隨機字符串
參數:
1、(int)$length = 32 #隨機字符長度,默認為32
2、(int)$mode = 0 #隨機字符類型,0為大小寫英文和數字,1為數字,2為小寫子木,3為大寫字母,4為大小寫字母,5為大寫字母和數字,6為小寫字母和數字
返回:取得的字符串
使用:
$code = new activeCodeObj;
$str = $code->getCode($length, $mode);
*/
class activeCodeObj
{
function getCode ($length = 32, $mode = 0)
{
switch ($mode) {
case '1':
$str = '1234567890';
break;
case '2':
$str = 'abcdefghijklmnopqrstuvwxyz';
break;
case '3':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
break;
case '6':
$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
break;
default:
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
break;
}
$result = '';
$l = strlen($str);
for($i = 0;$i < $length;$i ++){
$num = rand(0, $l);
$result .= $str[$num];
}
return $result;
}
}
PHP代碼:--------------------------------------------------------------------------------
/*
作用:取得客戶端信息
參數:
返回:指定的資料
使用:
$code = new clientGetObj;
1、浏覽器:$str = $code->getBrowse();
2、IP地址:$str = $code->getIP();
4、操作系統:$str = $code->getOS();
*/
class clientGetObj
{
function getBrowse()
{
global $_SERVER;
$Agent = $_SERVER['HTTP_USER_AGENT'];
$browser = '';
$browserver = '';
$Browser = array('Lynx', 'MOSAIC', 'AOL', 'Opera', 'JAVA', 'MacWeb', 'WebExplorer', 'OmniWeb');
for($i = 0; $i <= 7; $i ++){
if(strpos($Agent, $Browsers[$i])){
$browser = $Browsers[$i];
$browserver = '';
}
}
if(ereg('Mozilla', $Agent) && !ereg('MSIE', $Agent)){
$temp = explode('(', $Agent);
$Part = $temp[0];
$temp = explode('/', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'Netscape Navigator';
}
if(ereg('Mozilla', $Agent) && ereg('Opera', $Agent)) {
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(')', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/', '\1', $browserver);
$browserver = $browserver;
$browser = 'Opera';
}
if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent)){
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(';', $Part);
$Part = $temp[1];
$temp = explode(' ', $Part);
$browserver = $temp[2];
$browserver = preg_replace('/([d.]+)/','\1',$browserver);
$browserver = $browserver;
$browser = 'Internet Explorer';
}
if($browser != ''){
$browseinfo = $browser.' '.$browserver;
} else {
$browseinfo = false;
}
return $browseinfo;
}
function getIP ()
{
global $_SERVER;
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function getOS ()
{
global $_SERVER;
$agent = $_SERVER['HTTP_USER_AGENT'];
$os = false;
if (eregi('win', $agent) && strpos($agent, '95')){
$os = 'Windows 95';
}
else if (eregi('win 9x', $agent) && strpos($agent, '4.90')){
$os = 'Windows ME';
}
else if (eregi('win', $agent) && ereg('98', $agent)){
$os = 'Windows 98';
}
else if (eregi('win', $agent) && eregi('nt 5.1', $agent)){
$os = 'Windows XP';
}
else if (eregi('win', $agent) && eregi('nt 5', $agent)){
$os = 'Windows 2000';
}
else if (eregi('win', $agent) && eregi('nt', $agent)){
$os = 'Windows NT';
}
else if (eregi('win', $agent) && ereg('32', $agent)){
$os = 'Windows 32';
}
else if (eregi('linux', $agent)){
$os = 'Linux';
}
else if (eregi('unix', $agent)){
$os = 'Unix';
}
else if (eregi('sun', $agent) && eregi('os', $agent)){
$os = 'SunOS';
}
else if (eregi('ibm', $agent) && eregi('os', $agent)){
$os = 'IBM OS/2';
}
else if (eregi('Mac', $agent) && eregi('PC', $agent)){
$os = 'Macintosh';
}
else if (eregi('PowerPC', $agent)){
$os = 'PowerPC';
}
else if (eregi('AIX', $agent)){
$os = 'AIX';
}
else if (eregi('HPUX', $agent)){
$os = 'HPUX';
}
else if (eregi('NetBSD', $agent)){
$os = 'NetBSD';
}
else if (eregi('BSD', $agent)){
$os = 'BSD';
}
else if (ereg('OSF1', $agent)){
$os = 'OSF1';
}
else if (ereg('IRIX', $agent)){
$os = 'IRIX';
}
else if (eregi('FreeBSD', $agent)){
$os = 'FreeBSD';
}
else if (eregi('teleport', $agent)){
$os = 'teleport';
}
else if (eregi('flashget', $agent)){
$os = 'flashget';
}
else if (eregi('webzip', $agent)){
$os = 'webzip';
}
else if (eregi('offline', $agent)){
$os = 'offline';
}
else {
$os = 'Unknown';
}
return $os;
}
}
PHP代碼:--------------------------------------------------------------------------------
class cnStrObj
{
function substrGB ($str = '', $start = '', $len = ''){
if($start == 0
$start == ''){
$start = 1;
}
if($str == ''
$len == ''){
return false;
}
for($i = 0; $i < $start + $len; $i ++){
$tmpstr = (ord($str[$i]) >= 161 && ord($str[$i]) <= 247&& ord($str[$i+1]) >= 161 && ord($str[$i+1]) <= 254)?$str[$i].$str[++$i]:$tmpstr = $str[$i];
if ($i >= $start && $i < ($start + $len))
{
$tmp .=$tmpstr;
}
}
return $tmp;
}
function isGB ($str)
{
$strLen = strlen($str);
$length = 1;
for($i = 0; $i < $strLen; $i ++) {
$tmpstr = ord(substr($str, $i, 1));
$tmpstr2 = ord(substr($str, $i+1, 1));
if(($tmpstr <= 161
$tmpstr >= 247) && ($tmpstr2 <= 161
$tmpstr2 >= 247)){
$legalflag = false;
} else {
$legalflag = true;
}
}
return $legalflag;
}
}
PHP代碼:--------------------------------------------------------------------------------
<?php
/***************************************
** Filename.......: class.smtp.inc
** Project........: SMTP Class
** Version........: 1.00b
** Last Modified..: 30 September 2001
***************************************/
define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
define('SMTP_STATUS_CONNECTED', 2, TRUE);
class smtp{
var $connection;
var $recipients;
var $headers;
var $timeout;
var $errors;
var $status;
var $body;
var $from;
var $host;
var $port;
var $helo;
var $auth;
var $user;
var $pass;
/***************************************
** Constructor function. Arguments:
** $params - An assoc array of parameters:
**
** host - The hostname of the smtp server Default: localhost
** port - The port the smtp server runs on Default: 25
** helo - What to send as the HELO command Default: localhost
** (typically the hostname of the
** machine this script runs on)
** auth - Whether to use basic authentication Default: FALSE
** user - Username for authentication Default: <blank>
** pass - Password for authentication Default: <blank>
** timeout - The timeout in seconds for the call Default: 5
** to fsockopen()
***************************************/
function smtp($params = array()){
if(!defined('CRLF'))
define('CRLF', "\r\n", TRUE);
$this->timeout = 5;
$this->status = SMTP_STATUS_NOT_CONNECTED;
$this->host = 'localhost';
$this->port = 25;
$this->helo = 'localhost';
$this->auth = FALSE;
$this->user = '';
$this->pass = '';
$this->errors = array();
foreach($params as $key => $value){
$this->$key = $value;
}
}
/***************************************
** Connect function. This will, when called
** statically, create a new smtp object,
** call the connect function (ie this function)
** and return it. When not called statically,
** it will connect to the server and send
** the HELO command.
***************************************/
function connect($params = array()){
if(!isset($this->status)){
$obj = new smtp($params);
if($obj->connect()){
$obj->status = SMTP_STATUS_CONNECTED;
}
return $obj;
}else{
$this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
socket_set_timeout($this->connection, 0, 250000);
$greeting = $this->get_data();
if(is_resource($this->connection)){
return $this->auth ? $this->ehlo() : $this->helo();
}else{
$this->errors[] = 'Failed to connect to server: '.$errstr;
return FALSE;
}
}
}
/***************************************
** Function which handles sending the mail.
** Arguments:
** $params - Optional assoc array of parameters.
** Can contain:
** recipients - Indexed array of recipients
** from - The from address. (used in MAIL FROM<img src="images/smilies/smile.gif" border="0" alt="">,
** this will be the return path
** headers - Indexed array of headers, one header per array entry
** body - The body of the email
** It can also contain any of the parameters from the connect()
** function
***************************************/
function send($params = array()){
foreach($params as $key => $value){
$this->set($key, $value);
}
if($this->is_connected()){
// Do we auth or not? Note the distinction between the auth variable and auth() function
if($this->auth){
if(!$this->auth())
return FALSE;
}
$this->mail($this->from);
if(is_array($this->recipients))
foreach($this->recipients as $value)
$this->rcpt($value);
else
$this->rcpt($this->recipients);
if(!$this->data())
return FALSE;
// Transparency
$headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
$body = str_replace(CRLF.'.', CRLF.'..', $this->body);
$body = $body[0] == '.' ? '.'.$body : $body;
$this->send_data($headers);
$this->send_data('');
$this->send_data($body);
$this->send_data('.');
return (substr(trim($this->get_data()), 0, 3) === '250');
}else{
$this->errors[] = 'Not connected!';
return FALSE;
}
}
/***************************************
** Function to implement HELO cmd
***************************************/
function helo(){
if(is_resource($this->connection)
AND $this->send_data('HELO '.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
return TRUE;
}else{
$this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
return FALSE;
}
}
/***************************************
** Function to implement EHLO cmd
***************************************/
function ehlo(){
if(is_resource($this->connection)
AND $this->send_data('EHLO '.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
return TRUE;
}else{
$this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
return FALSE;
}
}
/***************************************
** Function to implement AUTH cmd
***************************************/
function auth(){
if(is_resource($this->connection)
AND $this->send_data('AUTH LOGIN')
AND substr(trim($error = $this->get_data()), 0, 3) === '334'
AND $this->send_data(base64_encode($this->user)) // Send username
AND substr(trim($error = $this->get_data()),0,3) === '334'
AND $this->send_data(base64_encode($this->pass)) // Send password
AND substr(trim($error = $this->get_data()),0,3) === '235' ){
return TRUE;
}else{
$this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
return FALSE;
}
}
/***************************************
** Function that handles the MAIL FROM: cmd
***************************************/
function mail($from){
if($this->is_connected()
AND $this->send_data('MAIL FROM:<'.$from.'>')
AND substr(trim($this->get_data()), 0, 2) === '250' ){
return TRUE;
}else
return FALSE;
}
/***************************************
** Function that handles the RCPT TO: cmd
***************************************/
function rcpt($to){
if($this->is_connected()
AND $this->send_data('RCPT TO:<'.$to.'>')
AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){
return TRUE;
}else{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
/***************************************
** Function that sends the DATA cmd
***************************************/
function data(){
if($this->is_connected()
AND $this->send_data('DATA')
AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
return TRUE;
}else{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;
}
}
/***************************************
** Function to determine if this object
** is connected to the server or not.
***************************************/
function is_connected(){
return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
}
/***************************************
** Function to send a bit of data
***************************************/
function send_data($data){
if(is_resource($this->connection)){
return fwrite($this->connection, $data.CRLF, strlen($data)+2);
}else
return FALSE;
}
/***************************************
** Function to get data.
***************************************/
function &get_data(){
$return = '';
$line = '';
if(is_resource($this->connection)){
while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' '){
$line = fgets($this->connection, 512);
$return .= $line;
}
return $return;
}else
return FALSE;
}
/***************************************
** Sets a variable
***************************************/
function set($var, $value){
$this->$var = $value;
return TRUE;
}
} // End of class
?>
用 PHP 動態創建 Flash 動畫 (Rate:) (轉)
作者: unknow 發布時間: 2002-7-11 文章類型: 轉載 閱讀次數: 124次 來自: unknow
Macromedia 公司出品的 Flash 動畫軟件現已經成為Web頁面上非常流行的表現工具,網站開發者利用它引起浏覽者的興趣。然而不幸的是,僅僅使用ActionScript創建動畫受到很大的限制,Macromedia已經宣布,打算放棄Flash Generator產品,轉而采用支持Flash MX的Cold Fusion,我們的網站將向何處去呢?現在,我們可以利用Ming PHP庫來輕松地動態創建Flash動畫,並且和我們的代碼無縫集成。我們可以根據數據庫裡的數據創建出各種不同效果的動畫。
Ming 動畫庫最初出現在PHP4.05版本裡,替代了以前版本的LibSwf模塊。Ming 庫采用C編寫,支持多種語言,下面先看一下如何利用PHP來制作動畫。該庫使用簡單,和 PHP 緊密集成,但是,該庫仍然處於實驗階段,目前的版本是0.2a,在線的教程、例子、和幫助手冊可以讓我們方便地學習,Ming裡的函數組織的很有條理,方便PHP和Flash開發者查找。使用 PHP和Ming庫,不但讓我們的網站有華麗的裝飾,而且可以減少開發Flash動畫的費用和繁復的勞動。
使用Ming庫有兩種方式,我們可以內嵌到PHP裡(對Unix平台而言),也可以把它作為一個PHP模塊來運行,裡面的函數支持所有的平台,使用時也無需實例化。如果我們把Ming庫作為PHP模塊來運行的話,我們必須明確地調用Ming庫,和使用其他模塊一樣,然後使用該庫裡的函數。由於使用前必須先加載模塊,與把Ming作為內部函數編譯的方式相比,要有一點性能的下降。
Ming庫是完全免費的,我們可以到創作者的網站下載,地址是:[url]http://www.opaque.net/ming/[/url],該網站....php.net/ming。
一旦我們正確安裝和配置了Ming庫,我們可以創建一個PHP對象,從PHP裡調用Ming的函數,通過調用函數和屬性,就可以定義我們的動畫了。在PHP模塊裡有13個對象,提供了Flash的一些功能,也有幾個命令可以來控制發行時的參數設定。下面就看看如何利用它來創建動畫。
使用Macromedia Flash創建SWF格式的動畫,我們先創建一些符號,Flash有三種類型的符號:圖形、動畫剪輯、按鈕。一旦我們創建了這些符號,我們就可以把它們復制到場景裡,每一個拷貝稱為一個實例,可以放任意多的實例到我們的場景裡。接下來就可以定義這些對象的行為了,也就是定義動作和運動軌跡,也可以定義動畫的參數,如尺寸大小和背景顏色,如果需要,可以保存成.SWF文件。
利用PHP的Ming庫創建Flash動畫,和Macromedia Flash一樣,必須先創建一些符號,這些符號就是PHP對象的實例,然後定義這些對象在動畫場景裡的位置和相互關系,接著定義對象在每一幀裡的動作,最後定義動畫自己。我們可以直接把SWF直接輸出到浏覽器,也可以保存成SWF格式的文件,以備後用。Flash的優點就是圖形化的用戶接口,利用Ming庫不但可以更加簡單的控制對象,而且具有無限的擴展性和再用性。
下面我們就用一個完整的例子來看看如何使用Ming庫:
<?php
/* 首先創建一個符號,並填充顏色 */
$square = new SWFShape();
$sqfill = $square->addFill(0, 0, 0xff);
$square->setRightFill($sqfill);
$square->movePenTo(-250,-250);
$square->drawLineTo(250,-250);
$square->drawLineTo(250,250);
$square->drawLineTo(-250,250);
$square->drawLineTo(-250,-250);
/* 在動畫剪輯裡使用上面的符號,並添加一些腳本 */
$sqclip = new SWFSprite();
$i = $sqclip->add($square);
$i->setDepth(1);
$sqclip->setframes(25);
$sqclip->add(new SWFAction("stop();"));
$sqclip->nextFrame();
$sqclip->add(new SWFAction("play();"));
for($n=0; $n<24; $n++) {
$i->rotate(-15);
$sqclip->nextFrame();
}
/* 下面創建按鈕 */
function rect($r, $g, $b) {
$s = new SWFShape();
$s->setRightFill($s->addFill($r, $g, $b));
$s->drawLine(500,0);
$s->drawLine(0,500);
$s->drawLine(-500,0);
$s->drawLine(0,-500);
return $s;
}
$b = new SWFButton();
$b->addShape(rect(0xff, 0, 0), SWFBUTTON_UP | SWFBUTTON_HIT);
$b->addShape(rect(0, 0xff, 0), SWFBUTTON_OVER);
$b->addShape(rect(0, 0, 0xff), SWFBUTTON_DOWN);
$b->addAction(new SWFAction("setTarget('/box'); gotoandplay(2);"), SWFBUTTON_MOUSEDOWN);
/* 下面創建動畫,並添加上面的符號和按鈕 */
$m = new SWFMovie();
$m->setDimension(4000,3000);
$i = $m->add($sqclip);
$i->setDepth(3);
$i->moveTo(1650, 400);
$i->setName("box");
$i = $m->add($b);
$i->setDepth(2);
$i->moveTo(1400,900);
/* 最後,我們把它輸出到浏覽器 */
header('Content-type: application/x-shockwave-flash');
$m->output();
?>