標簽歸檔: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.www.juyunmall.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.www.juyunmall.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.www.juyunmall.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.www.juyunmall.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.www.juyunmall.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