Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if mcrypt extension exists in php

I would like to know the simplest and fastest PHP code line to check if mcrypt extension is available/installed.

There is a function that encrypts a string and first it requires to check if mcrypt is usable. If not, it will execute an alternative encrypt solution available on the system.

Thanks!

like image 615
Heroselohim Avatar asked Aug 24 '14 22:08

Heroselohim


People also ask

How do I know if PHP mcrypt is installed?

Determine if the mcrypt extension is loaded in any of the following ways: Set up a phpinfo. php file in the web server's root directory and examine the output in a web browser. Run the following command: $ php -r "phpinfo();" | grep mcrypt.

What is mcrypt PHP extension?

The mcrypt PHP module provides an interface to the mcrypt library and supports encryption. The cPanel-provided EasyApache 4 profiles include the mcrypt PHP module by default.

What replaces PHP mcrypt?

ext/mcrypt ¶ The mcrypt extension has been abandonware for nearly a decade now, and was also fairly complex to use. It has therefore been deprecated in favour of OpenSSL, where it will be removed from the core and into PECL in PHP 7.2.


1 Answers

You can use function_exists to check if one of the mcrypt functions exists.

if(function_exists('mcrypt_encrypt')) {
    echo "mcrypt is loaded!";
} else {
    echo "mcrypt isn't loaded!";
}

Edit 30.07.2016:
Since my answer still gets a few upvotes from time to time, I benchmarked the performance of mine and Cristi Draghici's answers. The conclusion is, that function_exists is a bit faster than extension_loaded. https://3v4l.org/So4Ep

like image 137
Charlotte Dunois Avatar answered Sep 28 '22 00:09

Charlotte Dunois