3.8,给你的女友吧,程序员也是懂浪漫的。

<?php
header("Content-type: image/gif");

$width    = 520;
$size     = 13;
$img      = imagecreate($width, $width);
$bg_color = imagecolorallocate($img, 0, 0, 0);
$red      = imagecolorallocate($img, 255, 0, 0);

for ($i = 0; $i <= $width; $i++) {
    $x = (16 * pow(sin($i), 3)) * $size + $width / 2;
    $y = (13 * cos($i) - 5 * cos(2 * $i) - 2 * cos(3 * $i) - cos(4 * $i)) * $size + $width / 2;
    imagesetpixel($img, $x, $y, $red);
}
$img = imagerotate($img, 180, 0);
imagegif($img);
imagedestroy($img);

PHP7安装redis扩展

一、安装扩展
1.下载并编译安装

wget https://github.com/phpredis/phpredis/archive/3.0.0.tar.gz
tar zxvf 3.0.0.tar.gz
cd phpredis-3.0.0/

phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

2.在配置文件/usr/local/php/etc/php.ini中加入

extension="redis.so"

3.重启PHP服务 nginx、apache 或 php-fpm

二、安装Redis

wget http://download.redis.io/releases/redis-3.2.5.tar.gz
tar zxvf redis-3.2.5.tar.gz
cd redis-3.2.5
make
make install

make install 之后,会在/usr/local/bin下面生成几个文件 redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server

移动文件,便于管理:(所有源代码安装的软件都安装在/usr/local下)
创建两个文件夹,bin用于存放命令,etc用于存放配置文件。

mkdir -p /usr/local/redis/bin
mkdir -p /usr/local/redis/etc

将redis-3.2.5文件夹下的redis.conf复制到/usr/local/redis/etc/

修改配置文件中的 daemonize no 改为 daemonize yes,可以在后台运行redis

配置启动项 utils 文件夹下面的 redis_init_script 文件
cp redis_init_script /etc/init.d/redisd

#!/bin/bash
# chkconfig:   2345 90 10
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
                echo "Redis started"
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    status)
        if netstat -tnpl | grep -q redis-server; then
            PID=`pidof redis-server`
            echo "Redis (pid $PID) is running..."
        else
            echo "Redis is stopped"
            exit 0
        fi
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

chkconfig redisd on

Let's Encrypt

克隆脚本

sudo git clone https://github.com/diafygi/acme-tiny.git  
cd acme-tiny

创建Let's Encrypt私钥

openssl genrsa 4096 > account.key

创建CSR(Certificate Signing Request,证书签名请求) 文件

将需要加密的域名加到下面的代码中,目前一张证书最多可以加密 100 个域名:

openssl genrsa 4096 > domain.key     
openssl req -new -sha256 -key domain.key -subj "/" -reqexts SAN -config <(cat /etc/pki/tls/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:yoursite.com,DNS:www.yoursite.com")) > domain.csr

证明你拥有该域名

手动生成challenges目录,用来存放验证文件(路径可以根据需要修改)

mkdir -p /var/www/challenges

配置nignx的80端口

server {
    listen 80;
    server_name yoursite.com www.yoursite.com;
    location /.well-known/acme-challenge/ {
        alias /var/www/challenges/;
        try_files $uri =404;
    }
}

获取签名证书

sudo chmod +x acme_tiny.py  
python acme_tiny.py --account-key ./account.key --csr ./domain.csr --acme-dir /var/www/challenges/ > ./signed.crt

安装证书

针对nginx, 你还需要将 Let's Encrypt 的中间件证书 intermediate.pem 内容附加在签名证书signed.crt之后:

wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem  
cat signed.crt intermediate.pem > chained.pem

server {
    listen 443;
    server_name yoursite.com www.yoursite.com;

    ssl on;
    ssl_certificate /path/to/chained.pem;
    ssl_certificate_key /path/to/domain.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA;
    ssl_session_cache shared:SSL:50m;
    ssl_prefer_server_ciphers on;

    #...你的其他配置
}

证书自动更新定时任务

#!/usr/bin/sh

python /path/to/acme_tiny.py --account-key /path/to/account.key --csr /path/to/domain.csr --acme-dir /var/www/challenges/ > /tmp/signed.crt || exit

wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem

cat /tmp/signed.crt intermediate.pem > /path/to/chained.pem

service nginx reload

定时任务可以设置为每个月执行一次:

0 0 1 * * /path/to/renew_cert.sh 2>> /var/log/acme_tiny.log

设置网站跳转

location / {
    if ($host = "cjango.com") {
        return 301 https://www.cjango.com$request_uri;
    }
}

composer安装和基本使用

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

使用composer -V查看安装版本

修改中文镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

composer 升级

composer self-update

升级依赖包

composer update

PHP提高性能的几个设置

$sudo sysctl vm.nr_hugepages=512
zend_extension="opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.enable=1
opcache.file_cache=/tmp
opcache.huge_code_pages=1
opcache.validate_timestamps=1
opcache.revalidate_freq=0

简单的身份证号码校验

function checkIdCard($idcard)
{
    // 只能是18位
    if (strlen($idcard) != 18) {
        return false;
    }
    // 取出本体码
    $idcard_base = substr($idcard, 0, 17);
    // 取出校验码
    $verify_code = substr($idcard, 17, 1);
    // 加权因子
    $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    // 校验码对应值
    $verify_code_list = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    // 根据前17位计算校验码
    $total = 0;
    for ($i = 0; $i < 17; $i++) {
        $total += substr($idcard_base, $i, 1) * $factor[$i];
    }
    // 取模
    $mod = $total % 11;
    // 比较校验码
    if ($verify_code == $verify_code_list[$mod]) {
        return true;
    } else {
        return false;
    }
}

PHP 自 5.2 到 5.6 中新增的功能详解[转]

因为 PHP 那“集百家之长”的蛋疼语法,加上社区氛围不好,很多人对新版本,新特征并无兴趣。
本文将会介绍自 PHP5.2 起,直至 PHP5.6 中增加的新特征。

PHP5.2 以前:autoload, PDO 和 MySQLi, 类型约束
PHP5.2:JSON 支持
PHP5.3:弃用的功能,匿名函数,新增魔术方法,命名空间,后期静态绑定,Heredoc 和 Nowdoc, const, 三元运算符,Phar
PHP5.4:Short Open Tag, 数组简写形式,Traits, 内置 Web 服务器,细节修改
PHP5.5:yield, list() 用于 foreach, 细节修改
PHP5.6: 常量增强,可变函数参数,命名空间增强







- 阅读全部 -

扫一扫