17

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!

3
  • Have you searched to see if there is a generic PHP function that checks whether a function exists? Commented Aug 24, 2014 at 22:28
  • Yes, you are right with that, but also I was looking for the fastest check code. Commented Aug 24, 2014 at 22:46
  • you can use some of the techniques from stackoverflow.com/questions/3131411/… Commented Aug 8, 2016 at 7:18

3 Answers 3

46

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

Sign up to request clarification or add additional context in comments.

Comments

17

You can also use extension_loaded():

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

Comments

1

If you are using a development environment like XXAMP, or WAMP, there should be a default "phpinfo" page. For example, in XXAMP it would be:

http://localhost/dashboard/phpinfo.php

You can also achieve this same screen by viewing a php file that has: phpinfo(); somewhere in the code.

In this screen, simply search for the string "mcrypt support". If installed, you will see a box that says "enabled".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.