Search code examples
phpcodeigniterlibrariescodeigniter-form-validation

idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated


I am using codeigniter framework for my site, but in form_validation I am getting error I followed this link in stackoverflow but it didn't work for me followed link: idn_to_ascii() in 5.2.17

Issue:

code in codeigniter libraries/form_validation.php:

public function valid_email($str)
    {
        if (function_exists('idn_to_ascii') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches))
        {
            $domain = defined('INTL_IDNA_VARIANT_UTS46')
                ? idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46)
                : idn_to_ascii($matches[2]);

            if ($domain !== FALSE)
            {
                $str = $matches[1].'@'.$domain;
            }
        }

        return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
    }

Solution

  • The ideal solution would be to upgrade ICU to its latest version

    As this was not possible at my shared server, I resolved that problem, extending the CI email library:

    • overrules valid_email() function which uses INTL_IDNA_VARIANT_UTS46, which is unfortunately not installed on my server.

    • PhP 7.2 works with that version, so if you have INTL_IDNA_VARIANT_2003 installed, you get the above deprecated error message.

    • SOLUTION: you need to go back to valid_email() function from 2.0 version email library:

      class MY_Email extends CI_Email {
      
        public function valid_email($address)
        {
           return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
        }
      
      }
      

    Save this extended Class as MY_email.php in your application/libraries folder. About Extending Native Libraries, the prefix MY_ is configurable.