This topic is locked

Allow user to login using "username" AND/OR "username@gmai

9/2/2010 10:14:05
PHPRunner Tips and Tricks
F
FunkDaddy author

I created an application that allows users to register for a service where they get assigned an email address as part of their registration/account setup. I wanted to allow the user to login to the application by using their newly created email address or simply by using the username that was used to generate that email address).
For example:

  1. User chooses to register as "johnsmith"
  2. Email account is automatically created by my application called "johnsmith@myapp.com"
  3. I want to allow user to login to my web app by typing either "johnsmith" and/or "johnsmith@myapp.com" (just like Gmail allows you to do).
    Here is the code to add to the "Before Login" event of the Login page:


//Allow user to login whether he/she inputs username as email or simply using all characters to left of @ symbol in the email

//Ex: Username stored in database is "johnsmith", but you also want to allow user to login as "johnsmith@myapp.com" if he/she chooses to do so.
$temp_value = substr($username, 0, stripos($username, "@") ); //Strip username entered in login of all characters to the right of @ symbol (including the @ symbol itself).
$sql="select User_Password, User_Login, UserID, LinkGroupID from users_tbl where User_Login ='".$temp_value."' AND User_Password = '".$password."'"; //Extract info from database that matches that username

$rs=CustomQuery($sql);

$data=db_fetch_array($rs);
//Use IF statement to compare the data that was extracted from database (which would only have a result if username and password matched)

if ($data["User_Password"]==$password)

{

$_SESSION["UserID"] = $data["User_Login"]; //Set the $_SESSION variables to allow user to remain logged into the application

$_SESSION["GroupID"] = $data["LinkGroupID"];

header("Location: menu.php"); //redirect user beyond the login page if they successfully login!

}


Credit for pointing me in the right direction should go to Ann from support who provided me with some code that inspired this remodeld version I've just posted here.

F
FunkDaddy author 9/27/2010

I ran into an issue with this code I posted, so I am adding an updated version here for folks interested in using it. I simply added a few more lines of code to the existing lines... the reason was that I wanted the username in login to be converted to all lowercase after user entered it, and also if they didn't enter a username with an @ symbol after the name the $temp_value in my code wasn't finding anything at all. Anyhow, here is the new code (Before Login event):



//Allow user to login whether he/she inputs username as email or simply using all characters to left of @ symbol in the email

//Ex: Username stored in database is "johnsmith", but you also want to allow user to login as "johnsmith@gmail.com" if he/she chooses to do so.

$username = strtolower($username);

$temp_value = substr($username, 0, stripos($username, "@") ); //Strip username entered in login of all characters to the right of @ symbol (including the @ symbol itself).

if ($temp_value == null) {$temp_value = $username;};
$sql="select User_Password, User_Login, UserID, LinkGroupID from users_tbl where User_Login ='".$temp_value."' AND User_Password = '".$password."'"; //Extract info from database that matches that username

$rs=CustomQuery($sql);

$data=db_fetch_array($rs);
//Use IF statement to compare the data that was extracted from database (which would only have a result if username and password matched)

if ($data["User_Password"]==$password)

{

$_SESSION["UserID"] = $data["User_Login"]; //Set the $_SESSION variables to allow user to remain logged into the application

$_SESSION["GroupID"] = $data["LinkGroupID"];

$_SESSION["LinkUserID"] = $data["UserID"];

$_SESSION["Account_Name"] = $data["Account_Name"];

header("Location: catalog_entries_tbl_list.php"); //redirect user beyond the login page if they successfully login! XXX

}
return true;