ios笔记

iOS9 隐藏状态栏方法

1.在Info.plist中增加 Status bar is initially hidden一行,选择为 YES,

2.还需增加 View controller-based status bar appearance 一行,选择为 NO。

这个方法支持iOS7及以后的系统,iOS9以后,通过[UIApplication sharedApplication] 取得app的单例,然后调用setStatusBarHidden方法隐藏 Status Bar的方法作废!

swift隐藏键盘

view.endEditing(true)
textfield.resignFirstResponder()

下一项

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if textfield.isFirstResponder() {
            textfield1.becomeFirstResponder()
        } else if textfield1.isFirstResponder() {
            textfield.becomeFirstResponder()
        } else {
            view.endEditing(true)
        }
        return true
    }

App Transport Security

在Info.plist中添加NSAppTransportSecurity类型Dictionary。
在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

php生成随机字符串

//生成随机数,用于生成salt
function random_str($length){
//生成一个包含 大写英文字母, 小写英文字母, 数字 的数组
$arr = array_merge(range(0, 9), range(‘a’, ‘z’), range(‘A’, ‘Z’));
$str = ”;
$arr_len = count($arr);
for ($i = 0; $i < $length; $i++){
$rand = mt_rand(0, $arr_len-1);
$str.=$arr[$rand];
}
return $str;
}

thinkphp3.2.1验证码2次验证

$verify = new \Think\Verify();
global $seKey;
$seKey = $verify->seKey;
function authcode($str){
$key = substr(md5($GLOBALS['seKey']), 5, 8);
$str = substr(md5($str), 8, 10);
return md5($key . $str);
}
function check($code, $id = '') {
$key = authcode($GLOBALS['seKey']).$id;
// 验证码不能为空
$secode = session($key);
if(empty($code) || empty($secode)) {
return false;
}
if(authcode(strtoupper($code)) == $secode['verify_code']) {
return true;
}
return false;
}
if (check(I('verify'))) {
$this->success('验证码正确', $_SERVER['HTTP_REFERER']);
} else {
$this->error('验证码错误', $_SERVER['HTTP_REFERER']);
}

php字符串加密解密

//加密解密
// $string: 明文 或 密文
// $operation:DECODE表示解密,其它表示加密
// $key: 密匙
// $expiry:密文有效期
function encrypt($string, $operation = 'DECODE', $key = '', $expiry = 0) {
// 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
$ckey_length = 4;

// 密匙
$key = md5($key ? $key : '5Gja4ESmXFzCN4HSExjpc9x4FVXljBSd');

// 密匙a会参与加解密
$keya = md5(substr($key, 0, 16));
// 密匙b会用来做数据完整性验证
$keyb = md5(substr($key, 16, 16));
// 密匙c用于变化生成的密文
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
// 参与运算的密匙
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
// 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),解密时会通过这个密匙验证数据完整性
// 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
// 产生密匙簿
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
// 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
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') {
// substr($result, 0, 10) == 0 验证数据有效性
// substr($result, 0, 10) - time() > 0 验证数据有效性
// substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) 验证数据完整性
// 验证数据有效性,请看未加密明文的格式
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return false;
}
} else {
// 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
// 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码
return $keyc.str_replace('=', '', base64_encode($result));
}

laravel5、php7、mongodb学习

使用laravel-mongodb

mongodb php driver

php7需要mongodb1.1x

PHP Driver PHP 5.3 PHP 5.4 PHP 5.5 PHP 5.6 PHP 7.0 HHVM 3.9
mongodb-1.1  
mongodb-1.0    
mongo-1.6    
mongo-1.5    

下载mongodb之后使用phpize安装

phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
#php.ini加
extension =mongodb.so
php composer.phar require jenssegers/mongodb  

提示错误,php扩展mbstring错误,用phpize编译安装mbstring

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - danielstjules/stringy 1.10.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - danielstjules/stringy 1.10.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - danielstjules/stringy 1.10.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - Installation request for danielstjules/stringy == 1.10.0.0 -> satisfiable by danielstjules/stringy[1.10.0].


