Author Archives: Robins

[MySQL] mysql 5.5 and 5.6 timestamp default default value CURRENT_TIMESTAMP problem

The behavior of TIMESTAMP in MySQL 5.5:

1. The implicit default value of the first TIMESTAMP NOT NULL field without a default value: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

2. The implicit default value of the TIMESTAMP NOT NULL field with no default value set later: 0000-00-00 00:00:00

3. The table creation statement that does not support multiple CURRENT_TIMESTAMP default values ​​of 5.5 is similar to this:

CREATE TABLE `audit_log` (
  `id` int ( 10 ) unsigned NOT NULL AUTO_INCREMENT,
  `ent_id` int ( 10 ) unsigned NOT NULL DEFAULT ' 0 ' ,
  `rule_id` int ( 10 ) unsigned NOT NULL DEFAULT ' 0 ' ,
  rules_detail` VARCHAR `( 2048 ) the NOT NULL the DEFAULT '' the COMMENT ' rule details ' ,
  sender_email` VARCHAR `( 512 ) the NOT NULL the DEFAULT '' the COMMENT ' Sender mail review ' ,
  `receiver_email` varchar( 512 ) NOT NULL DEFAULT '' COMMENT'recipient 's mailbox ' ,
  subject` VARCHAR `( 512 ) the NOT NULL the DEFAULT '' the COMMENT ' theme ' ,
  `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `start_time` timestamp NOT NULL DEFAULT ' 0000-00-00 00:00:00 ' ,
  `end_time` timestamp NOT NULL DEFAULT ' 0000-00-00 00:00:00 ' ,
  status` tinyint `( 3 ) the NOT NULL the DEFAULT unsigned ' 1 ' the COMMENT ' current state (by 1, 2 rejected rejected timeout 3, 4 by timeout) ' ,
  reviewer_leader` VARCHAR `( 512 ) the NOT NULL the DEFAULT '' the COMMENT ' moderator ' ,
  PRIMARY KEY (`id`),
  KEY `idx_ent_id` (`ent_id`)
) ENGINE =InnoDB DEFAULT CHARSET=utf8 COMMENT = ' Audit log table '

The behavior of TIMESTAMP in MySQL 5.6:

Supports multiple CURRENT_TIMESTAMP default values, but does not support setting the default value to 0000-00-00 00:00:00

5.6 can be like this:

  `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `end_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

[PHP] __autoload function will be called when class_exists does not exist

The definition of this function is as follows:

class_exists ( string $class_name [, bool $autoload = true ]): bool

The second parameter is whether to automatically call the autoload function

class_name 
class name. The matching of names is not partition case. 
Whether 

autoload calls __autoload by default.

Of course, it is now recommended to use the following function for automatic loading

spl_autoload_register(function ($class_name) { 
    require_once $class_name. ' .php ' ; 
});

If you don’t want to call, give false as the second parameter

[PHP] Solve the problem that the new version of Chrome’s same-site policy cannot record cookies across domains

In Chrome 80 and above, the same-site policy will be enabled by default

Samesite has the following values

Lax: Only requests from the same origin and top-level domain can carry cookies (equivalent to same-site)
Strict: Only requests from same origin can carry cookies (equivalent to same-origin)
None: No restrictions on the use of cookies, Just use

Solve cross-domain issues:

If you need to send cookies across domains, please use the None enumeration value to select no SameSite restriction. The None command needs to be used with the Secure command
Tip: The None enumeration value is a standard new enumeration value. Some old browsers do not recognize this enumeration value. Cause some problems.

PHP records cookie changes, do not use the setcookie method, directly use the header method to splice cookies:

It must be HTTPS request, the splice must meet cookie text format, for example, the following
record specified expiration time in the cookie value * .sina.net domain
header ( “Set-Cookie: key = value; Expires = expiration time; path = /; domain=.sina.net; SameSite=None; Secure”);

if (strpos($_SERVER[ ' HTTP_USER_AGENT ' ], ' Chrome/8 ' ) !== false ) {
 
    $expireTime =gmdate( " D, d MYH:i:s " , time()+SID_COOKIE_EXPIRE). " GMT " ;
    header( " Set-Cookie: SID= " .$mailSid. " ; expires= " .$expireTime. " ; path=/; domain=.sina.net; SameSite=None; Secure " );
 Secure " ); 
}

[PHP]json_encode Chinese JSON_UNESCAPED_UNICODE returns null in php5.3

Note that when json_encode Chinese, the default is unicode encoding, if you want to become Chinese, you need to increase the parameter JSON_UNESCAPED_UNICODE

But the JSON_UNESCAPED_UNICODE parameter is only supported above php5.4

So you can use the following code:

function json_encode2($array)
{
    if (version_compare(PHP_VERSION, ' 5.4.0 ' , ' < ' )){
        $str = json_encode($array);
        $str = preg_replace_callback( " #\\\u([0-9a-f]{4})#i " ,function($matchs){
             return iconv( ' UCS-2BE ' , ' UTF-8 ' , pack( ' H4 ' , $matchs[ 1 ]));
        },$str);
        return $str;
    } else {
         return json_encode($array, JSON_UNESCAPED_UNICODE);
    }
}

[Uniapp] Solve the error plus not defined when pushing

The code in the official document is a bit problematic. You can call the plus code with the following delay and wait for plus to load.

In addition, add the conditional compilation, only the code below the app will compile

                // #ifdef APP-PLUS
                     // trigger when the page loads   
                    setTimeout(function(){
                         if (plus){
                             var pinf = plus.push.getClientInfo();
                             var cid = pinf.clientid; // Client ID 
                            console.log (cid);
                              // Monitor system notification bar message click event 
                             plus.push.addEventListener( ' click ' , function(msg){  
                                  // Business logic code for processing click messages   
                             }, false);  
                              // Monitor and receive transparent message events   
                             plus.push.addEventListener( ' receive ' , function(msg){  
                                  // Business logic code for processing transparent messages   
                                 var options = {cover: false };
                                 plus.push.createMessage(msg, " RemoteMSG " ,options);
                             }, false );
                        }
                    }, 4000 );
                 // #endif

client_id can be obtained

Implement base64_decode in GO language to solve the problem of illegal characters

When using the base64 decode of the standard library, there will be an error of illegal characters. The following function is my test and it can be decrypted normally.

Pay attention to this parameter: base64.RawStdEncoding is the key to solving illegal characters

func Base64Decode(str string ) string {
    reader: = strings.NewReader(str)
    decoder: = base64.NewDecoder(base64.RawStdEncoding, reader)
     // Decoding 
    buf in streaming mode := make([] byte , 1024 )
     // Save the decoded data 
    dst := "" 
    for {
        n, err : = decoder.Read(buf)
        dst += string (buf[:n])
         if n == 0 || err != nil {
             break
        }
    }
    return dst
}

[MySQL] Note that MySQL5.1 does not support utf8mb4 Error 1115: Unknown character set:’utf8mb4′

MYSQL5.5 supports utf8mb4, and MYSQL5.1 does not support “utf8mb4”.

When connecting to the database, if utf8mb4 is specified, the error code that will appear is:

Error 1115: Unknown character set:’utf8mb4′

There will be utf8mb4 incompatibility.

 

So if you want to use utf8mb4 to store emoji characters, please upgrade as soon as possible

[Nginx] Solve the problem of being blocked by CORS policy: No’Access-Control-Allow-Origin’ header is present on the requested resource.

When a cross-domain request interface is required, it will appear

been blocked by CORS policy: No’Access-Control-Allow-Origin’ header is present on the requested resource.

It can be solved in code or directly in nginx

Similar to the code deployed by GOFLY under nginx

Just add the header header

                add_header Access-Control-Allow-Origin * ;
                add_header Access -Control-Allow-Methods ' GET, POST, OPTIONS ' ;

 

server{
       listen 80 ;
        server_name gofly.sopans.com;
        access_log   / var /log/nginx/ gofly.sopans.com.access.log main;
        location / static {
                root / var /www/html/go-fly; // Own deployment path 
        }        
        location / {
                add_header Access -Control-Allow-Origin * ;
                add_header Access -Control-Allow-Methods ' GET, POST, OPTIONS ' ;
                proxy_pass http: // 127.0.0.1:8081; 
                    proxy_http_version 1.1 ;
                    proxy_set_header X -Real- IP $remote_addr;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection " upgrade " ;
                    proxy_set_header Origin "" ;
        }
}

Javascript SecurityError: Failed to read the’localStorage’ property from’Window’: Access is denied for this document.

When using chrome incognito mode, third-party cookies are blocked by default 

If local storage or cookie is used, an error will be reported in js

Error SecurityError: Failed to read the ‘localStorage ‘ property from ‘Window’: Access is denied for this document.

Failed to read the the’localStorage’ property from windows: Access is denied for this document.

 

This problem can only be handled at the code level to be compatible, and no errors will be reported first.

            if (navigator.cookieEnabled&& typeof window.localStorage !== ' undefined ' ) {
      
            }