七、PHP JSON 格式php
json_encode ( mixed $value
[, int $options
= 0 [, int $depth
= 512 ]] )html
返回字符串,包含了 value
值 JSON 形式的表示。算法
編碼受傳入的 options
參數影響,此外浮點值的編碼依賴於 serialize_precision。數據庫
參數json
value
數組
待編碼的 value
,除了resource 類型以外,能夠爲任何數據類型。安全
全部字符串數據的編碼必須是 UTF-8。網絡
options
dom
由如下常量組成的二進制掩碼: JSON_HEX_QUOT
, JSON_HEX_TAG
, JSON_HEX_AMP
, JSON_HEX_APOS
, JSON_NUMERIC_CHECK
,JSON_PRETTY_PRINT
, JSON_UNESCAPED_SLASHES
, JSON_FORCE_OBJECT
, JSON_PRESERVE_ZERO_FRACTION
, JSON_UNESCAPED_UNICODE
,JSON_PARTIAL_OUTPUT_ON_ERROR
。 關於 JSON 常量詳情參考JSON 常量。ide
如下常量表示了 json_last_error() 所返回的錯誤類型。 JSON_ERROR_NONE (integer) 沒有錯誤發生。自 PHP 5.3.0 起生效。 JSON_ERROR_DEPTH (integer) 到達了最大堆棧深度。自 PHP 5.3.0 起生效。 JSON_ERROR_STATE_MISMATCH (integer) 出現了下溢(underflow)或者模式不匹配。自 PHP 5.3.0 起生效。 JSON_ERROR_CTRL_CHAR (integer) 控制字符錯誤,多是編碼不對。自 PHP 5.3.0 起生效。 JSON_ERROR_SYNTAX (integer) 語法錯誤。 自 PHP 5.3.0 起生效。 JSON_ERROR_UTF8 (integer) 異常的 UTF-8 字符,也許是由於不正確的編碼。 自 PHP 5.3.3 起生效。 JSON_ERROR_RECURSION (integer) The object or array passed to json_encode() include recursive references and cannot be encoded. If theJSON_PARTIAL_OUTPUT_ON_ERROR option was given, NULL will be encoded in the place of the recursive reference. Available since PHP 5.5.0. JSON_ERROR_INF_OR_NAN (integer) The value passed to json_encode() includes either NAN or INF. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, 0 will be encoded in the place of these special numbers. Available since PHP 5.5.0. JSON_ERROR_UNSUPPORTED_TYPE (integer) A value of an unsupported type was given to json_encode(), such as a resource. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, NULL will be encoded in the place of the unsupported value. Available since PHP 5.5.0. JSON_ERROR_INVALID_PROPERTY_NAME (integer) A key starting with \u0000 character was in the string passed to json_decode() when decoding a JSON object into a PHP object. Available since PHP 7.0.0. JSON_ERROR_UTF16 (integer) Single unpaired UTF-16 surrogate in unicode escape contained in the JSON string passed to json_encode(). Available since PHP 7.0.0. 下面的常量能夠和 json_decode() 的 form 選項結合使用。 JSON_BIGINT_AS_STRING (integer) 將大數字編碼成原始字符原來的值。 自 PHP 5.4.0 起生效。 JSON_OBJECT_AS_ARRAY (integer) Decodes JSON objects as PHP array. This option can be added automatically by calling json_decode() with the second parameter equal to TRUE. Available since PHP 5.4.0. 下面的常量能夠和 json_encode() 的 form 選項結合使用。 JSON_HEX_TAG (integer) 全部的 < 和 > 轉換成 \u003C 和 \u003E。 自 PHP 5.3.0 起生效。 JSON_HEX_AMP (integer) 全部的 & 轉換成 \u0026。 自 PHP 5.3.0 起生效。 JSON_HEX_APOS (integer) 全部的 ' 轉換成 \u0027。 自 PHP 5.3.0 起生效。 JSON_HEX_QUOT (integer) 全部的 " 轉換成 \u0022。 自 PHP 5.3.0 起生效。 JSON_FORCE_OBJECT (integer) 使一個非關聯數組輸出一個類(Object)而非數組。 在數組爲空而接受者須要一個類(Object)的時候尤爲有用。 自 PHP 5.3.0 起生效。 JSON_NUMERIC_CHECK (integer) 將全部數字字符串編碼成數字(numbers)。 自 PHP 5.3.3 起生效。 JSON_PRETTY_PRINT (integer) 用空白字符格式化返回的數據。 自 PHP 5.4.0 起生效。 JSON_UNESCAPED_SLASHES (integer) 不要編碼 /。 自 PHP 5.4.0 起生效。 JSON_UNESCAPED_UNICODE (integer) 以字面編碼多字節 Unicode 字符(默認是編碼成 \uXXXX)。 自 PHP 5.4.0 起生效。 JSON_PARTIAL_OUTPUT_ON_ERROR (integer) Substitute some unencodable values instead of failing. Available since PHP 5.5.0. JSON_PRESERVE_ZERO_FRACTION (integer) Ensures that float values are always encoded as a float value. Available since PHP 5.6.6. JSON_UNESCAPED_LINE_TERMINATORS (integer) The line terminators are kept unescaped when JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was before PHP 7.1 without this constant. Available since PHP 7.1.0.
depth
設置最大深度。 必須大於0。
返回值
成功則返回 JSON 編碼的 string 或者在失敗時返回 FALSE
。
json_decode ( string $json
[, bool $assoc
= false [, int $depth
= 512 [, int $options
= 0 ]]] )
接受一個 JSON 編碼的字符串而且把它轉換爲 PHP 變量。
參數
json
待解碼的 json
string 格式的字符串。
這個函數僅能處理 UTF-8 編碼的數據。
assoc
當該參數爲 TRUE
時,將返回 array 而非 object 。
depth
指定遞歸深度。
options
JSON解碼的掩碼選項。 如今有兩個支持的選項。 第一個是JSON_BIGINT_AS_STRING
, 用於將大整數轉爲字符串而非默認的float類型。第二個是 JSON_OBJECT_AS_ARRAY
, 與將assoc
設置爲 TRUE
有相同的效果。
#EXAMPLE
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; $json_de_obj = json_decode($json); $json_de_array = json_decode($json, true); var_dump($json_de_obj); var_dump($json_de_array); $json_en_obj = json_encode($json_de_obj); $json_en_array = json_encode($json_de_array); var_dump($json_en_obj ); var_dump($json_en_array ); ?> 輸出結果: object(stdClass)[10] public 'a' => int 1 public 'b' => int 2 public 'c' => int 3 public 'd' => int 4 public 'e' => int 5 array (size=5) 'a' => int 1 'b' => int 2 'c' => int 3 'd' => int 4 'e' => int 5 string '{"a":1,"b":2,"c":3,"d":4,"e":5}' (length=31) string '{"a":1,"b":2,"c":3,"d":4,"e":5}' (length=31)
八、PHP 加密解密函數
(mcrypt7.1開始擴展被廢棄,OpenSSL擴展代替)
加密函數:
單向加密函數:
md5(string,raw);
參數 | 描述 |
---|---|
string | 必需。規定要計算的字符串。 |
raw | 可選。規定十六進制或二進制輸出格式:
|
crypt(string, salt)
返回使用 DES、Blowfish 或 MD5 算法加密的字符串。
在不一樣的操做系統上,該函數的行爲不一樣,某些操做系統支持一種以上的算法類型。在安裝時,PHP 會檢查什麼算法可用以及使用什麼算法。
具體的算法依賴於 salt 參數的格式和長度。經過增長由使用特定加密方法的特定字符串所生成的字符串數量,salt 可使加密更安全。
這裏有一些和 crypt() 函數一塊兒使用的常量。這些常量值是在安裝時由 PHP 設置的。
[CRYPT_SALT_LENGTH] | 默認的加密長度。使用標準的 DES 加密,長度爲 2 |
[CRYPT_STD_DES] | 基於標準 DES 算法的散列使用 "./0-9A-Za-z" 字符中的兩個字符做爲鹽值。在鹽值中使用非法的字符將致使 crypt() 失敗。 |
[CRYPT_EXT_DES] | 擴展的基於 DES 算法的散列。其鹽值爲 9 個字符的字符串,由 1 個下劃線後面跟着 4 字節循環次數和 4 字節鹽值組成。它們被編碼成可打印字符,每一個字符 6 位,有效位最少的優先。0 到 63 被編碼爲 "./0-9A-Za-z"。在鹽值中使用非法的字符將致使 crypt() 失敗。 |
[CRYPT_MD5] | MD5 散列使用一個以 $1$ 開始的 12 字符的字符串鹽值。 |
[CRYPT_BLOWFISH] | Blowfish 算法使用以下鹽值:「$2a$」,一個兩位 cost 參數,「$」 以及 64 位由 「./0-9A-Za-z」 中的字符組合而成的字符串。在鹽值中使用此範圍以外的字符將致使 crypt() 返回一個空字符串。兩位 cost 參數是循環次數以 2 爲底的對數,它的範圍是 04-31,超出這個範圍將致使 crypt() 失敗。 |
CRYPT_SHA256 | SHA-256 算法使用一個以 $5$ 開頭的 16 字符字符串鹽值進行散列。若是鹽值字符串以 「rounds=<N>$」 開頭,N 的數字值將被用來指定散列循環的執行次數,這點很像 Blowfish 算法的 cost 參數。默認的循環次數是 5000,最小是 1000,最大是 999,999,999。超出這個範圍的 N 將會被轉換爲最接近的值。 |
CRYPT_SHA512 | SHA-512 算法使用一個以 $6$ 開頭的 16 字符字符串鹽值進行散列。若是鹽值字符串以 「rounds=<N>$」 開頭,N 的數字值將被用來指定散列循環的執行次數,這點很像 Blowfish 算法的 cost 參數。默認的循環次數是 5000,最小是 1000,最大是 999,999,999。超出這個範圍的 N 將會被轉換爲最接近的值。 |
在該函數支持多種算法的系統上,若是支持上述常量則設置爲 "1",不然設置爲 "0"。
password_hash(string $password
, int $algo
[, array $options
]) 是crypt()的一個簡單封裝,而且徹底與現有的密碼哈希兼容.推薦使用該函數來進行單向加密。
鹽值(salt)選項從 PHP 7.0.0 開始被廢棄(deprecated)了。 如今最好選擇簡單的使用默認產生的鹽值。
當前支持的算法:
PASSWORD_DEFAULT
- 使用 bcrypt 算法 (PHP 5.5.0 默認)。 注意,該常量會隨着 PHP 加入更新更高強度的算法而改變。 因此,使用此常量生成結果的長度將在將來有變化。 所以,數據庫裏儲存結果的列可超過60個字符(最好是255個字符)。PASSWORD_BCRYPT
- 使用 CRYPT_BLOWFISH
算法建立散列。 這會產生兼容使用 "$2y$" 的 crypt()。 結果將會是 60 個字符的字符串, 或者在失敗時返回 FALSE
。PASSWORD_ARGON2I
- 使用 Argon2 散列算法建立散列。PASSWORD_BCRYPT
支持的選項:
salt(string) - 手動提供散列密碼的鹽值(salt)。這將避免自動生成鹽值(salt)。
省略此值後,password_hash() 會爲每一個密碼散列自動生成隨機的鹽值。這種操做是有意的模式。
鹽值(salt)選項從 PHP 7.0.0 開始被廢棄(deprecated)了。 如今最好選擇簡單的使用默認產生的鹽值。
cost (integer) - 表明算法使用的 cost。crypt() 頁面上有 cost 值的例子。
省略時,默認值是 10。 這個 cost 是個不錯的底線,但也許能夠根據本身硬件的狀況,加大這個值。
PASSWORD_ARGON2I
支持的選項:
memory_cost (integer) - 計算 Argon2 散列時的最大內存(單位:字節 byte)。默認值: PASSWORD_ARGON2_DEFAULT_MEMORY_COST
。
time_cost (integer) - 計算 Argon2 散列時最多的時間。默認值: PASSWORD_ARGON2_DEFAULT_TIME_COST
。
threads (integer) - 計算 Argon2 散列時最多的線程數。默認值: PASSWORD_ARGON2_DEFAULT_THREADS
。
可解密的加密函數:
mcrypt_encrypt(string $cipher
, string $key
, string $data
, string $mode
[, string $iv
] )
參數
cipher
MCRYPT_ciphername
常量中的一個,或者是字符串值的算法名稱。
key
加密密鑰。 若是密鑰長度不是該算法所可以支持的有效長度,則函數將會發出警告並返回 FALSE
data
使用給定的 cipher
和 mode
加密的數據。 若是數據長度不是 n*分組大小,則在其後使用 '\0' 補齊。
返回的密文長度可能比 data
更大。
mode
MCRYPT_MODE_modename
常量中的一個,或如下字符串中的一個:"ecb","cbc","cfb","ofb","nofb" 和 "stream"。
iv
Used for the initialization in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If the provided IV size is not supported by the chaining mode or no IV was provided, but the chaining mode requires one, the function will emit a warning and return FALSE
.
PHP5.6開始再也不接受無效長度的 key
and iv
參數。 若是參數長度無效,則 mcrypt_decrypt() 函數會產生警告而且返回 FALSE
。 以前版本中,對於長度不足的密鑰和初始向量會在其後補齊 '\0' 使其達到有效長度。
openssl_encrypt(string $data
, string $method
, string $key
[, int $options
= 0 [, string $iv
= "" [, string&$tag
= NULL [, string $aad
= "" [, int $tag_length
= 16 ]]]]] )
以指定的方式和 key 加密數據,返回原始或 base64 編碼後的字符串。
參數
data
待加密的明文信息數據。
method
密碼學方式。openssl_get_cipher_methods() 可獲取有效密碼方式列表。
key
key。
options
options
是如下標記的按位或: OPENSSL_RAW_DATA
、 OPENSSL_ZERO_PADDING
。
iv
非 NULL 的初始化向量。
tag
使用 AEAD 密碼模式(GCM 或 CCM)時傳引用的驗證標籤。
aad
附加的驗證數據。
tag_length
驗證 tag
的長度。GCM 模式時,它的範圍是 4 到 16。
返回值
成功時返回加密後的字符串, 或者在失敗時返回 FALSE
。
錯誤/異常
method
傳入未知算法時,產生 E_WARNING
級別的錯誤。
iv
傳入空字符串時產生 E_WARNING
級別的錯誤。
解密函數:
mcrypt_decrypt()
openssl_decrypt()
編碼轉換
base64_encode(string) base64編碼
base64_decode(string) base64解碼
urlencode(string) URL編碼
urldecode(string) URL解碼
convert_uuencode() 使用 uuencode 算法對字符串進行編碼。把全部字符串(包括二進制)編碼爲可打印的字符,以確保其數據庫存儲及網絡傳輸數據的安全。【uuencoded 數據比原數據大約增大 35%。】
convert_uudecode() 對 uuencode 編碼的字符串進行解碼。
htmlentities() 把字符轉換爲 HTML 實體。
html_entity_decode() 把 HTML 實體轉換爲字符。
htmlspecialchars() 把預約義的字符轉換爲 HTML 實體。
htmlspecialchars_decode() 把預約義的 HTML 實體轉換爲字符。
預約義的字符是:
#EXAMPLE1
<?php
# 密鑰應該是隨機的二進制數據, # 開始使用 scrypt, bcrypt 或 PBKDF2 將一個字符串轉換成一個密鑰 # 密鑰是 16 進制字符串格式 $str = "須要加密的字符串"; $key = pack('H*', cbc04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"); # 顯示 AES-128, 192, 256 對應的密鑰長度: # 16,24,32 字節。 $cipher = MCRYPT_RIJNDAEL_128; $mode = MCRYPT_MODE_CBC; $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$mode),MCRYPT_RAND); $str_encrypt = mcrypt_encrypt($cipher,$key,$str,$mode,$iv); $str_encrypt = base64_encode($str_encrypt); echo "加密後的內容是:".$str_encrypt."<br>"; $str_decrypt = base64_decode($str_decrypt); $str_decrypt = mcrypt_decrypt($cipher,$key,$str_encrypt,$mode,$iv); echo "解密後的內容:".$str_decrypt."<br>";
#EXAMPLE2
<?php
//$key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes $plaintext = "message to be encrypted"; $cipher = "aes-128-gcm"; if (in_array($cipher, openssl_get_cipher_methods())) { $ivlen = openssl_cipher_iv_length($cipher); $iv = openssl_random_pseudo_bytes($ivlen); $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag); //store $cipher, $iv, and $tag for decryption later $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag); echo $original_plaintext."\n"; } ?>