Category Archives: PHP

[Solved] PHP connect to MongoDB error: Server does not support sessions

Solution steps:

Find the mongodb configuration file and make the following adjustments

replication:
   oplogSizeMB: <int>
   replSetName: <string>
   enableMajorityReadConcern: <boolean>

After restarting the mongodb service, enter Mongo for execution

rs.initiate()
    Done!

Reference:
https://stackoverflow.com/questions/50255195/how-to-configure-a-mongodb-cluster-which-supports-sessions

ERROR: [pool www] cannot get uid for user ‘localhost.localdomain’ [Solved]

If PHP is newly installed and an error is reported,

The following information:

12月 04 14:56:00 localhost.localdomain php-fpm[54862]: [04-Dec-2021 14:56:00] ERROR: [pool www] cannot get uid for user 'localhost.localdomain'
12月 04 14:56:00 localhost.localdomain php-fpm[54862]: [04-Dec-2021 14:56:00] ERROR: FPM initialization failed

This is because there is a problem with our PHP default parameter settings

It needs to be modified in this script

sudo vi /etc/opt/remi/php72/php-fpm.d/www.conf
We need to find out our username in cat /etc/passwd
cat /etc/group to see the user group we are interested in.
Just change this parameter again.

Finally, restart PHP

sudo systemctl restart php72-php-fpm.service

composer platform_check tp6 thinkphp6 laravel Error (PHP Version)

in fact, the reason should be that the new version of composer adds a thing to check the PHP version. You can solve the problem by turning off this setting

By command

composer config -l -g

Can find

platform_check

The value should be PHP_Only or something

Then turn it off by the command below

composer config -g platform-check false

Then again

composer update

It should be solved

file_get_Contents and fopen return false when requesting HTTPS address: Error content: SSL routes: ssl3_get_server_certificate:certificate verify failed

Recently found file in project_get_Contents, fopen suddenly doesn’t work?

After troubleshooting, it is found that the access can be successful by using HTTP access, and an error will be reported with s and false will be returned;

At first, I thought it was an SSL certificate problem, so I changed the certificate and operated again; False is returned. Detailed error log: SSL routes: ssl3_get_server_certificate:certificate verify failed;

Still not;

file_get_Contents can be solved in the following three ways:

1. Modify the php.ini configuration file

For PHP under windows, just go to php.ini and delete the front of extension=php_openssl.dll; and restart the service. (Note that allow_url_fopen must also be turned on)

For PHP under Linux, you must install the OpenSSL module. After installation, you can access it.

2.stream_context_Create method

$url= 'https://example.com';
$arrContextOptions=array(
      "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );  
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

3. Replace file with curl function_ get_ contents

function getSslPage($url) {
    /*  http://www.manongjc.com/article/1428.html */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

Use method 2 to solve the file_get_contents problem temporarily, but I always feel something is wrong, because it can be accessed before;

Later, fopen reported an error of false;

Keep looking for information……

This is because PHP cannot verify the certificate. Look at phpinfo and find openssl.cafile, which can be found in the php.ini file;

The certificate exists. It should be expired. Replace the certificate.

You can download the latest CA certificate at the following address
https://curl.haxx.se/ca/cacert.pem

Replace the certificate and reload the PHP configuration; Perfect solution!!!

Hyperf captures PHP error levels, such as notice, warning and error, and returns them to the front-end

1. register listener

Config/autoload/listeners.php is as follows

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  [email protected]
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
   \Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler::class
];

Hyperf\exceptionhandler\listener\errorexception handler:: class, error_reporting() Error level listener.

Refer to the listener chapter on the official website https://hyperf.wiki/2.2/#/zh -cn/exception-handler

2. Register global Middleware

Now configure the middleware under config/autoload/middleware.php

<?php

declare(strict_types=1);

namespace App\Middleware\Auth;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;

class FooMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;
    /**
     * @var HttpResponse
     */
    protected $response;

    public function __construct(ContainerInterface $container, HttpResponse $response)
    {
        $this->container = $container;
        $this->response = $response;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        try {
            return $handler->handle($request);
        } catch (\Throwable $throwable) {
            return $this->response->json(
                [
                    'code' => -1,
                    'message' => $throwable->getMessage(),
                    'data' => [
                    ],
                ]
            );
        }

    }
}

After completing the above steps, you can return PHP error prompts such as notice to the front end, and the PHP program will not continue to execute.

[Solved] PHP Compile error: error adding symbols: DSO missing from command line

PHP compilation: error adding symbols: DSO missing from command line

After half a day’s struggle, the solution is as follows:

1. ./configure –disable-shared –enable-static

2. make& make test

3. make install

Tips for compiling and installing PHP 7.4 in CentOS 7

Generally, install EPEL and then install:

