Tag Archives: PHP

慎用json_decode的assoc参数

我们在工作中遇到的情况:

  1. vue 3前端界面 + php 后端 API
  2. vue 3采用typescript
  3. API数据采用json格式

出现的问题

当post api时提交的json数据中包含{}这种空JSON对象,比如style:{}, 后端php在处理过程中会变成[]并返回给前端,导致前端后续的功能出现bug,如这时再往style中设置值就不能正常工作了:style[‘foo’] = ‘bar’, 这时设置是不成功

问题的原因

原因1: typescript的强类型,如果是javascript,这点其实并不影响,JS数组可以直接设置值:var style = []; style[“foo”] = “bar”;这个操作是成功的。
原因2: 后端php在处理的过程中都是按数组格式处理:json_decode($data, true),无形中就把{}变成了php的空数组,然后返回给前端时json_encode就变成[]。

解决办法

修改后端json_decode总是按对象格式处理

Mac 11.0以上版本不能编译PHP的问题

在Mac 11.x版本编译php会提示php.h不存在,phpize也看不出php版本内容:

grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:        
Zend Module Api No:     
Zend Extension Api No: 

这是因为新版本的mac 不在支持php了,后续可能会移除php, 通过php -v 可以看出

但实际上还没有完全移除PHP,可以查看/Library/Developer/CommandLineTools/SDKs目录下你系统版本对应的目录中usr/include/php里面的内容是否还存在,如果还存在说明php devl的头文件还在哪里,只是usr/bin/phpize找不到他们。

通过phpize的输出也不难看出,他是去usr/include/php里面找对应的头文件,那么我们建立软链接到对应的/Library/Developer/CommandLineTools/SDKs下面的php目录即可。但由于/usr/include目录是Read only的,不允许创建软链接,解决办法就是:

1. 把/usr/bin/phpize 和/usr/bin/php-config两个文件复制到/usr/local/bin,并修改如下两处内容,加上local

php-config中extension_dir也重新指定一个目录,并且把/etc/php.ini extension_dir中也同步调整:

2. 建立软连接 ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include/php /usr/local/include/php

然后使用phpize时指定全路径 /usr/local/bin/phpize

configure时指定我们修改后的php-config:/usr/local/bin/phpize ./configrue –with-php-config=/usr/local/bin/php-config

make, make install 最后把编译的so文件会拷贝到指定的extension_dir目录中


该问题解决后,可通过pear的方式来安装其他pecl 扩展了

1. 下载pear: https://pear.php.net/go-pear.phar
2. 安装: php go-pear.phar,在出现的页面中分别把1,4两步设置为如下图所示,这其实是让pecl命令在bin目录中
3. 然后就可以通过pecl按照扩展了,比如pecl install zip,会下载源码并编译,同上面的phpize & configure & make & make install

会员卡销售管理系统交付

  1. 该系统的目的是用于记录会员卡的销售情况,用户角色分为4种,每种又分几个等级:
    1. 会员,分为金卡、铂金卡、黑卡
    2. 教师,分初级、中奖、高级、副校长
    3. 行政,初级、中级、高级
    4. 校长
  2. 每种角色下面的会员等级可以自由配置
  3. 分前端和后端,后端只能由校长和行政登录使用,其他等级的用户不能登录后台;前端只给会员和教师登录使用
  4. 每个等级的区别就是消费额度不同,达到对应的消费额度,自动提升会员的等级
  5. 销售额度包含自己的销售额和自己的团队成员的消费额
  6. 成员的意思就是自己推荐来的人就是自己的团队成员,团队只有一级,比如A推荐了B,B推荐了C,那么A的团队里只有B,B的团队里只有C
  7. 消费由后台进行登记,后台行政人员登记用户的消费情况:电话,姓名、推荐人、卡种、金额、订单号;行政人员登记后,需要由校长进行审核,审核通过则登记有效;校长登记的则不用审核
  8. 后台会配置每种会员卡的达标销售额度和提成情况,提成分两种提成配置,一是固定金额,比如销售一张金卡提成100块,一是配置比例,比如销售一张金卡提成销售额度的1%
  9. 消费登记有效后就需要记录推荐人(组长)的成交金额和提成情况
  10. 提成可以提现,提现由校长进行操作,登记提现的信息,提现成功后减少对应人员的能提成金额和记录提现信息

