2012-10-29 3 views
0

<anythinghere>@domain3.com과 같은 전역 사용자 이름을 $ usernames 배열 (아래 코드)의 사용자 이름 값으로 설정할 수 있기를 바랍니다. 이것은 내가 "인증 된"도메인을 기반으로 사용자를 리디렉션 할 수 있도록하기위한 것입니다.배열의 PHP 정규식 문자열

아래 코드에 예를 넣습니다.

$usernames = array("[email protected]", $X)과 같은 작업을 수행 할 수 있습니까? $X = <anything-so-long-as-not-blank>@domain3.com? 다음은

전체 코드 :

<?php 

//VALIDATE USERS 
$usernames = array("[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"); 
$passwords = array("password1", "password2", "password3", "password4", "password5", "password6"); 


//REDIRECT SPECIFIC VALID USERS OR DOMAIN 
function get_page($username) { 
$username = strtolower($username); 
switch ($username) { 
    case "[email protected]" : return "http://www.google.com"; 
    case "[email protected]" : return "http://www.yahoo.com"; 
    case "[email protected]" : return "http://www.stackoverflow.com"; 
    case "[email protected]" : 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 이미 챔피언처럼 도움이되었습니다. 어떤 사람이 나를 넘어 뜨릴 수 있는지 궁금하네요? 건배!

+0

논리에 이상한 것으로 보입니다. 와일드 카드로 사용자 이름을 제공하기를 원하지만, 무엇을 원하고 싶습니까? – hashchange

+0

그래서 @ domain.com 앞에있는 모든 텍스트가 일치 할 수있는 사용자 이름을 원합니다. 사용자 이름을 리디렉션하는 섹션은 올바른 사용자 이름을 리디렉션 할 수 있습니다. 주소를 오류 페이지로 보내지 마십시오. 그래서 기본적으로 나는 도메인을 검증하고 있습니다. 하지만 그것은 $ username 배열의 값이어야합니다. 그렇지 않으면 모든 것을 다시 써야합니다! (어느 쪽이 허용되지 않았는지) – James

답변

0

코드를 먼저 정리해야합니다. 테스트를 실행하면 오류가 많습니다. IMO를 읽는 것도 약간 어렵습니다.

아래와 같이 작동 코드 샘플을 첨부했습니다.

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

// Your pseudo database here ;) 
$usernames = array(
    "[email protected]", 
    "[email protected]", 
    "[email protected]", 
    "[email protected]", 
    "/[a-z][A-Z][0-9]@domain2\.com/", // use an emtpy password string for each of these 
    "/[^@][email protected]\.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(
    "[email protected]"   => "http://www.google.com", 
    "[email protected]"   => "http://www.yahoo.com", 
    "[email protected]"   => "http://www.stackoverflow.com", 
    "[email protected]"   => "http://www.serverfault.com", 
    "/[a-z][A-Z][0-9]@domain2\.com/" => "http://target-for-aA1-usertypes.com", 
    "/[^@][email protected]\.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; 

} 
+0

감사합니다. Michael, 코드를 배치하는 것이 훨씬 낫습니다. 내 문제는이 설정으로도 @ domain.com 앞에 접두사를 입력 할 수없고 리디렉션 될 수 없다는 것입니다. 나는 여전히 유효한 사용자가 아니기 때문에. domain.com의 접두사를 확인하는 기능을 정의하지 않았습니다. 리디렉션은 가능하지만 유효한 사용자가 아니기 때문에 오류 페이지가 표시됩니다. 적어도 그것은 그것을 읽은 그대로입니까? 내가 틀렸다면 나를 바로 잡아주세요! 건배. – James

+0

예, 위 코드는 유효한 사용자를 배열에 정의 된 대상으로 리디렉션합니다. 잘못된 로그인이 오류 페이지로 전송됩니다. 그것이 당신의 코드와 질문을 읽는 방법입니다. 어쨌든 잘못된 사용자를 리다이렉션하려는 경우, 'login.php? login_error = 1'로 보내지 않고 get_redirection_target을 처리 할 때 왜 처리할까요? ? – hashchange

+0

건배 메이트, 기본적으로 내 목표는 유효한 DOMAIN이 유효한 사용자 이름으로 모두 승인되기 전에 텍스트를 포함시키는 것입니다. 잘못된 사용자 이름을 리디렉션하지 않습니다. 그것에는 두 부분이 있습니다. 리다이렉션이 자리하고 있습니다. 도메인 이름 부분의 유효성 확인은 현재 문제가있는 부분입니다. 이런 식으로 할 수 없다면, 나는 그것을 턱에 가져갈 것입니다. 나는 당신이 [a-z] [A-Z] @ domain.com과 같은 사용자 이름 배열에 정규 표현식을 넣고 그 일을 할 수 있었으면 좋겠습니다. 지금까지 당신의 노력에 감사드립니다! – James