Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to be able to set a global username like <anythinghere>@domain3.com as a username value in the $usernames array (in code below). This is so that I can then go and redirect users based on domain, having already been "authenticated".

I will put example in code below.

Can i do something like $usernames = array("[email protected]", $X) where $X = <anything-so-long-as-not-blank>@domain3.com?

Full Code Below:

<?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 Has already helped me like a champ. I am wondering if any one can get me over the line? Cheers!

share|improve this question
    
There seems to be something odd with your logic. You want to supply a username with wildcards, fair enough - but what do you want to match it against? –  hashchange Oct 29 '12 at 7:38
    
So I want a username that can have ANY text before @domain.com to MATCH to. The section that redirects usernames then can redirect a valid username. Not send the address to the error page. So basically I am validating a domain. But it has to be a value in that $username array or I have to rewrite everything! (Which I'm not allowed to do) –  James Oct 29 '12 at 8:09

1 Answer 1

up vote 0 down vote accepted

Your code needed a clean-up first. There's a bunch of errors in it if you do a test run. It's also a bit hard to read IMO.

I've attached a working code sample below.

// 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
    "/[^@]+@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(
    "[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",
    "/[^@]+@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;

}
share|improve this answer
    
Thanks Michael, that is definitely much better laid out code. My problem is that even with this setup I am unable to type in any prefix before @domain.com and be redirected. Because I am still not a valid user. We have not defined the ability to validate any prefix at domain.com. Redirect, yes, but because not a valid user, we get thrown to an error page. At least that is as I have read it? Please correct me if I am wrong! Cheers. –  James Oct 29 '12 at 12:24
    
Yes, the code above redirects valid users to the target defined in the array. Invalid logins get sent off to the error page. That's how I read your code and your question ... Anyway, if you want to redirect invalid users as well, why don't you just call get_redirection_target when handling them, instead of sending them to 'login.php?login_error=1'? –  hashchange Oct 29 '12 at 12:35
    
Cheers mate, basically my goal is to make any text included before a valid DOMAIN be accepted ON WHOLE as a valid username. Not to redirect invalid usernames. There are two parts to it, the redirection is spot on, the validating a domain name part is where I am now having trouble. If it cannot be done this way, I will take that on the chin. I just wish you could put some regex in the username array like [a-z][A-Z]@domain.com and have that work... Thank you for your efforts thus far! –  James Oct 29 '12 at 12:45
    
Slowly getting there ;) So you want any username to be legit if it matches the pattern "/whatever pattern you come up with/@domain.com". Right? Do these folks need to supply a password? Do they all have the same password? –  hashchange Oct 29 '12 at 12:56
    
Awesome we are on the same page! Haha. Yes, they all have the same password PER DOMAIN. They do not supply password. Although, I guess you could say they supply the username prefix to the domain. –  James Oct 29 '12 at 13:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.