Installation failed, reverting ./composer.json to its original content.

Laravel错误

5.1在php7环境上运行出现的错误

Fatal error: Uncaught ReflectionException: Class log does not exist in /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php:736 Stack trace: #0 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(736): ReflectionClass->__construct(‘log’) #1 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(626): Illuminate\Container\Container->build(‘log’, Array) #2 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(674): Illuminate\Container\Container->make(‘log’, Array) #3 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(837): Illuminate\Foundation\Application->make(‘log’) #4 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(800): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter)) #5 /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php(769): Illuminate\Container\Container in /home/liuman/jlcloud2/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 736

解决方案:

删除vendor文件夹和composer.lock

composer install

laravel 500错误是storage没有权限

解决方案:chmod -R 777 storage

cache错误

file_put_contents(/..cache/services.json): failed to open stream: Permission denied

解决方案:sudo php artisan cache:clear

php composer.phar install报错

先安装composer,然后运行php composer.phar install –no-scripts

PDO错误,已安装PDO还提示

[PDOException]
SQLSTATE[HY000] [2002] No such file or directory

解决方案:将.env下的DB_HOST=localhost改为DB_HOST=127.0.0.1

SQLSTATE[HY000] [1045]

解决方案:
php artisan cache:clear
php artisan config:clear

php artisan 没反应

composer dumpautoload

The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.

php artisan key:generate

[Dotenv\Exception\InvalidFileException]
Dotenv values containing spaces must be surrounded by quotes.

.env文件中文或者空格必须加引号

OpenSSL extension is required

打开php.ini启用php_openssl

Key path “file://storage\oauth-public.key” does not exist or is not readable

php artisan passport:install

[Composer\Downloader\TransportException] The “https://packagist.phpcomposer.com/p/provider-2017-07%24c3e8d929d5d06fa b76cef9c5b5e4305dbe89c1599b79e63eee70490a6b8df914.json” file could not be d ownloaded (HTTP/1.1 404 Not Found)

composer diagnose

[ErrorException] proc_open(): fork failed – Cannot allocate memory

/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024

/sbin/mkswap /var/swap.1

/sbin/swapon /var/swap.1

LogicException Key path “file:///storage/oauth-public.key” does not exist or is not readable

chown www-data:www-data storage/oauth-*.key

chmod 600 storage/oauth-*.key

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))

AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

C:\ProgramData\MySQL\MySQL Server 8.0\my.ini里default_authentication_plugin=caching_sha2_password改default_authentication_plugin=mysql_native_password

exception:”InvalidArgumentException”
file:”C:\web\jinguan\vendor\laravel\framework\src\Illuminate\Http\JsonResponse.php”
line:75
message:”Malformed UTF-8 characters, possibly incorrectly encoded”

Event::fire错误是redis没开

Class session does not exist

chmod -R 777 bootstrap/cache

Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)

php.ini打开扩展

extension=fileinfo

Laravel请求和输入

//获取输入数据,不用担心所使用的HTTP方法
$id = Input::get('id');
 
//可以指定默认值
$id = Input::get('id', 1);
 
//检测提交信息是否存在
if(Input::has('id'))
{
    echo Input::get('id');
}
 
//获取提交的所哟信息
print_r(Input::all());
 
//获取指定的几个提交信息
print_r(Input::only('id'));
 
//获取排除执行信息外的信息
print_r(Input::except('name'));
 
//用点符号获取数组形式的输入信息
$input = Input::get('users.0.name');

php笔记

1、array_merge_recursive  当有重复的键名时,值不会被覆盖,而是将多个相同键名的值递归组成一个数组。

2、cos函数和javascript的cos不同,php的cos参数为弧度,js的cos为角度。

3、php递归使用局部变量传参地址&。

4、stripslashes取消转义。

5、json_encode($data,JSON_UNESCAPED_UNICODE)//中文不编译成Unicode编码。