【问题标题】:PHP Regex String in Array数组中的 PHP 正则表达式字符串
【发布时间】:2012-10-29 04:09:24
【问题描述】:

我希望能够将像<anythinghere>@domain3.com 这样的全局用户名设置为 $usernames 数组中的用户名值(在下面的代码中)。这样我就可以根据域重定向用户,已经“认证”了。

我将在下面的代码中放置示例。

我可以在$X = <anything-so-long-as-not-blank>@domain3.com 处执行类似$usernames = array("username@domain1.com", $X) 的操作吗?

完整代码如下:

<?php

//VALIDATE USERS
$usernames = array("username@domain1.com", "username2@domain1.com", "username3@domain1.com", "username1@domain2.com", "username2@domain2.com", "username1@domain3.com");
$passwords = array("password1", "password2", "password3", "password4", "password5", "password6");


//REDIRECT SPECIFIC VALID USERS OR DOMAIN
function get_page($username) {
$username = strtolower($username);
switch ($username) {
    case "username@domain1.com"    : return "http://www.google.com";
    case "username2@domain1.com"    : return "http://www.yahoo.com";
    case "username3@domain1.com"    : return "http://www.stackoverflow.com";
    case "username1@domain2.com"    : return "http://www.serverfault.com";
        }
return preg_match('/@domain3\.com$/',$username) ?
"http://www.backblaze.com" : "DefaultBackupPage.php";
}
$page = get_page($_POST['username']);

for($i=0;$i<count($usernames);$i++)

{
  $logindata[$usernames[$i]]=$passwords[$i];
}

$found = 0; 

for($i=0;$i<count($usernames);$i++) 
{ 
   if ($usernames[$i] == $_POST["username"]) 
   { 
   $found = 1; 
   } 
} 
if ($found == 0) 
{ 
   header('Location: login.php?login_error=1'); 
   exit; 
} 
if($logindata[$_POST["username"]]==$_POST["password"])
{
   session_start();
   $_SESSION["username"]=$_POST["username"];
   header('Location: '.$page);
   exit;
}
else
{
   header('Location: login.php?login_error=1');
   exit;
}
?>

@inhan 已经像冠军一样帮助了我。我想知道是否有人可以让我过线?干杯!

【问题讨论】:

  • 您的逻辑似乎有些奇怪。你想提供一个带有通配符的用户名,这很公平——但你想匹配什么?
  • 所以我想要一个用户名,它可以在@domain.com 之前包含任何文本来匹配。然后重定向用户名的部分可以重定向有效的用户名。不将地址发送到错误页面。所以基本上我正在验证一个域。但它必须是 $username 数组中的一个值,否则我必须重写所有内容! (我不允许这样做)

标签: php regex arrays post


【解决方案1】:

您的代码需要先进行清理。如果您进行测试运行,其中会出现一堆错误。 IMO 也有点难读。

我在下面附上了一个工作代码示例。

// Get users
$input_pwd = ( isset( $_POST["password"] ) ? $_POST["password"] : '' );
$input_user = ( isset( $_POST["username"] ) ? $_POST["username"] : '' );

// Your pseudo database here ;)
$usernames = array(
    "username@domain1.com",
    "username2@domain1.com",
    "username3@domain1.com",
    "username1@domain2.com", 
    "/[a-z][A-Z][0-9]@domain2\.com/",   // use an emtpy password string for each of these
    "/[^@]+@domain3\.com/"              // entries if they don't need to authenticate
);

$passwords = array( "password1", "password2", "password3", "password4", "", "" );

// Create an array of username literals or patterns and corresponding redirection targets
$targets = array(
    "username@domain1.com"           => "http://www.google.com",
    "username2@domain1.com"          => "http://www.yahoo.com",
    "username3@domain1.com"          => "http://www.stackoverflow.com",
    "username1@domain2.com"          => "http://www.serverfault.com",
    "/[a-z][A-Z][0-9]@domain2\.com/" => "http://target-for-aA1-usertypes.com",
    "/[^@]+@domain3\.com/"           => "http://target-for-all-domain3-users.com",
    "/.+/"                           => "http://default-target-if-all-else-fails.com",
);

$logindata = array_combine( $usernames, $passwords );

if ( get_user_data( $input_user, $logindata ) === $input_pwd ) {

   session_start();
   $_SESSION["username"] = $input_user;
   header('Location: ' . get_user_data( $input_user, $targets ) );
   exit;

} else {
    // Supplied username is invalid, or the corresponding password doesn't match
   header('Location: login.php?login_error=1'); 
   exit; 
}

function get_user_data ( $user, array $data ) {

    $retrieved = null;

    foreach ( $data as $user_pattern => $value ) {

        if (
               ( $user_pattern[0] == '/' and preg_match( $user_pattern, $user ) )
            or ( $user_pattern[0] != '/' and $user_pattern === $user)
        ) {

            $retrieved = $value;
            break;

        }

    }

    return $retrieved;

}

【讨论】:

  • 谢谢迈克尔,这绝对是更好的布局代码。我的问题是,即使使用此设置,我也无法在 @domain.com 之前输入任何前缀并被重定向。因为我仍然不是有效用户。我们尚未定义在 domain.com 上验证任何前缀的能力。重定向,是的,但由于不是有效用户,我们会被抛出一个错误页面。至少那是我读过的吗?如果我错了,请纠正我!干杯。
  • 是的,上面的代码将有效用户重定向到数组中定义的目标。无效登录被发送到错误页面。这就是我阅读您的代码和您的问题的方式......无论如何,如果您也想重定向无效用户,为什么在处理它们时不调用 get_redirection_target ,而不是将它们发送到 'login.php?login_error=1' ?
  • 干杯伙伴,基本上我的目标是使包含在有效域之前的任何文本都被完全接受为有效用户名。不要重定向无效的用户名。它有两个部分,重定向是正确的,验证域名部分是我现在遇到问题的地方。如果不能以这种方式完成,我会把它放在下巴上。我只是希望您可以在 [a-z][A-Z]@domain.com 之类的用户名数组中放入一些正则表达式并完成这项工作...感谢您迄今为止所做的努力!
  • 慢慢到达那里 ;) 所以你希望任何用户名都是合法的,如果它与“/无论你想出什么模式/@domain.com”模式匹配。正确的?这些人需要提供密码吗?他们都有相同的密码吗?
  • 太棒了,我们在同一页上!哈哈。是的,每个域都有相同的密码。他们不提供密码。虽然,我猜你可以说他们为域提供了用户名前缀。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
相关资源
最近更新 更多