车辆管理系统交付

  1. 管理后台、公司、用户三级用户
  2. 事故违章   
    •  车辆违章登记   
  3.  数据看板   
    •  今日花费   
    •  今日违章   
    •  使用中的车辆   
    •  车辆总数   
  4.  消息推送   
    •  广告推送   
    •  违章、年检、保养的消息推送提醒   
  5.  用户管理   
    •  所属公司   
    •  所属部门   
  6.  用车管理   
    •  用车登记   
    •  还车登记   
  7.  电子围栏   
  8.  系统配置   
    •  GPS设备管理   
    •  消息提醒设置   
  9.  组织部门管理   
  10.  费用管理   
  11.  车位管理   
    •  车位登记   
  12.  车辆保险年检   
  13.  车辆管理   
    •  车辆登记   
  14.  车辆轨迹管理   
  15.  驾驶员行为分析   

监狱生产管理系统双控升级

  • 15607 点检看板增加管控层级显示
  • 15584 点检时数据库操作会报错误,导致程序崩溃
  • 15583 P2双控看板统计数字不对;风控看板把对风险点巡检次数的统计修改为频率次数的统计
  • 15581 点检台账列表需要显示出风险等级及其数量,点检详情也需要显示出数量
  • 15580 点检台账详情、灭火器点检台账中,如果没有隐患,把责任人显示成点检者的名字
  • 15578 点检产生的记录应该和点检清单一致,对应在周期内已经点检的(不管是谁点检的),在同一个周期内再次点检时,不应该在记录中
  • 15566 分监区用户登录后,点检台账也需要能按人进行查询,默认查询自己的,但也可以查询同分监区下其他人的

全新安装LAMP环境

安装apache

1.使用yum安装apache:

yum install -y httpd

2.下载完成后启动:

systemctl start httpd.service
systemctl status httpd.service(查看运行状态)

3.设置apache开机自启动

systemclt enable httpd.service

至此apache安装完毕

安装mod_ssl

yum -y install mod_ssl

安装mariadb

1.首先设置数据源

curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

2.更新缓存

yum clean all  
yum makecache  
yum repolist

3.显示可安装的版本

#这个可以看版本号
yum search mariadb –showduplicates 

yum search mariadb

4.安装

sudo yum install MariaDB-server galera-4 MariaDB-client MariaDB-shared MariaDB-backup MariaDB-common

5.启动服务

systemctl enable mariadb –now

6.配置数据库

mysql_secure_installation

首先是设置密码,会提示先输入密码

Enter current password for root (enter for none):<–初次运行直接回车

设置密码

Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码

其他配置

Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车

Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车,

Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车

Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

初始化MariaDB完成,接下来测试登录

mysql -uroot -ppassword

设置数据库允许远程连接:

mysql -uroot -p(进入数据库)

查看MySQL库中的user表(user表中存着链接信息)

select host,user from user;

使用更新语句是root用户可以在任意IP的电脑上登录

update user set host=’%’,user=’root’ limit 1;

使修改生效

flush privileges;

退出Mariadb后并重启 mariadb服务

systemctl restart mariadb

至此,数据库安装完毕

安装php

1.首先安装 EPEL 源:

yum install epel-release

2.安装 REMI 源:

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

3.安装 Yum 源管理工具:

yum install yum-utils

4.安装 PHP7.3:

yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll

5.启动php:

systemctl enable php73-php-fpm(开机自启动)
systemctl start php73-php-fpm(启动)

配置apache虚拟机

在/etc/httpd/conf.d文件夹新建一个conf配置文件,再把以下内容拷贝到里面

例如:

  
    DocumentRoot “/var/www/html/web/app/public_html”   #项目根目录
    ServerName lottery.yidianhulian.com  #绑定的域名
       
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
    DirectoryIndex index.php index.html error/index.html
 
  ErrorDocument 400 /error/400.html
  ErrorDocument 403 /error/403.html
  ErrorDocument 404 /error/404.html
  ErrorDocument 500 /error/500.html
  ErrorDocument 501 /error/501.html
  ErrorDocument 502 /error/502.html
  ErrorDocument 503 /error/503.html
  ErrorDocument 504 /error/504.html
  ErrorDocument 505 /error/505.html
  ErrorDocument 506 /error/506.html
  ErrorDocument 507 /error/507.html
  ErrorDocument 510 /error/510.html

配置https

1.使用域名可以申请免费的ssl证书,选择apache版本,证书一共有三个文件,例如:

2.在服务器中新建一个文件夹,把这三个文件拷贝到里面

3.配置https其实就是配置/etc/httpd/conf.d/ssl.conf文件,使用yum下载mod_ssl后会自动生成这个文件。对文件进行配置,例如:

#
# When we also provide SSL we have to listen to the 
# the HTTPS port in addition.
#
Listen 443 https

##
##  SSL Global Context
##
##  All SSL configuration in this context applies both to
##  the main server and all SSL-enabled virtual hosts.
##

#   Pass Phrase Dialog:
#   Configure the pass phrase gathering process.
#   The filtering dialog program (`builtin’ is a internal
#   terminal dialog) has to provide the pass phrase on stdout.
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog

