简体   繁体   English

如何读取通过表单输入的电子邮件地址的域名

[英]How to read the domain name of an email address input via a form

I have a login form, and I want to be able to extract the domain name of the email address input then overwrite a pre-defined variable with a new one just read.我有一个登录表单,我希望能够提取电子邮件地址输入的域名,然后用刚刚读取的新变量覆盖预定义的变量。

This is my code:这是我的代码:

<form action="example.php" method="POST">
<input type="text" name="input_value"> //assume I input gooogle@gmail.com
<input type="hidden" name="isp" id="isp" value="unknown" />
<input type="submit" name="submit">

example.php:例子.php:

<?php
$isp = $_POST["isp"];
//rest of code

?>

I want to be able to read the domain and convert the variable $isp to a pre-defined value I put in place eg 'gmail.com' = 'gmail', hence $isp = 'gmail' .我希望能够读取域并将变量 $isp 转换为我设置的预定义值,例如 'gmail.com' = 'gmail',因此$isp = 'gmail'

You really should rely on imap_rfc822_parse_adrlist from the php-imap module which uses the c-client API.您确实应该依赖使用c-client API 的php-imap模块中的imap_rfc822_parse_adrlist It processes a comma-seperated list and returns an array of objects and allows the format User <user@example.com .它处理一个逗号分隔的列表并返回一个对象数组并允许格式User <user@example.com With some additional logic you can fit it to your needs.通过一些额外的逻辑,您可以满足您的需求。

You stated in comments that you do not want to rely on installation dependencies.您在评论中表示您不想依赖安装依赖项。 I strongly recommend to do so, even if there is a filter_var function providing an email filter as well.我强烈建议这样做,即使有filter_var函数也提供电子邮件过滤器。 It is not well implemented, producing false negatives.没有很好地实施,产生假阴性。 I would even not rely on it hoping it does not produce false positives.我什至不会依赖它,希望它不会产生误报。

Eg RFC 822 allows quoted strings to appear in the mailbox part.例如,RFC 822 允许带引号的字符串出现在邮箱部分。 Both, imap_rfc822_parse_adrlist and filter_var , consider "abc.def@ghi.jkl"@example.com and abc."def@ghi".jkl@example.com to be valid. imap_rfc822_parse_adrlistfilter_var认为"abc.def@ghi.jkl"@example.comabc."def@ghi".jkl@example.com是有效的。 However, abc.def"@"ghi.jkl@example.com is according to the RFC 882 specs valid as well but a false negative of filter_var .但是, abc.def"@"ghi.jkl@example.com根据 RFC 882 规范也有效,但是filter_var的假阴性。 There are issues with IDN domains as well and probably many more. IDN 域也存在问题,可能还有更多问题。

Implementing a full RFC 822 compliant regex or algorithm in PHP is a very hard and error-prone task.在 PHP 中实现完全符合 RFC 822 的正则表达式或算法是一项非常困难且容易出错的任务。 Even simple email formats should not be validated by regex or similar since this will become a root of bugs or even serious security issues in larger projects some day.即使是简单的电子邮件格式也不应该通过正则表达式或类似的方式进行验证,因为这将成为错误的根源,甚至有一天会成为大型项目中严重安全问题的根源。

I can provide you some fallback relying on filter_var.我可以为您提供一些依靠 filter_var 的后备方案。 Again, I strongly recommend not to enable it by default.同样,我强烈建议不要默认启用它。 You can deliver it with a warning to enable it on the own risk.您可以提供警告以启用它,风险自负。

<?php  declare (strict_types=1);

// This should NOT be enabled. If possible, ensure to have installed the php-imap module.
//
// define('ALLOW_FALLBACK_EMAIL_CHECK', true);


if(function_exists('imap_rfc822_parse_adrlist'))
{
  /**
   * Get the host part (domain) of an email address.
   *
   * @param  string|null  $email  email address, can be null
   * @return string|null          host part of the email address if the address is valid, otherwise NULL
   */
  function get_host_from_email_addr(?string $email) : ?string
  {
    if(null === $email)
      return null;

    @[$obj, $obj2] = imap_rfc822_parse_adrlist($email, '');
    imap_errors(); // flush errors, otherwise unsuppressable notifications on wrong format are shown due to external calls

    // we want to allow 'simple email addresses' only and therefore exact the 2 properties:
    //   ->mailbox  and  ->host
    return (isset($obj->mailbox, $obj->host) && !isset($obj2) && 2 === count(get_object_vars($obj)))
      ? $obj->host ?: null
      : null
    ;
  }
}

else if(defined('ALLOW_FALLBACK_EMAIL_CHECK') && ALLOW_FALLBACK_EMAIL_CHECK)
{
  function get_host_from_email_addr(?string $email) : ?string
  {
    // This probably ensures getting a valid host name from email address.
    // However, filter_var works too restrictive generating false negatives.
    return filter_var($email, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE) ? array_slice(explode('@', $email), -1)[0] : null ;
  }
}

else
  throw new Error('Unresolved dependency: Please install php-imap module or set constant ALLOW_FALLBACK_EMAIL_CHECK to TRUE.');


$isp_names =
[
  'gmail.com'   => 'Gmail',
  'outlook.com' => 'Outlook',
];

$isp = @$_GET['isp'] ?: $isp_names[get_host_from_email_addr(@$_GET['input_value'])] ?? 'unknown';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM