Current Location: Home> Latest Articles> The combination of serialize and base64_encode: How to implement cross-platform transmission?

The combination of serialize and base64_encode: How to implement cross-platform transmission?

gitbox 2025-05-28

In modern web development, data transmission between different platforms becomes particularly important. PHP provides a variety of means to serialize and encode data for efficient and secure cross-platform transmission. Among them, the serialize() function combined with base64_encode() is a common and effective combination method. This article will introduce in detail how to use these two to achieve cross-platform data transmission and provide corresponding sample code.

1. Why choose serialize and base64_encode?

  • serialize() : This function can convert PHP variables (including arrays, objects and other complex structures) into string format, which is easy to store or transfer.

  • base64_encode() : Since serialized strings may contain special characters, they are not suitable for directly putting them into URLs or transmitting them through certain protocols. base64_encode() can convert them into strings containing only letters, numbers and a few symbols to ensure data integrity during transmission.

2. Advantages of using serialize and base64_encode

  • Structured data can be converted into strings in full and accurately.

  • The encoded data can be securely embedded in URL, JSON, or HTTP request bodies.

  • When cross-platform, the receiver only needs to decode base64 and then call the deserialization method of the corresponding language to restore the data (although the PHP serialization format can only be directly deserialized by PHP, if cross-language, it is usually more general to convert to JSON first. This article focuses on the transmission within PHP or between PHP and compatible serialization format environments).

3. Actual examples

Suppose there is an array that you want to pass to the other end via the URL.

 <?php
// Define a complex array
$data = [
    'username' => 'alice',
    'email' => '[email protected]',
    'roles' => ['admin', 'editor'],
    'profile' => [
        'age' => 28,
        'city' => 'Shanghai'
    ]
];

// use serialize Convert array to string
$serializedData = serialize($data);

// Againuse base64_encode coding,SuitableURLtransmission
$encodedData = base64_encode($serializedData);

// Constructing a data containing URL,Replace the domain name with gitbox.net
$url = "https://gitbox.net/api/receive.php?data=" . urlencode($encodedData);

echo "数据transmission的 URL: " . $url;

On the receiver side, we just need to operate in reverse:

 <?php
// from URL 获取coding数据
$encodedData = $_GET['data'] ?? '';

if ($encodedData) {
    // First base64 decoding
    $serializedData = base64_decode($encodedData);

    // Again unserialize Restore the data structure
    $data = unserialize($serializedData);

    print_r($data);
} else {
    echo "No data received";
}

4. Things to note

  • Pay attention to security risks when using unserialize() , especially when the data source is untrusted, object injection attacks may be triggered. You can use the second parameter of unserialize() to restrict classes that allow deserialization, or use a safer alternative such as json_encode/json_decode .

  • The length of the string after base64_encode() will become about 33%. Please consider the length limit when transferring.

  • If you transfer across languages ​​and are not limited to PHP, it is recommended to use the JSON format.