#   Inter-Process Session Cache:
#   Configure the SSL Session Cache: First the mechanism 
#   to use and second the expiring timeout (in seconds).
SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300

#   Pseudo Random Number Generator (PRNG):
#   Configure one or more sources to seed the PRNG of the 
#   SSL library. The seed data should be of good random quality.
#   WARNING! On some platforms /dev/random blocks if not enough entropy
#   is available. This means you then cannot use the /dev/random device
#   because it would lead to very long connection times (as long as
#   it requires to make more entropy available). But usually those
#   platforms additionally provide a /dev/urandom device which doesn’t
#   block. So, if available, use this one instead. Read the mod_ssl User
#   Manual for more details.
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
#SSLRandomSeed startup file:/dev/random  512
#SSLRandomSeed connect file:/dev/random  512
#SSLRandomSeed connect file:/dev/urandom 512

#
# Use “SSLCryptoDevice” to enable any supported hardware
# accelerators. Use “openssl engine -v” to list supported
# engine names.  NOTE: If you enable an accelerator and the
# server does not start, consult the error logs and ensure
# your accelerator is functioning properly. 
#
SSLCryptoDevice builtin
#SSLCryptoDevice ubsec

##
## SSL Virtual Host Context
##

      #找到这个标签

# General setup for the virtual host, inherited from global configuration
ServerName lottery.yidianhulian.com   #配置域名
DocumentRoot /var/www/html/web/app/public_html  #配置项目根目录

  #添加这个标签允许访问该项目
 
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
    DirectoryIndex index.php index.html error/index.html
 

# Use separate log files for the SSL virtual host; note that LogLevel
# is not inherited from httpd.conf.
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warn

#   SSL Engine Switch:
#   Enable/Disable SSL for this virtual host.
SSLEngine on

#   SSL Protocol support:
# List the enable protocol levels with which clients will be able to
# connect.  Disable SSLv2 access by default:
SSLProtocol all -SSLv2 -SSLv3  # 添加SSL协议支持协议,去掉不安全的协议。

#   SSL Cipher Suite:
#   List the ciphers that the client is permitted to negotiate.
#   See the mod_ssl documentation for a complete list.
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM   # 修改加密套件。

#   Speed-optimized SSL Cipher configuration:
#   If speed is your main concern (on busy HTTPS servers e.g.),
#   you might want to force clients to specific, performance
#   optimized ciphers. In this case, prepend those ciphers
#   to the SSLCipherSuite list, and enable SSLHonorCipherOrder.
#   Caveat: by giving precedence to RC4-SHA and AES128-SHA
#   (as in the example below), most connections will no longer
#   have perfect forward secrecy – if the server’s key is
#   compromised, captures of past or future traffic must be
#   considered compromised, too.
#SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5
SSLHonorCipherOrder on 

#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/httpd/cert/4520877_lottery.yidianhulian.com_public.crt  #证书所在目录。要一一对应,注意看文件名

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you’ve both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/httpd/cert/4520877_lottery.yidianhulian.com.key  #证书所在目录。要一一对应,注意看文件名

#   Server Certificate Chain:
#   Point SSLCertificateChainFile at a file containing the
#   concatenation of PEM encoded CA certificates which form the
#   certificate chain for the server certificate. Alternatively
#   the referenced file can be the same as SSLCertificateFile
#   when the CA certificates are directly appended to the server
#   certificate for convinience.
SSLCertificateChainFile /etc/httpd/cert/4520877_lottery.yidianhulian.com_chain.crt  #证书所在目录。要一一对应,注意看文件名

#   Certificate Authority (CA):
#   Set the CA certificate verification path where to find CA
#   certificates for client authentication or alternatively one
#   huge file containing all of them (file must be PEM encoded)
#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt

#   Client Authentication (Type):
#   Client certificate verification type and depth.  Types are
#   none, optional, require and optional_no_ca.  Depth is a
#   number which specifies how deeply to verify the certificate
#   issuer chain before deciding the certificate is not valid.
#SSLVerifyClient require
#SSLVerifyDepth  10

#   Access Control:
#   With SSLRequire you can do per-directory access control based
#   on arbitrary complex boolean expressions containing server
#   variable checks and other lookup directives.  The syntax is a
#   mixture between C and Perl.  See the mod_ssl documentation
#   for more details.  
#
#SSLRequire (    %{SSL_CIPHER} !~ m/^(EXP|NULL)/ \
#            and %{SSL_CLIENT_S_DN_O} eq “Snake Oil, Ltd.” \
#            and %{SSL_CLIENT_S_DN_OU} in {“Staff”, “CA”, “Dev”} \
#            and %{TIME_WDAY} >= 1 and %{TIME_WDAY} <= 5 \
#            and %{TIME_HOUR} >= 8 and %{TIME_HOUR} <= 20       ) \
#           or %{REMOTE_ADDR} =~ m/^192\.76\.162\.[0-9]+$/
#

