Codeigniter: idn_to_ascii() on PHP 7.2

Created on 14 Oct 2017  路  19Comments  路  Source: bcit-ci/CodeIgniter

CodeIgniter 3.1.6, PHP 7.2.0RC3

As of PHP 7.2 the constant INTL_IDNA_VARIANT_2003 is deprecated.
https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003

The function idn_to_ascii() as is currently used throws the warning:

A PHP Error was encountered
Severity: 8192
Message: idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated
...

There are three places to be updated:

Email library, the method valid_email()
https://github.com/bcit-ci/CodeIgniter/blob/3.1.6/system/libraries/Email.php#L1035

With the following snippet:

        $this->load->library('email');
        echo $this->email->valid_email('test@Blo脽.de') ? 'yes' : 'no';

I tried this modification that seems to me ok:

    public function valid_email($email)
    {
        if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && $atpos = strpos($email, '@'))
        {
            $email = self::substr($email, 0, ++$atpos).idn_to_ascii(self::substr($email, $atpos), 0, INTL_IDNA_VARIANT_UTS46);
        }

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

The second place that would need a similar modification is CI_Email::valid_email()
https://github.com/bcit-ci/CodeIgniter/blob/3.1.6/system/libraries/Email.php#L1852

And the third pace is CI_Form_validation::valid_email()
https://github.com/bcit-ci/CodeIgniter/blob/3.1.6/system/libraries/Form_validation.php#L1230

Enhancement

Most helpful comment

hello all,
for idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated
just add this in system/libraries/form_validation.php in line 1234

    public function valid_email($str)
    {
        if (function_exists('idn_to_ascii') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches))
        {
            $str = $matches[1].'@'.idn_to_ascii($matches[2],IDNA_NONTRANSITIONAL_TO_ASCII,INTL_IDNA_VARIANT_UTS46);
        }

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

correct me any mistake.
its work for me with CI.3.1.7

many thanks,

udin

All 19 comments

Thanks, I've pushed a patch for this.

hello all,
for idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated
just add this in system/libraries/form_validation.php in line 1234

    public function valid_email($str)
    {
        if (function_exists('idn_to_ascii') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches))
        {
            $str = $matches[1].'@'.idn_to_ascii($matches[2],IDNA_NONTRANSITIONAL_TO_ASCII,INTL_IDNA_VARIANT_UTS46);
        }

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

correct me any mistake.
its work for me with CI.3.1.7

many thanks,

udin

CI 3.1.7 doesn't have this issue; I don't understand why you're suggesting modifications at all.

dear narfbg,

yes it does...iam facing that problem with CI 3.1.7 with PHP 7.2 on Xampp...so if any others face it that works for me.

many thanks for your reply.

No, it doesn't.

Particularly on PHP 7.2 the only way to trigger that notice is if you have an ICU version lower than 4.6, at which point there's literally nothing you can do about it.

Read more here: https://forum.codeigniter.com/thread-69780.html

@narfbg
This is also a problem for me.
PHP ver:7.2
CI ver: 1.3.9

Same here PHP 7.2 CI 3.1.8

why is this closed? this is not fixed. i got this error on my server but works on local weird!? fix pls! im not using this CI email validation at moment. id rather override this with my own email php validation or on js. this is no good

Same here PHP 7.2.1 & CI 3.1.8 . "valid_email()" has a serious issue .

that shit also error when you use their email library

That shit is the fault of whoever setup your servers with libicu versions from 10 years ago that nobody should be using anymore. And before talking shit, you should at least read the shit that I wrote.

I'll repeat and simplify: this is impossible to fix! Literally. Not possible.

PHP 7.4 will require libicu 50+, at which point you'll be forced to have a proper setup and the error message will go away, but until then - start bothering your server guys, hosting companies, etc. It's their fault.

I'm facing same issue its working fine on same version in localhost but when i place my project live on server it stuck with this error, kindly suggest me the best possible solution.

@saadi09 the solution i did was to override the CI core function to validate email. Go to system/libraries/Email.php and find the validate_email function and replace with this. you can also rename it for backup and paste this below it.

public function validate_email($email){
if ( ! is_array($email))
{
$this->_set_error_message('lang:email_must_be_array');
return FALSE;
}

foreach ($email as $val)
{
    if ( ! $this->valid_email($val))
    {
        $this->_set_error_message('lang:email_invalid_address', $val);
        return FALSE;
    }
}

return TRUE;

}

public function valid_email($email){
return (bool) filter_var(trim($email), FILTER_VALIDATE_EMAIL);
}

Thanks :) it works pretty fine now although I'm using my own validation library rather than this one.

for anyone still getting this for php >= 7.2 try checking if INTL_IDNA_VARIANT_UTS46 constant is defined
I added these lines on system/libraries/Email.php and system/libraries/Form_validation.php

if(!defined('INTL_IDNA_VARIANT_UTS46')) { define ('INTL_IDNA_VARIANT_UTS46', 1); }

Well, this is my case with this error:
ERROR - 2020-02-07 15:35:11 --> Severity: 8192 --> Function mcrypt_create_iv() is deprecated public_html/system/core/Security.php 578
ERROR - 2020-02-07 15:35:11 --> Severity: 8192 --> idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated public_html/system/libraries/Form_validation.php 1249

PHP version:
[root@one logs]# php -v
PHP 7.2.27 (cli) (built: Jan 22 2020 09:31:55) ( NTS ) Copyright (c) 1997-2018 The PHP Group

LIBICU version:
[root@one logs]# yum install libicu
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package libicu-50.2-3.el7.x86_64 already installed and latest version
Nothing to do
[root@one logs]#

CodeIgniter version:
const CI_VERSION = '3.1.11';

I think I don't meet the criteria for the diagnosis here. Am i missing anything?

Yes, you shouldn't be getting the warning with libicu 50. The version you have installed right now might differ from the one your PHP is linked against - that's my only guess.

Yes, you shouldn't be getting the warning with libicu 50. The version you have installed right now might differ from the one your PHP is linked against - that's my only guess.

Thanks for your reply. I'll look in that direction.

I had the same problem after this solution it's solved but $this->session->set_flashdata('smsg', 'Mail has been sent successfully.'); is not working. please help. BTW it worked before.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tjoosten picture Tjoosten  路  6Comments

erikk1986 picture erikk1986  路  3Comments

cyberhck picture cyberhck  路  5Comments

rmdhfz picture rmdhfz  路  3Comments

vahidvdn picture vahidvdn  路  4Comments