Develo Design

Develo Design Menu

Magento 2 – A fix for “mcrypt_module_open() is deprecated”

One of the requirements for Magento 2 is mcrpyt, however in PHP7.1 this extension is now officially deprecated. For those who still want to use PHP7.1 for the other benefits it offers (features, performance) or are stuck with it (shared hosting?) and don’t have mcrypt available to use, you’ll have a hard time getting Magento 2 to play ball with the server running PHP7.1.

You’ll come across errors when running bin/magento setup:upgrade

magento2_mcrypt_console_error

You may also come across similar errors when trying to access your Magento 2 website in general; you have a few options to get around this issue…

Update your Magento 2 version to the latest version:
2.2 at time of writing, is the version i noticed that fixes the mcrypt error (and thankfully a ton of other errors), however in a realistic world, it’s not always feasible to do so. If upgrading Magento to version 2.2 is not an option, this still leaves us with two other possible solutions; suppress the error or change the Encryption/Crypt.php file.

Suppressing the error (or any deprecation errors):
In your index.php file (whichever it serves from), add the following

error_reporting(E_ALL & ~E_DEPRECATED);
or
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

This should help suppress any deprecation errors that it throws, however don’t be surprised if when in developer mode deploy:mode:set developer it changes your preferred error reporting settings.

Change the Encryption/Crypt.php file:
The last solution to the problem is to make some changes in the Crypt.php file itself. I don’t condone “hacking the core”, however if this is your last resort ie, your sites unusable from this error, it might be your only solution. If you’re on Magento 2.1.6 and are soon upgrading to Magento 2.2, the upgrade will override/patch this file to add in the same changes.

Theres an operator in PHP that allows you to individually suppress any errors that come from a piece of code. You can add the @ symbol in front of an expression and “hey presto”, no errors.

We can use this to suppress calls to instances of mcrypt so our Magento 2 store will run again. Go through the Encryption/Crypt.php file and add @ in front of  any of  the calls to the mcrypt extension.
mcrypt_module_open will now be @mcrypt_module_open
mcrypt_enc_get_key_size will now be @mcrypt_enc_get_key_size
mcrypt_module_close will now be @mcrypt_module_close

… You get the idea 👏

The best solution here, if possible would be to update your Magento 2 version. If that isn’t possible then try the above solutions to get your Magento 2 store back up and running again. If you went down the route of changing the Encryption/Crypt.php file, heres the patched version of it for you to use which should be backwards compatible (tested on Magento 2.1.6 and PHP7.1) Encryption/Crypt.php

Share

Loading Disqus Comments ...
Loading Facebook Comments ...

Post Comment

Your email address will not be published. Required fields are marked *

Discuss My Project

We want to hear about your project! Provide us with a few details and we’ll get back to you for a chat.