4

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);
    }
user_1234
  • 741
  • 1
  • 9
  • 22
  • what are your CI and PHP versions? – Vickel Dec 08 '20 at 14:38
  • does this answer help? https://magento.stackexchange.com/a/255145 – Vickel Dec 08 '20 at 14:44
  • @Vickel codeigniter version 3.1.11 – user_1234 Dec 09 '20 at 04:58
  • Please don't post images of text; just paste the text itself into the question. As well as making the question unreadable by anyone with vision impairments or using a limited display, it means that anyone searching for that error message because they have the same problem won't find your question. – IMSoP Dec 09 '20 at 14:06

1 Answers1

4

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.

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • Just to clarify, it is not true that "PHP 7.2 only works with that". It still **works** with `INTL_IDNA_VARIANT_2003`; the message is saying that it is **deprecated**, i.e. support **will be removed in a future version**. It's perfectly safe to ignore that message until you're getting ready to upgrade to the version where it's removed, probably to PHP 8.0, by which time you will probably also be able to update the ICU library – IMSoP Dec 09 '20 at 14:10
  • @Vickel I will check and let u know – user_1234 Dec 09 '20 at 14:18
  • 1
    no, just extend the existing one, see the link in the answer about extending Libs – Vickel Dec 09 '20 at 14:24
  • @Vickel last point I want to ask should I use like this $this->email->valid_email('someval');, if this is not the correct way ell me extact way,I niether used custom libraries or extended libraries – user_1234 Dec 09 '20 at 15:23
  • 1
    you only need to extend the library, which you do creating a MY_email.php and place it in the folder I mentioned. That's all. CI will find it automatically and use that new function, whenever needed. You limit yourself using your email library as you would do normally. – Vickel Dec 09 '20 at 15:28