yum install -y epel-release
yum install -y oniguruma oniguruma-devel

If not, install/update EPEL manually:

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum install -y oniguruma oniguruma-devel

Or:

CentOS 7 can be installed directly by executing the following commands:

yum -y install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.6-1.el7.remi.x86_64.rpm
yum -y install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.6-1.el7.remi.x86_64.rpm

Or:

yum remove epel-release #If a conflict is reported, remove the conflicting package first
rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y oniguruma oniguruma-devel
yum clean all && yum makecache
yum repolist all #Check epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 is or not disabled

How to compile boost into a separate so file
https://github.com/ulfjack/ryu/issues/111
https://www.boost.org/doc/libs/1_77_0/doc/html/boost_asio/using.html#boost_asio.using.optional_separate_compilation

[Solved] layui Error: “layui.js:4 Uncaught TypeError: Cannot create property ‘LAY_TABLE_INDEX’ on string ……”

Layui reported an error “layui. JS: 4 uncaught typeerror: cannot create property ‘lay_table_index’ on string…”

Read the information from the cloud database with PHP applet, and an error will be reported if it is placed in the layui. Later, I put the returned JSON into the local test, and found that it is because the data in res.data is of string type, and layui requires JSON object type,

So we just need to turn every piece of data in res.data

Above figure:

PHP FPM and Nginx start normally, page 502 Error [How to Solve]

PHP FPM and nginx are started normally, and page 502 is solved

Background: for the newly installed system, after manually installing PHP, nginx and PHP FPM modules, deploy a PHP project locally. When running, it is found that only nginx version page displays 502

The troubleshooting process is as follows, which is briefly recorded:

Check the startup of PHP FPM and nginx:

# Check if php-fpm and nginx are up
ps -ef|grep php-fpm
ps -ef|grep nginx

The result is: both are up and running

View the nginx log information at the time of the request:

# First, we looked at the location of the nginx runtime logs.
find/-name nginx.conf # Find the nginx configuration file and see where the error.log is stored
# View the error.log information as follows.
*58 connect() failed (111: Connection refused) while connecting to upstream

Troubleshooting results:

PHP FPM and nginx are all started normally, so it is not the reason why PHP FPM is not started. Continue to check and find that:

There are two ways to link nginx and php.
1. fastcgi_pass 127.0.0.1:9000;
2. fastcgi_pass unix:/run/php/php7.0-fpm.sock;


You have to go to the php fpm configuration file to see how this works
/etc/php/7.0/fpm/pool.d/www.conf

If Listen is the port, write 127.0.0.1:9000;
If it's a path, the nginx configuration file should learn the path as well.unix:/run/php/php7.0-fpm.sock;

Perfect solution: check the www.conf configuration file, and the path is after listen, so modify the nginx configuration file of the project:

### # fastcgi_pass   127.0.0.1:9000;  # Comment out this port and replace it with the following path format
fastcgi_pass unix:/run/php/php7.3-fpm.sock; # fastcgi_pass to the address of phm.sock on the local machine

[Thinkphp6] Connect to SQL Server and use subquery to report error: when subquery is not introduced with exists, only one expression can be specified in the selection list

Connect to SQL Server in thinkphp6 and report errors using subquery

sentence

Print SQL statements

Mysql

SELECT `NickName`,`Gender`,`Mobile`,`RealName`,`Birthday` FROM `SiteCustomerSupplement` `s` 

INNER JOIN `WxUser` `u` ON `u`.`OpenId`=`s`.`OpenId` 

WHERE `s`.`OpenId` IN (SELECT DISTINCT `OpenId` FROM `WxPackagesOrder` WHERE `OrderStatus` >= 40 AND `RegionId` = 72) AND `Birthday` BETWEEN '2021-10-26' AND '2021-11-02'

SQL Server

SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER ( ORDER BY rand()) AS ROW_NUMBER FROM (SELECT [NickName],[Gender],[Mobile],[RealName],[Birthday] FROM [SiteCustomerSupplement] [s] 

INNER JOIN [WxUser] [u] ON [u].[OpenId]=[s].[OpenId]

WHERE [s].[OpenId] IN (SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER ( ORDER BY rand()) AS ROW_NUMBER FROM (SELECT DISTINCT [OpenId] FROM [WxPackagesOrder] WHERE [OrderStatus] >= 40 AND [RegionId] = 72) AS thinkphp) AS T1) AND [Birthday] BETWEEN '2021-10-26' AND '2021-11-02') AS thinkphp) AS T1

Obviously, it is much longer than the MySQL statement, mainly this paragraph

SELECT thinkphp.*, ROW_NUMBER() OVER ( ORDER BY rand()) AS ROW_NUMBER FROM

So when the problem comes, how to solve it?Just replace it with a native statement. Don’t use TP internal encapsulation.