Fatal error: Call to undefined function mb_strlen()
When you encounter a Fatal error: Call to undefined function mb_strlen() in your PHP logs, it is almost never a mystery of logic or database configuration. It is strictly an environment issue. The error message points directly to line 14 of your file, where the code attempts to calculate the length of a string using multi-byte character support. However, the mb_strlen function simply does not exist in the current PHP build running on your server.
This specific scenario often arises when deploying legacy tutorials or older scripts onto modern hosting environments. In the source material provided, the developer is building a donation center based on a 2010 tutorial. The code relies heavily on mb_strlen to validate input length and ensure that names and messages contain at least two characters. While this logic is sound for handling UTF-8 dataâsuch as emojis or accented characters like é or ñâthe function was not included in the default PHP configuration when the script was originally written, or it has been disabled on the new host.
To resolve this immediately without rewriting the entire validation layer, you must enable the mbstring extension. The quickest way to do this is via your server's configuration interface. If you are using cPanel, navigate to Select PHP Version and ensure that mbstring is enabled in the list of available extensions. Alternatively, if you have SSH access, you can edit the php.ini file (located in /etc/php/7.x/cli/php.ini or /etc/php/7.x/apache2/php.ini, depending on your setup) and uncomment the line:
extension=mbstring.so
Once saved and restarted, run php -m from the command line to verify that mbstring appears in the list. This step restores the missing function instantly, allowing the existing validation logic to execute as intended.
However, fixing the immediate error is only half the battle. The source code provided contains significant security vulnerabilities that you should address alongside this fix. The script uses a deprecated database interface: mysql_query. The original MySQL extension (mysql_*) was removed in PHP 7.0. If your server is running PHP 7 or higher, which is standard for almost all shared hosting today, the code will fail with a new fatal error even after fixing mb_strlen.
You must migrate to either MySQLi or PDO. For this specific script, converting to MySQLi is straightforward. Replace mysql_query with mysqli_query and ensure you are using prepared statements instead of string concatenation for your SQL insert. String concatenation exposes the application to SQL injection attacks, especially when dealing with user input like $nameField and $messageField.
Furthermore, the script uses a custom esc() function. In modern PHP development, you should rely on built-in functions or framework helpers. If you are not using a framework, ensure that htmlspecialchars is applied to output data before echoing it back to the browser. The original tutorial from 2010 assumed an older ecosystem where these details were less scrutinized; today's standards require stricter sanitization.
If you are looking for a more robust alternative to maintaining legacy PHP scripts, consider migrating this donation center logic into a modern framework like Laravel. A framework handles extension requirements, database migrations, and security best practices out of the box. While the immediate fix involves enabling mbstring and upgrading your database layer, long-term maintenance suggests moving away from raw mysql_* calls entirely.
Finally, always verify your server environment against the script's requirements before deployment. A 2010 tutorial may assume PHP 5.x with specific extensions disabled by default on modern hosts. Checking phpinfo() and enabling necessary extensions like mbstring, pdo_mysql, and openssl is a mandatory step in any production deployment of legacy code.
