Category Archives: PHP

[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);
    }
}

[PHP] Solve the 500 error problem-nginx and fpm have no error logs

When deploying the code, a 500 error occurred 

However, neither the error log of www-error.log defined under php-fpm nor the error log defined by nginx shows anything

You can use the following method

 strace $(pidof’php-fpm’|sed’s/\([0-9]*\)/-p \1/g’) -e write -e read -s 1024

 

Use strace to monitor the read and write function calls of the fpm process

You can see errors like this

 

Wrong password or no permission when connecting to mysql

Just do the corresponding processing

Laravel5.5 Error: SQLSTATE[42000] Syntax error or access violation 1071 Specified key was too long; max k

Laravel5.5 Configuration Background management Package Voyager problem: SQLSTATE[42000]: Syntax Error or Access Restriction: 1071 Specified key was too long; max key length is 1000 bytes
I heard voyager is good. I’ve tried to create a new file.
D:\wamp64\WWW\laravel\myproject
lambda > PHP artisan voyager: install – with – dummy

Setting up the hooks

Hooks are now ready to use! Go ahead and try to "php artisan hook:install test-hook"
Publishing the Voyager assets, database, language, and config files
Copied Directory [\vendor\tcg\voyager\publishable\assets] To [\public\vendor\tcg\voyager\assets]
Copied Directory [\vendor\tcg\voyager\publishable\database\migrations] To [\database\migrations]
Copied Directory [\vendor\tcg\voyager\publishable\database\seeds] To [\database\seeds]
Copied Directory [\vendor\tcg\voyager\publishable\demo_content] To [\storage\app\public]
Copied Directory [\vendor\tcg\voyager\publishable\lang] To [\resources\lang]
Publishing complete.
Publishing complete.
Migrating the database tables into your application
Migration table created successfully.

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

  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

The problem is too big, according to the official laravel document, change the file of Laravel 5.5 (../app/will/appServiceProvider. PHP) content, below

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; //Import Schema


class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191); //Solved by increasing StringLength
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

D: \wamp64\www\laravel\myproject


Setting up the hooks
Hooks are now ready to use! Go ahead and try to "php artisan hook:install test-hook"
Publishing the Voyager assets, database, language, and config files
Copied Directory [\vendor\tcg\voyager\publishable\assets] To [\public\vendor\tcg\voyager\assets]
Copied Directory [\vendor\tcg\voyager\publishable\database\migrations] To [\database\migrations]
Copied Directory [\vendor\tcg\voyager\publishable\database\seeds] To [\database\seeds]
Copied Directory [\vendor\tcg\voyager\publishable\demo_content] To [\storage\app\public]
Copied Directory [\vendor\tcg\voyager\publishable\lang] To [\resources\lang]
Publishing complete.
Publishing complete.
Migrating the database tables into your application
Migration table created successfully.

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `translations` add unique `translations_table_name_column_nam
  e_foreign_key_locale_unique`(`table_name`, `column_name`, `foreign_key`, `locale`))

  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

The database shows that users and Migration imported successfully, while others failed. Finally, I went to Github to find the solution
https://github.com/the-control-group/voyager/issues/901#issuecomment-291470960

1. Update the "config/database.php" for 'mysql'
'engine' => null,
with
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
++++++++++++++++++++++++++++++++++++++++++++

2. Update the "app/Providers/AppServiceProvider.php" with
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Specified key was too long error, Laravel News post:
        Schema::defaultStringLength(191);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
++++++++++++++++++++++++++++++++++++++++++++

3. Execute Command:
php artisan cache:clear
php artisan config:clear
php artisan voyager:install --with-dummy
Now, everything works! :)

Has a similar problem here: https://github.com/the-control-group/voyager/issues/1333
Make some changes as described above, and then install
D:\wamp64\www\laravel\myproject
Lambda & gt; PHP artisan voyager: install – with – dummy


