*** Authentica v1.0 ***
Tired of writing the code again and again for authenticating the user of your application? That too for each and every application you build?
If yes, then you are landed at the right place. Authentica allows you to reuse the library for authenticating user quickly and fetch the authenticated user's data when successfully authenticated.
For more such applications & Help:-
- Github :- https://github.com/suraj2334 *
- Website :- http://suraj-mundalik.info *
- Login system supporting encrypted passwords
- Fetch the authenticated data if authentication is successful
- Support for MD5, SHA1 and PlainText password
-
Authenticate(AUTHENTICATION_DETAILS)
This method takes authentication details in an associate array form. If authentication succeeds this method will return an associate array comprising of all the attributes in the table matching the user credentials. Read the following for more detailed view.
-
getAuthenticationStatus()
Returns true if authentication was successful else false.
-
Include the authentica.php in your script
-
Create a Connection Identifier using:
$con = mysql_connect(HOST, USER, PASSWORD) -
Create an Object of type Authentica with following parameters (in the specified order) to the constructor:
- Connection Identifier (From Step 2, $con)
- Database Name (Where your authentication table resides)
- Table Name (For example, users which holds user authentication details (Username, Password, etc..)
-
Create an Associate Array of authentication details
$auth_data = array( "Username" => "suraj",
"Password" => "suraj",
"PASSWORD_ENCRYPTED" => "md5"
);
Where,
"Username" is the Column Name "suraj" is the value to be verified,
"Password" is the Column Name "suraj" is the value to be verified,
"PASSWORD_ENCRYPTED" specifies whether the Password is PlainText or encrpyted using either md5 or sha1
Note: PASSWORD_ENCRYPTED must be specified blank if none of md5 or sha1 is used ("PASSWORD_ENCRYPTED" => "") -
Authenticate the user credentials by calling the Authenticate() method by passing the $auth_data as a parameter, created in Step 4
-
If successful, the method will return the columns and its values specific to the given credentials in the form of an associate array or else empty on failure.
Database : test
Table : users
First_Name | Last_Name | Mobile | Username | Password |
---|---|---|---|---|
Mohan | Shinde | 7896541230 | mohan | e9206237def4b4ef46fd933ed0f5a08f |
Suraj | Mundalik | 9876543210 | suraj | 4dd49f4f84e4d6945e3bc6d14812004e |
Tushar | Jachak | 8796541230 | tushar | df7c905d9ffebe7cda405cf1c82a3add |
<?php
require('authentica.php');
$con = @mysql_connect("localhost", "root", "");
$auth = new Authentica($con, "test", "users");
$auth_data = array( "Username" => "suraj",
"Password" => "suraj",
"PASSWORD_ENCRYPTED" => "md5"
);
$login_data = $auth->Authenticate($auth_data);
if(empty($login_data)){
echo "Login Failed.";
}
else{
echo "Login Successful.";
}
?>
Above code will output Login Successful., as Username = "suraj" & Password = "suraj" are valid entries in the users table. Note that we have stored the passwords using md5 so we have specified the PASSWORD_ENCRYPTED attribute as md5 in the $auth_data to encrypt the password "suraj" to md5 while verifying. Output will be:
Login Successful.
<?php
require('authentica.php');
$con = @mysql_connect("localhost", "root", "");
$auth = new Authentica($con, "test", "users");
$auth_data = array( "Username" => "suraj",
"Password" => "suraj",
"PASSWORD_ENCRYPTED" => "md5"
);
$login_data = $auth->Authenticate($auth_data);
if(empty($login_data)){
echo "Login Failed";
}
else{
echo "Login Successful.<br><br>";
foreach($login_data as $attrib => $value){
echo $attrib." = ".$value."<br>";
}
}
?>
Same as previous, just we are accessing the data returned by Authenticate() method. Output will be:
Login Successful.
First_Name = Suraj
Last_Name = Mundalik
Mobile = 9876543210
Username = suraj
Password = 4dd49f4f84e4d6945e3bc6d14812004e
<?php
require('authentica.php');
$con = @mysql_connect("localhost", "root", "");
$auth = new Authentica($con, "test", "users");
$auth_data = array( "Username" => "spiderman",
"Password" => "marvels",
"PASSWORD_ENCRYPTED" => "md5"
);
$login_data = $auth->Authenticate($auth_data);
if($auth->getAuthenticationStatus()){
echo "Login Successful.";
}
else{
echo "Login Failed";
}
?>
You can simply check whether authentication is successful or not using the getAuthenticationStatus() method returns true on success else false. Here it will return false as there is no user with spiderman as Username and marvels as Password in the users table. So output will be:
Login Failed
The library provides sufficient error handling for invalid parameters passed to its methods. If the method fails it will return the error Invalid or Empty ***.
Where *** can be:-
- CONNECTION_IDENTIFIER (If $con is not a valid connection identifier)
- DATABASE_NAME (If Database_Name parameter is empty or invalid)
- TABLE_NAME (If Table_Name parameter is empty or invalid)