


PHP can also do great things - Detailed explanation of encoding and decoding in PHP_PHP Tutorial
Detailed explanation of encoding and decoding in PHP can also do great things with PHP
PHP can also do great things with detailed explanation of encoding and decoding in PHP
This article mainly introduces the detailed explanation of encoding and decoding in PHP that PHP can do great things. This article explains ASCII encoding and decoding, URL encoding and decoding, Base64 encoding and decoding, HTML entity encoding and decoding, binary, octal, decimal, and hexadecimal For content such as system conversion and mutual conversion, friends in need can refer to it
Write in front
PHP can also do great things. This is the classic usage of PHP syntax features and related function libraries that I have summarized. It may not really be able to achieve the effect of making a big difference, but mastering these methods can be of some help in your work and study. , I hope everyone can brainstorm and make "PHP Can Do Big Things" more exciting! Please indicate the source for reprinting (jb51.net)
2. Foreword
PHP is a common scripting language, mainly because it is easy to learn and quick to use. Almost 50% of web programs have PHP (incomplete statistics). PHP provides a wealth of functions and API interfaces for development, which allows us to use its powerful built-in functions and extensions very conveniently. This article is the first article in the series "PHP Can Do Big Things", which mainly summarizes the advantages of PHP in encoding, decoding, Knowledge of base conversion.
3. PHP encoding and decoding
1. ASCII encoding and decoding
ASCII (pronunciation: English pronunciation: /ˈæski/ ASS-kee, American Standard Code for Information Interchange, American Standard Code for Information Interchange) is a computer coding system based on the Latin alphabet. It is mainly used to display modern English, while its extended version EASCII can partially support other Western European languages and is equivalent to the international standard ISO/IEC 646. As the World Wide Web made ASCII widely used, it was gradually replaced by Unicode until December 2007. https://zh.wikipedia.org/zh/ASCII

PHP basic functions have built-in ASCII encoding and decoding functions, which allows us to easily perform ASCII encoding and decoding.
int ord (string $string) //Returns the ASCII code value of the first character of string string.
string chr (int $ascii) //Returns a single character corresponding to ascii specified.
The code is as follows:
$str = 'Welcome to China';
Function getNum($string){
$needle = 0;
$num = '';
while (isset($string[$needle])) {
$num .= $num==0?'':' ';
$num .= ord($string[$needle]);
$needle ;
}
return $num;
}
Function getChar($num){
$num_arr = explode(' ', $num);
$string = '';
foreach ($num_arr as $value) {
$string .= chr($value);
}
return $string;
}
echo "Character to ASCII code n";
echo getNum($str);
echo "n";
echo "ASCII character n";
echo getChar(getNum($str));
/* @OUTPUT
Character to ASCII code
87 101 108 99 111 109 101 32 116 111 32 67 104 105 110 97
ASCII character
Welcome to China
*/
?>
2. URL encoding and decoding
URL encoding is a format used by browsers to package form inputs. The browser retrieves all names and values from the form and sends them to the server as part of the URL or separately as name/value parameter encoding. For example, when we visit a web page, there will be many strings with %, which is URL encoding.
URL encoding generally uses UTF-8 encoding format, so it is recommended to use UTF-8 format to transfer data. The URL encoding in the normal sense can be understood as the hexadecimal number of the ASCII code plus % before it, and there is no case distinction.
The code is as follows:
string urlencode(string $str) //This function facilitates encoding a string and using it in the request part of the URL. It also facilitates passing variables to the next page. Spaces are encoded as .
string urldecode(string $str) //Decode any %XX in the given encoded string, the plus sign (' ') is decoded into a space character.
string rawurlencode (string $str) //Encode the specified characters according to RFC 3986, and convert spaces into .
string rawurldecode (string $str) //Returns a string. The sequence of percent signs (%) followed by two hexadecimal digits in this string will be replaced with literal characters. Not converted to spaces.
The two sets of functions have the same usage, except for the conversion processing of and spaces: rawurlencode converts spaces into and does not convert into spaces; urlencode is different.
The code is as follows:
$str_arr = array(
'www.jb51.net',
'http://www.jb51.net/',
'PHP can also do great things',
'!@#$%^&*()_ =-~`[]{}|\;:'",./?'
);
foreach ($str_arr as $key => $value) {
echo $value,"t->t",urlencode($value),"n";
}
/* @OUTPUT
www.jb51.net -> www.jb51.net
http://www.jb51.net/ -> http://www.jb51.net/
PHP can also do big things -> PHP can also do big things
!@#$%^&*()_ =-~`[]{}|;:'",./? -> !@#$%^&*()_+ =-~`[]{}|;:'",./?
*/
?>
3. Base64 encoding and decoding
Base64 is a representation method for binary data based on 64 printable characters. Since 2 to the 6th power is equal to 64, every 6 bits is a unit, corresponding to a printable character. Three bytes have 24 bits, corresponding to 4 Base64 units, that is, 3 bytes need to be represented by 4 printable characters. It can be used as a transfer encoding for email. The characters used include 26 uppercase and lowercase letters, plus 10 numbers, plus sign " ", slash "/", a total of 64 characters, and the equal sign "=" is used as a suffix. The complete base64 definition can be found in RFC 1421 and RFC 2045. The encoded data is slightly longer than the original data, 4/3 of the original length. In emails, according to RFC 822, a carriage return and line feed must be added for every 76 characters. It can be estimated that the encoded data length is approximately 135.1% of the original length. https://zh.wikipedia.org/zh/Base64
string base64_encode(string $data) //Use base64 to encode data.
string base64_decode (string $data [, bool $strict = false ]) //Decode base64 encoded data.
Case: The img tag in the HTML page can use base64 encoding in the src attribute to output images, which can reduce the number of HTTP requests.
Copy the code. The code is as follows:
$string = file_get_content('3mc2.png');
echo ';
/* @OUTPUT
UEhQ5Lmf6IO95Yqe5aSn5LqL
*/
?>
4. HTML entity encoding and decoding
Some characters are reserved in HTML and have special meanings. For example, the less than sign "
string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = “UTF-8″ [, bool $double_encode = true ]]] ) //Convert HTML to the following HTML special characters Entity encoding
1.'&' (ampersand) becomes ‘&'
2.'"' (double quote) becomes ‘"' when ENT_NOQUOTES is not set.
3."‘" (single quote) becomes ‘'' (or ') only when ENT_QUOTES is set.
4.'
5.'>' (greater than) becomes ‘>'
string htmlspecialchars_decode (string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ]) //The function of this function is exactly the opposite of htmlspecialchars(). It converts special HTML entities back to normal characters.
There is also a function htmlentities/html_entity_decode with the same function. This pair of functions encodes HTML entities even for Chinese characters, and will produce garbled characters, so it is recommended to use htmlspecialchars for encoding and decoding.
Case: To prevent XSS cross-site scripting attacks, the data submitted by the user needs to be converted into HTML entities:
The code is as follows:
$_POST['message'] = 'Test message character'">

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