Setting up the hooks
Hooks are now ready to use! Go ahead and try to "php artisan hook:install test-hook"
Publishing the Voyager assets, database, language, and config files
Copied Directory [\vendor\tcg\voyager\publishable\assets] To [\public\vendor\tcg\voyager\assets]
Copied Directory [\vendor\tcg\voyager\publishable\database\migrations] To [\database\migrations]
Copied Directory [\vendor\tcg\voyager\publishable\database\seeds] To [\database\seeds]
Copied Directory [\vendor\tcg\voyager\publishable\demo_content] To [\storage\app\public]
Copied Directory [\vendor\tcg\voyager\publishable\lang] To [\resources\lang]
Publishing complete.
Publishing complete.
Migrating the database tables into your application
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2016_01_01_000000_add_voyager_user_fields
Migrated:  2016_01_01_000000_add_voyager_user_fields
Migrating: 2016_01_01_000000_create_data_types_table
Migrated:  2016_01_01_000000_create_data_types_table
Migrating: 2016_01_01_000000_create_pages_table
Migrated:  2016_01_01_000000_create_pages_table
Migrating: 2016_01_01_000000_create_posts_table
Migrated:  2016_01_01_000000_create_posts_table
Migrating: 2016_02_15_204651_create_categories_table
Migrated:  2016_02_15_204651_create_categories_table
Migrating: 2016_05_19_173453_create_menu_table
Migrated:  2016_05_19_173453_create_menu_table
Migrating: 2016_10_21_190000_create_roles_table
Migrated:  2016_10_21_190000_create_roles_table
Migrating: 2016_10_21_190000_create_settings_table
Migrated:  2016_10_21_190000_create_settings_table
Migrating: 2016_11_30_135954_create_permission_table
Migrated:  2016_11_30_135954_create_permission_table
Migrating: 2016_11_30_141208_create_permission_role_table
Migrated:  2016_11_30_141208_create_permission_role_table
Migrating: 2016_12_26_201236_data_types__add__server_side
Migrated:  2016_12_26_201236_data_types__add__server_side
Migrating: 2017_01_13_000000_add_route_to_menu_items_table
Migrated:  2017_01_13_000000_add_route_to_menu_items_table
Migrating: 2017_01_14_005015_create_translations_table
Migrated:  2017_01_14_005015_create_translations_table
Migrating: 2017_01_15_000000_add_permission_group_id_to_permissions_table
Migrated:  2017_01_15_000000_add_permission_group_id_to_permissions_table
Migrating: 2017_01_15_000000_create_permission_groups_table
Migrated:  2017_01_15_000000_create_permission_groups_table
Migrating: 2017_01_15_000000_make_table_name_nullable_in_permissions_table
Migrated:  2017_01_15_000000_make_table_name_nullable_in_permissions_table
Migrating: 2017_03_06_000000_add_controller_to_data_types_table
Migrated:  2017_03_06_000000_add_controller_to_data_types_table
Migrating: 2017_04_11_000000_alter_post_nullable_fields_table
Migrated:  2017_04_11_000000_alter_post_nullable_fields_table
Migrating: 2017_04_21_000000_add_order_to_data_rows_table
Migrated:  2017_04_21_000000_add_order_to_data_rows_table
Migrating: 2017_07_05_210000_add_policyname_to_data_types_table
Migrated:  2017_07_05_210000_add_policyname_to_data_types_table
Migrating: 2017_08_05_000000_add_group_to_settings_table
Migrated:  2017_08_05_000000_add_group_to_settings_table
Attempting to set Voyager User model as parent to App\User
Dumping the autoloaded files and reloading all new files
Adding Voyager routes to routes/web.php
Seeding data into the database
Adding the storage symlink to your public folder
The [public/storage] directory has been linked.
Successfully installed Voyager! Enjoy

Success!
Analysis: The key is this:

'engine' => 'InnoDB ROW_FORMAT=DYNAMIC'

This means that the MySQL storage engine USES InnoDB(instead of MyISAM), dynamic text format. Of course InnoDB is not without its disadvantages, you can further study this.

404 solution for WordPress Chinese tag

One of the common problems in blogs or websites built by WordPress is that the Chinese tag link does not exist. Google Administrator Tool prompt grabs 404 errors. In particular, windows hosts often have Chinese tag link grabbing errors, and Chinese tags cannot be displayed normally. Or Chinese tags can be displayed normally, but 404 errors will appear after clicking the link, which brings great inconvenience to users and is extremely difficult It greatly reduces the friendliness of the website.

How to solve this problem?

First method:

Open WP include/ classes.php (before 3.1) or WP include/class- wp.php (after 3.1 +, hereinafter referred to as the new version) find line 154 (the new version is line 142)