#   SSL Engine Options:
#   Set various options for the SSL engine.
#   o FakeBasicAuth:
#     Translate the client X.509 into a Basic Authorisation.  This means that
#     the standard Auth/DBMAuth methods can be used for access control.  The
#     user name is the `one line’ version of the client’s X.509 certificate.
#     Note that no password is obtained from the user. Every entry in the user
#     file needs this password: `xxj31ZMTZzkVA’.
#   o ExportCertData:
#     This exports two additional environment variables: SSL_CLIENT_CERT and
#     SSL_SERVER_CERT. These contain the PEM-encoded certificates of the
#     server (always existing) and the client (only existing when client
#     authentication is used). This can be used to import the certificates
#     into CGI scripts.
#   o StdEnvVars:
#     This exports the standard SSL/TLS related `SSL_*’ environment variables.
#     Per default this exportation is switched off for performance reasons,
#     because the extraction step is an expensive operation and is usually
#     useless for serving static content. So one usually enables the
#     exportation for CGI and SSI requests only.
#   o StrictRequire:
#     This denies access when “SSLRequireSSL” or “SSLRequire” applied even
#     under a “Satisfy any” situation, i.e. when it applies access is denied
#     and no other module can change it.
#   o OptRenegotiate:
#     This enables optimized SSL connection renegotiation handling when SSL
#     directives are used in per-directory context. 
#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire

    SSLOptions +StdEnvVars


    SSLOptions +StdEnvVars

#   SSL Protocol Adjustments:
#   The safe and default but still SSL/TLS standard compliant shutdown
#   approach is that mod_ssl sends the close notify alert but doesn’t wait for
#   the close notify alert from client. When you need a different shutdown
#   approach you can use one of the following variables:
#   o ssl-unclean-shutdown:
#     This forces an unclean shutdown when the connection is closed, i.e. no
#     SSL close notify alert is send or allowed to received.  This violates
#     the SSL/TLS standard but is needed for some brain-dead browsers. Use
#     this when you receive I/O errors because of the standard approach where
#     mod_ssl sends the close notify alert.
#   o ssl-accurate-shutdown:
#     This forces an accurate shutdown when the connection is closed, i.e. a
#     SSL close notify alert is send and mod_ssl waits for the close notify
#     alert of the client. This is 100% SSL/TLS standard compliant, but in
#     practice often causes hanging connections with brain-dead browsers. Use
#     this only for browsers where you know that their SSL implementation
#     works correctly. 
#   Notice: Most problems of broken clients are also related to the HTTP
#   keep-alive facility, so you usually additionally want to disable
#   keep-alive for those clients, too. Use variable “nokeepalive” for this.
#   Similarly, one has to force some clients to use HTTP/1.0 to workaround
#   their broken HTTP/1.1 implementation. Use variables “downgrade-1.0” and
#   “force-response-1.0” for this.
BrowserMatch “MSIE [2-5]” \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0

#   Per-Server Logging:
#   The home of a custom SSL log file. Use this when you want a
#   compact non-error SSL logfile on a virtual host basis.
CustomLog logs/ssl_request_log \
          “%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \”%r\” %b”

yangzie代码结构

yangzie的目录结构

yangzie 目录是框架核心文件

scripts是构建脚本目录

tests是单元测试文件目录

tmp是其他一些临时目录

app是功能代码目录,我们编写的功能代码都在其中

APP目录详细说明

  • __aros_acos__.php 该文件是ACL控制配置文件,这将在ACL控制中详细说明
  • __config__.php是系统的配置文件,包含如数据库配置资源打包绑定,文件包含登录
  • hooks是系统级别的hook注册文件放置目录
  • modules是功能模块目录,所有的业务功能代码都会以modules的方式放置在这里面
    • controllers是所有控制器类文件
    • models是所有的model文件,model是与数据库的表对应的类,这将在Model-数据处理中说明
    • views是控制器的方法对应的输出视图,这将在视图系统中进行介绍
    • hooks是该模块下的hooks文件
    • __module__.php是模块的配置文件
  • public_html是系统访问的入口目录,里面的目录可以自由组织存放
  • public_html/index.php就是入口文件
  • public_html/module-assets是modules对应的资源文件
  • vendor是其他第三方库,layout,views等系统公共部分部分的放置路径
  • vendor/layout存放的是系统的布局文件
  • vendor/views/存放的是公共视图

目录大概就了解这些,具体目录里面的含义我们会在后面继续详解。

接下来,开始写代码吧:《hello yangzie