0

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

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

$usernames = array("username@domain1.com", $X)我可以在哪里做类似的事情$X = <anything-so-long-as-not-blank>@domain3.com吗?

完整代码如下:

<?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 已经像冠军一样帮助了我。我想知道是否有人可以让我过线?干杯!

4

1 回答 1

0

您的代码首先需要清理。如果您进行测试运行,其中会出现一堆错误。阅读 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;

}
于 2012-10-29T11:19:12.443 回答