$pathinfo = $_ SERVER['PATH_ INFO';

Replace with:

$pathinfo = mb_ convert_ encoding($_ SERVER['PATH_ INFO'], 'UTF-8', 'GBK');

Find line 159 (the new version is line 147)

$req_ uri = $_ SERVER['REQUEST_ URI'];

Replace with:

$req_ uri = mb_ convert_ encoding($_ SERVER['REQUEST_ URI'], 'UTF-8', 'GBK');

After my personal test, this method can be used, the new version of the statement may be different from the above, but the method is basically the same, the corresponding statement can be replaced.

Second method:

1. Found in the WP includes folder at the root of the site“ rewrite.php ”This document;

2. Back it up“ rewrite.php ”In case of error (we’d better back up the original code before changing the code);

3. Open“ rewrite.php ”We find the following code in the file:

function get_tag_permastruct()
{
   if (isset($this-&gt;tag_structure)) 
  {
    return $this-&gt;tag_structure;
  }
  if (empty($this-&gt;permalink_structure)) 
  { 
    //Change this line
    $this-&gt;tag_structure = '';
    return false;
  }
  if (empty($this-&gt;tag_base))
  $this-&gt;tag_structure = $this-&gt;front . 'tag/';
  else$this-&gt;tag_structure = $this-&gt;tag_base . '/';
  $this-&gt;tag_structure .= '%tag%';
  return $this-&gt;tag_structure;
}

4. Put some of them together

" if (empty($this-&gt;permalink_structure)) { ”

Amend to read

“ if (! empty($this-&gt;permalink_structure)) { ";

Note that there is only a “!” sign in English. After testing, this method is very effective. PS. didn’t find the code in this file directly…

The third method: the final solution to the Chinese tag 404 error is actually ISAPI_ Rewrite pseudo static rules are not well written, which leads to the failure to find the web page file. In fact, just change the rules: the previous tag pseudo static rules

RewriteRule /tag/(.*)$ /index\.php\?tag=$1

Changed rules

RewriteRule /tag/(.*)/$ /index\.php\?tag=$1

I didn’t test this method, so I can’t guarantee its availability. Please back it up before modification!

Third method:

If the above two methods are not feasible, we recommend another method: alias each tag.

Alias is another URL friendly name. It is usually lowercase and can only contain letters, numbers and hyphens.

But for blogs with more than 100 Chinese tags, this method has a huge workload. Use with caution.

This method can link English tags normally, but it works for websites with Chinese tag problems, but it doesn’t work for websites with 404 English tag links.

Syntax error: typeerror: about installing sass in vue3 this.getOptions is not a function

Vue3 successfully installed sass, but the following error occurred during compilation


 ERROR  Failed to compile with 1 error                                                                            PM 1:05:09

 error  in ./src/components/HelloWorld.vue?vue&type=style&index=0&id=469af010&lang=scss

Syntax Error: TypeError: this.getOptions is not a function


 @ ./node_modules/vue-style-loader??ref--8-oneOf-1-0!./node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!./node_modules/vue-loader-v16/dist/stylePostLoader.js!./node_modules/postcss-loader/src??ref--8-oneOf-1-2!./node_modules/[email protected]@sass-loader/dist/cjs.js??ref--8-oneOf-1-3!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader-v16/dist??ref--0-1!./src/components/HelloWorld.vue?vue&type=style&index=0&id=469af010&lang=scss 4:14-464 15:3-20:5 16:22-472
 @ ./src/components/HelloWorld.vue?vue&type=style&index=0&id=469af010&lang=scss
 @ ./src/components/HelloWorld.vue
 @ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader-v16/dist??ref--0-1!./src/App.vue?vue&type=script&lang=js
 @ ./src/App.vue?vue&type=script&lang=js
 @ ./src/App.vue
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://192.168.31.108:8080&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js

Cause of the problem

When the above error occurs, it is found that the version of SASS loader is too high, so the above problem occurs in the compilation

resolvent

We need to unload

cnpm uninstall less-loader

After reinstalling

cnpm i [email protected] --save-dev

Finally, it is compiled successfully

PHP use __Sleep() and __wakeup() method serializes the object

If: Notice: serialize(): “DIY” returned as member variable from__ The solution of sleep() but do error

<?php
header('content-type:text/html;charset=utf-8');
class SportObject{
	protected $type="DIY";
	public function getType(){
		return $this->type;
	}
	public function __sleep(){
		echo 'Use the serialize() function to save the object to a text file, database, etc. <br>';
		return array('type'); //Note here, this should return an array, not return $this on the ebook Of course the paper book is the correct way to write it.
	}
	public function __wakeup(){
		echo 'When this data is needed, use the unserialize() function on the serialized string to convert it back to an object<br>';
	}
}
$mybook=new SportObject();
$a=serialize($mybook);
echo 'Serialized string: '. $a."<br>";
$b=unserialize($a);
echo 'Restored member variable.'.$b->getType();
?>

Output results:

Use the serialize() function to save the object, which can be stored in text files, databases, etc.
the serialized string: O: 11: “sportobject”: 1: {s: 7: “* type”; s: 3: “DIY”;}
when the data is needed, use the unserialize() function to operate the serialized string and convert it back to the object
restored member variable: DIY