php mysql博客github_蘑菇博客 – 第20页 – A Developer Note

Varnish安装记录

1. 执行以下脚本

#!/bin/bash

yum install -y gcc gcc-c++ make wget automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig libedit libedit-devel

wget https://repo.varnish-cache.org/source/varnish-3.0.6.tar.gz

tar zxvf varnish-3.0.6.tar.gz

cd varnish-3.0.6

./configure --prefix=/usr/local/varnish

make && make install

groupadd varnish

useradd -d /var/lib/varnish -g varnish -s /sbin/nologin varnish

mkdir /etc/varnish/

cp redhat/varnish.initrc /etc/init.d/varnish

cp redhat/varnish.sysconfig /etc/sysconfig/varnish

cp redhat/varnish_reload_vcl /usr/local/varnish/varnish_reload_vcl

ln -s /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/default.vcl

ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/varnishd

ln -s /usr/local/varnish/varnish_reload_vcl /usr/bin/varnish_reload_vcl

ln -s /usr/local/varnish/bin/varnishadm /usr/bin/varnishadm

chmod +x /etc/init.d/varnish

uuidgen> /etc/varnish/secret

chmod 0600 /etc/varnish/secret

2. 修改/etc/varnish/default.conf

3. 启动Varnish

发布于2015年2月2日

归类为服务器

Yii2学习笔记

1.下载框架

首先下载 Yii2 的 Basic App 版本,下载地址:https://github.com/yiisoft/yii2/releases

2.安装框架

修改 yii/config/web.php 文件中 cookieValidationKey 的值。

3.开启重写

(1)修改 yii/config/web.php 文件,给 $config 的 components 项添加以下代码:

'urlManager' => [

'enablePrettyUrl' => true,

// 'enableStrictParsing' => true,

'showScriptName' => false,

'rules' => [

// ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],

],

]

(2)在web目录下新建.htaccess文件,写入以下代码:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

4.新建控制器

在 yii/controllers 目录下新建控制器 UserController.php

namespace frontend\controllers;

use Yii;

use yii\web\Controller;

use frontend\models\DefaultModel;

class DefaultController extends Controller

{

public function actionIndex()

{

// return $this->render('index');

}

}

5.配置数据库连接信息

修改 yii/config/db.php 文件,填写相应的数据库信息。

如果需要添加数据库前缀,增加 tablePrefix 选项。

6.新建模块操作数据库

在 yii/models 目录下新建模块,默认格式如下:

namespace frontend\models;

use Yii;

use yii\db\ActiveRecord;

class DefaultModel extends ActiveRecord

{

public static function tableName()

{

return 'table';

}

public function rules()

{

return [

[['name', 'title'], 'required'],

[['name'], 'string', 'max' => 10],

[['name'], 'unique']

];

}

}

7.新建视图

use yii\helpers\Html;

use yii\bootstrap\ActiveForm;

$this->title = 'title';

?>

= Html::encode($this->title) ?>

<?php $form = ActiveForm::begin(['id' => 'form-id']); ?>

8.获取CSRF的值

Yii::$app->request->csrfToken

9.获取$_POST的值

Yii::$app->request->isPost

Yii::$app->request->post()

10.数据库查询

yii\db\ActiveRecord::find()->where()->orderby()->asArray()->limit(10)->one()/count()/all();

yii\db\ActiveRecord::findBySql($sql)->all();

yii\db\ActiveRecord::findOne($where);

yii\db\ActiveRecord::findAll($where);

yii\db\ActiveRecord->save()

yii\db\ActiveRecord->insert()

yii\db\ActiveRecord->update()

yii\db\ActiveRecord->delete()

11. 获取已登录用户信息

# uid

Yii::$app->user->identity->id

# username

Yii::$app->user->identity->username

12. 控制器中的一些方法

# 返回首页

return $this->goHome();

# 返回上一页

return $this->goBack();

# 跳转

return $this->redirect(['/home']);

# 刷新页面

return $this->refresh();

13. 一些加密方法

# 获取一个随机密钥

