PHP 상용 클래스 공유(기록)
PHP 7.1 로 전통 적 인 암호 화 함수 mcrypt 를 폐기 하기 시 작 했 습 니 다.encrypt 를 openssl 로 변경 합 니 다.encrypt,따라서 이러한 종류의 PHP 버 전 은 다음 과 같 습 니 다.(PHP 5>=5.3.0,PHP 7)
METHOD = $method;
}
}
/**
*
*
* @param String
* @param string , ,
* @return void
*/
public function encrypt($input, $key = '') {
if (empty($key)) $key = $this->KEY;
$data = openssl_encrypt($input, $this->METHOD, $key, OPENSSL_RAW_DATA);
$data = base64_encode($data);
return $data;
}
/**
*
*
* @param String
* @param string , ,
* @return void
*/
public function decrypt($input, $key = '') {
if (empty($key)) $key = $this->KEY;
$data = openssl_decrypt(base64_decode($input), $this->METHOD, $key, OPENSSL_RAW_DATA);
return $data;
}
}
메 일 발송 클래스
PHPMailer 를 다운로드 하고 압축 을 푼 후 프로젝트 에 넣 고 클래스 의 도입 경 로 를 수정 합 니 다.
_init();
$this->_baseURL = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
}
private function _init() {
if ($this->_email == null) {
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = get_global_config('mail.Host');
$mail->SMTPAuth = true;
$mail->Username = get_global_config('mail.Username');
$mail->Password = get_global_config('mail.Password');
$mail->SMTPSecure = get_global_config('mail.SMTPSecure');
$mail->Port = get_global_config('mail.Port');
$mail->setFrom(get_global_config('mail.Username'), get_global_config('mail.Fromname'));
$this->_email = $mail;
return true;
} catch (Exception $e) {
// return null;
return false;
}
}
return true;
}
public function sendEmail($toEmail, $toUsername, $subject, $body) {
if (!$this->_email) return false;
try {
$this->_email->addAddress($toEmail, $toUsername);
$this->_email->isHTML(true);
$this->_email->Subject = $subject;
$this->_email->Body = $body;
// HTML
$this->_email->AltBody = $body;
$this->_email->send();
// echo 'Message has been sent';
return true;
} catch (Exception $e) {
return false;
// echo 'Message could not be sent.';
// echo 'Mailer Error: ' . $this->_email->ErrorInfo;
}
}
}
아 날로 그 자물쇠
Redis 아 날로 그 프로 세 스 잠 금 을 사용 하여 discuz 의 프로 세 스 잠 금 소스 코드 를 참고 하 십시오.
cache = Roc::redis();
$this->cache->select(Roc::get('redis.db'));
}
/**
*
*
* @param string
* @param integer
* @return boolean
*/
public function islocked($process, $ttl = 0) {
$ttl = $ttl < 1 ? 600 : intval($ttl);
return $this->_status('get', $process) || $this->_find($process, $ttl);
}
/**
*
*
* @param string
* @return void
*/
public function unlock($process) {
$this->_status('rm', $process);
$this->_cmd('rm', $process);
}
private function _status($action, $process) {
static $plist = array();
switch ($action) {
case 'set' : $plist[$process] = true; break;
case 'get' : return !empty($plist[$process]); break;
case 'rm' : $plist[$process] = null; break;
case 'clear' : $plist = array(); break;
}
return true;
}
private function _find($name, $ttl) {
if(!$this->_cmd('get', $name)) {
$this->_cmd('set', $name, $ttl);
$ret = false;
} else {
$ret = true;
}
// $this->_status('set', $name);
return $ret;
}
private function _cmd($cmd, $name, $ttl = 0) {
return $this->_process_cmd_memory($cmd, $name, $ttl);
// static $allowmem;
// if($allowmem === null) {
// $allowmem = Roc::get('redis.db');
// }
// if($allowmem) {
// return self::_process_cmd_memory($cmd, $name, $ttl);
// } else {
// return false;
// // return self::_process_cmd_db($cmd, $name, $ttl);
// }
}
private function _process_cmd_memory($cmd, $name, $ttl = 0) {
$ret = '';
$name = 'process_lock_'.$name;
switch ($cmd) {
case 'set' :
$ret = $this->cache->setex($name, time(), $ttl);
// $ret = memory('set', 'process_lock_'.$name, time(), $ttl);
break;
case 'get' :
$ret = $this->cache->get($name);
// $ret = memory('get', 'process_lock_'.$name);
break;
case 'rm' :
$ret = $this->cache->delete($name);
// $ret = memory('rm', 'process_lock_'.$name);
}
echo $cmd . ':';
var_dump($ret);
return $ret;
}
private function _process_cmd_db($cmd, $name, $ttl = 0) {
$ret = '';
switch ($cmd) {
case 'set':
$ret = C::t('common_process')->insert(array('processid' => $name, 'expiry' => time() + $ttl), FALSE, true);
break;
case 'get':
$ret = C::t('common_process')->fetch($name);
if(empty($ret) || $ret['expiry'] < time()) {
$ret = false;
} else {
$ret = true;
}
break;
case 'rm':
$ret = C::t('common_process')->delete_process($name, time());
break;
}
return $ret;
}
}
Redis
캐 시 를 찾 을 수 없 을 때 대응 하 는 리 셋 작업 을 지원 하 는 간단 한 Redis 작업 클래스
cache = new Redis();
}
// ============= String
/**
*
*
* @param String key
* @param String/Object , 。
* @param Int
* @return Boolean
*/
public function set($key, $value, $ttl = null) {
if (is_array($value)) $value = serialize($value);
if ($ttl !== null && $ttl > 0)
return $this->cache->setex($this->_key($key), $ttl, $value);
else
return $this->cache->set($this->_key($key), $value);
}
/**
*
*
* @param String
* @param Data , ,
* @return Data
*/
public function get($key, $callback = false) {
$res = $this->cache->get($this->_key($key));
if ($res === false && $callback === true) {
$method_name = "_$key";
if (method_exists($this, $method_name)) {
return $this->$method_name();
} else {
return false;
}
}
// , 。
// @ warning。
if (@unserialize($res) !== false) return unserialize($res);
return $res;
}
/**
*
*
* @param String
* @return true or false
*/
public function rm($key) {
return $this->cache->delete($this->_key($key));
}
public function inc($key, $step = 1) {
return $this->cache->incr($this->_key($key), $step);
}
public function dec($key, $step = 1) {
return $this->cache->decr($this->_key($key), $step);
}
private function _key($key) {
return $this->prefix . $key;
}
public function getMultiple($keys) {
$_keys = [];
foreach ($keys as $key) $_keys[] = $this->_key($key);
return $this->cache->getMultiple($_keys);
}
// ============== Hash
public function hset($hash, $key, $value) {
return $this->cache->hSet($hash, $key, $value);
}
public function hget($hash, $key) {
return $this->cache->hGet($hash, $key);
}
public function hgetall($hash) {
return $this->cache->hGetAll($hash);
}
public function hvals($hash) {
return $this->cache->hVals($hash);
}
public function hkeys($hash) {
return $this->cache->hKeys($hash);
}
// ================
// Your callback code here.
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
PHPMailer는 SMTP 서버에 연결할 수 없으며 SMTP 대소문자 수정과 관련이 없습니다.echo "尝试连接 $host ...<br>\r if(!$Get_host){ echo "域名解析为 $Get_host ...<br>\r $fp = pfsockopen($host,25,&$errno,&$errstr,60...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.