Yii::$app->security->generateRandomString();

# 密码加密

Yii::$app->security->generatePasswordHash($password);

# 验证密码

Yii::$app->security->validatePassword($password, $this->password_hash);

14. 用户数据表

CREATE TABLE `user` (

`ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',

`username` varchar(200) CHARACTER SET utf8mb4 NOT NULL COMMENT '用户名',

`auth_key` varchar(32) NOT NULL COMMENT '身份校验密钥',

`password_hash` varchar(200) NOT NULL COMMENT '密码哈希值',

`password_reset_token` varchar(200) DEFAULT '' COMMENT '重置密码密钥',

`phone` varchar(20) NOT NULL COMMENT '手机号',

`status` tinyint(4) NOT NULL DEFAULT '10' COMMENT '状态',

`created_at` int(11) NOT NULL COMMENT '创建时间',

`updated_at` int(11) NOT NULL COMMENT '更新时间',

PRIMARY KEY (`ID`),

UNIQUE KEY `username` (`username`),

UNIQUE KEY `phone` (`phone`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

发布于2015年1月23日

归类为php

W3 Total Cache精简

1. 在文件w3-total-cache中注释/lib/EDD/integration.php

2. 修改/lib/W3/Menus.php,注释$pages_tail,Monitoring

3. 修改/inc/options/dashboard.php文件,删除id为w3tc-dashboard-widgets的div

4. 修改inc/define.php文件,添加以下代码:

define('W3TC_PRO', true);

define('W3TC_ENTERPRISE', true);

5. 修改/lib/W3/Plugin/TotalCache.php文件,注释add_action(admin_bar_menu)和Performance optimized by W3 Total Cache

6. 添加中文语言包

7. 关闭浏览器缓存选项

8. 修改配置文件中的 evaluation.reminder 默认值为 1441247217

9. 修改配置文件中的 pgcache.enabled objectcache.enabled dbcache.enabled fragmentcache.enabled 默认值为 true

发布于2014年11月9日

归类为php

MySQL快速连接函数

1. 使用原生mysql函数

$config['db'] = array(

'host'=>'localhost',

'port'=>'3306',

'user'=>'root',

'pwd'=>'',

'name'=>'test',

'charset'=>'utf8',

);

mysql_link($config);

function mysql_link($config){

$con = mysql_connect($config['db']['host'].':'.$config['db']['port'], $config['db']['user'], $config['db']['pwd']);

mysql_select_db($config['db']['name']);

mysql_set_charset($config['db']['charset']);

if (!$con){

die('Could not connect: ' . mysql_error());

}

$sql = "INSERT INTO `wp_express_comments` (`post_id`, `type`) VALUES ('12', 'love')";

$query = mysql_query($sql, $con);

if($query === false){

echo 0;

}else{

echo 1;

}

mysql_close($con);

}

2. 使用mysqli函数

$mysqli = new mysqli('127.0.0.1', 'mogu_db', '123456', 'mogu_db');

if ($mysqli->connect_errno) {

printf("Connect failed: %sn", $mysqli->connect_error);

exit();

}

$query = $mysqli->query( "show tables" );

$result = array();

if ($query) {

while ($row = $query->fetch_assoc()) {

$result[] = $row;

}

$query->free();

}

$mysqli->close();

var_dump($result);

发布于2014年10月23日

归类为php

wordpress后台只显示指定分类

function mogublog_load_post_new(){

add_filter('query', 'mogublog_custom_cats');

}

add_filter('load-post-new.php', 'mogublog_load_post_new');

function mogublog_custom_cats($query){

if(strpos($query, "WHERE tt.taxonomy IN ('category')") !== false){

global $current_user;

get_currentuserinfo();

$user_id = $current_user->ID;

if($user_id == '1'){

$query = str_replace("WHERE tt.taxonomy IN ('category')", "WHERE tt.taxonomy IN ('category') AND t.term_id IN (1, 2) ", $query);

}

}

return $query;

}

发布于2014年10月22日

归类为php

Android字符串md5加密

[java]

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class MD5 {

public static String get(String id, String num) {

String str = id + num;

byte[] digest = null;

try {

digest = MessageDigest.getInstance("MD5").digest(

str.getBytes("UTF-8"));

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

StringBuilder md5 = new StringBuilder(digest.length * 2);

for (byte b : digest) {

if ((b & 0xFF) < 0x10)

md5.append("0");

md5.append(Integer.toHexString(b & 0xFF));

}

return md5.toString();

}

}

[/java]

发布于2014年10月18日

创建常用数据表的SQL语句

1. 用户表

CREATE TABLE IF NOT EXISTS `mogu_users` (

`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,

`user_pass` varchar(64) NOT NULL DEFAULT '',

`user_name` varchar(250) NOT NULL DEFAULT '',

`user_email` varchar(100) NOT NULL DEFAULT '',

`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',

`user_activation_key` varchar(60) NOT NULL DEFAULT '',

`user_status` int(11) NOT NULL DEFAULT '0',

PRIMARY KEY (`user_id`),

KEY `user_name` (`user_name`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

2. 数据表

CREATE TABLE IF NOT EXISTS `meta` (

`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,

`post_id` bigint(20) unsigned NOT NULL DEFAULT '0',

`meta_key` varchar(255) DEFAULT NULL,

`meta_value` text,

PRIMARY KEY (`meta_id`),

KEY `post_id` (`post_id`),

KEY `meta_key` (`meta_key`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

发布于2014年10月14日

归类为服务器

mogu file cache

$config = array(

'work_dir'=>'D:SoftEasyPHP-12.1wwwhtmlcache',

'default_timeout'=>600,

);

class mogu_file_cache{

public $work_dir;

public $default_timeout;

function __construct(){

global $config;

if(is_writeable($config['work_dir'])){

$this->work_dir = $config['work_dir'];

$this->default_timeout = $config['default_timeout'];

}else{

return false;

}

}

function exists($key){

return $this->get($key);

}

/*

*

*/

function set($key, $value, $time = 0){

if($time == 0){

$time = $this->default_timeout;

}

$key = md5($key);

$cache_dir = $this->work_dir.'/'.substr($key, 0, 3).'/'.substr($key, 3, 3);

if(!file_exists($cache_dir)){

mkdir($cache_dir, 0755, true);

}

$result = array(

'value'=>$value,

'time'=>time() + $time

);

return file_put_contents($cache_dir.'/'.$key, serialize($result));

}

function get($key){

$key = md5($key);

$cache_dir = $this->work_dir.'/'.substr($key, 0, 3).'/'.substr($key, 3, 3);

$cache_file = $cache_dir.'/'.$key;

if(file_exists($cache_file)){

$result = unserialize(file_get_contents($cache_file));

if( $result['time'] > time() ){

return $result['value'];

}else{

unlink($cache_file);

return false;

}

}else{

return false;

}

}

function replace($key, $value, $time = 0){

return $this->set($key, $value, $time);

}

function del($key){

$key = md5($key);

$cache_dir = $this->work_dir.'/'.substr($key, 0, 3).'/'.substr($key, 3, 3);

unlink($cache_dir.'/'.$key);

}

function clear(){

}

}

$cache = new mogu_file_cache();

$cache->set('user%&^&*^%', 'mogu');

$result = $cache->get('user%&^&*^%');

var_dump($result);

发布于2014年10月14日

归类为php

Redis客户端

根据redis协议写的php连接redis服务的客户端程序,目前只实现了简单的set和get功能。

class redis{

protected $con;

public $host;

public $port;

function __construct($config = '', $auto = 0){

$this->host = $config['redis']['host'];

$this->port = $config['redis']['port'];

if($auto == 1){

// 自动连接

$socket = $this->connect();

return $socket;

}

}

function connect(){

if(!empty($this->con)){

fclose($this->con);

$this->con = null;

}

// 发起连接

$socket = fsockopen($this->host, $this->port, $errno, $errstr);

if(!$socket){

$this->error('Connection error: '.$errno.':'.$errstr);

return false;

}

$this->con = $socket;

return $socket;

}

function send($args){

$command = '*'.count($args)."rn";

foreach($args as $arg){

$command .= '$'.strlen($arg)."rn".$arg."rn";

}

$w = fwrite($this->con, $command);

if($w){

$reply = $this->reply();

return $reply;

}else{

return false;

}

}

function reply(){

$reply = fgets($this->con);

if($reply == false){

return false;

}

$reply = trim($reply);

$result = null;

switch ($reply[0]){

case '-':

$this->error('error: '.$reply);

return false;

case '+':

return substr($reply, 1);

case '$':

if ($reply=='$-1'){

return null;

}

$size = intval(substr($reply, 1));

if($size > 0){

$result = stream_get_contents($this->con, $size);

}

fread($this->con, 2);

break;

case '*':

break;

case ':':

return intval(substr($reply, 1));

default:

$this->error('Non-protocol answer: '.print_r($reply, 1));

return false;

}

return $result;

}

function set(){

$args = func_get_args();

array_unshift($args, 'set');

return $this->send($args);

}

function get(){

$args = func_get_args();

array_unshift($args, 'get');

return $this->send($args);

}

protected function error($msg){

trigger_error($msg, E_USER_WARNING);

}

function __destroy(){

if(!empty($this->con)){

fclose($this->con);

}

}

}

$config = array('redis'=>array('host'=>'localhost', 'port'=>'6379'));

$con = new redis($config, 1);

// $reply = $con->set('name', 'mogu1');

$reply = $con->get('name');

var_dump($reply);

发布于2014年9月23日

归类为php

安装Memcached服务端

#!/bin/bash

yum -y install gcc make wget

wget http://mogu-lamp.qiniudn.com/libevent-2.0.21-stable.tar.gz

wget http://mogu-lamp.qiniudn.com/memcached-1.4.20.tar.gz

tar zxvf libevent-2.0.21-stable.tar.gz

cd libevent-2.0.21-stable

./configure --prefix=/usr

make && make install

cd ..

tar zxvf memcached-1.4.20.tar.gz

cd memcached-1.4.20

./configure --prefix=/usr/local/memcached

make && make install

cd ..

ln -s /usr/lib/libevent-2.0.so.5 /lib/libevent-2.0.so.5

ln -s /usr/local/memcached/bin/memcached /usr/bin/memcached

ldconfig

echo '#! /bin/bash

#

# memcached: MemCached Daemon

#

# chkconfig: - 90 25

# description: MemCached Daemon

#

### BEGIN INIT INFO

# Provides: memcached

# Required-Start: $syslog

# Required-Stop: $syslog

# Should-Start: $local_fs

# Should-Stop: $local_fs

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: memcached - Memory caching daemon

# Description: memcached - Memory caching daemon

### END INIT INFO

PORT=11211

USER=root

MAXCONN=1024

CACHESIZE=64

OPTIONS=""

RETVAL=0

prog="memcached"

start () {

echo -n $"Starting $prog: "

memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS

RETVAL=$?

echo

[ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached

}

stop () {

echo -n $"Stopping $prog: "

kill `cat /var/run/memcached.pid`

RETVAL=$?

echo

if [ $RETVAL -eq 0 ] ; then

rm -f /var/lock/subsys/memcached

rm -f /var/run/memcached.pid

fi

}

restart () {

stop

start

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

restart|reload)

restart

;;

*)

echo $"Usage: $0 {start|stop|status|restart|reload}"

exit 1

esac

exit $?' > /etc/init.d/memcached

chmod +x /etc/init.d/memcached

# 设置开机启动

chkconfig memcached --add

chkconfig memcached on

service memcached start

客户端

#!/bin/bash

wget http://pecl.php.net/get/memcache-2.2.7.tgz

tar zxvf memcache-2.2.7.tgz

cd memcache-2.2.7

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make && make install

cd ..

/*

[Memcache]

extension = memcache.so

*/

发布于2014年9月20日

归类为服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值