simple authentication using credentials in php
NickName:getthis Ask DateTime:2011-09-18T13:01:54

simple authentication using credentials in php

I need to write and authenticate user based on username, password and some other credentials like some of their user information name, mail etc. or some other thing. How can we do that in php?

My login code with username and password. how to add some credentials also with this program for authentication:

<?php

//Start session
session_start();

//Include database connection details
require_once('config.php');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if (!$db) {
    die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str)
{
    $str = @trim($str);
    if (get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

//Sanitize the POST values
$login = clean($_POST['login']);
$password = clean($_POST['password']);

//Input Validations
if ($login == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}
if ($password == '') {
    $errmsg_arr[] = 'Password missing';
    $errflag = true;
}

//If there are input validations, redirect back to the login form
if ($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: login-form.php");
    exit();
}

//Create query
$qry = "SELECT * FROM members WHERE login='$login' AND passwd='" . md5($_POST['password']) . "'";
$result = mysql_query($qry);

//Check whether the query was successful or not
if ($result) {
    if (mysql_num_rows($result) == 1) {
        //Login Successful
        session_regenerate_id();
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
        $_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
        $_SESSION['SESS_LAST_NAME'] = $member['lastname'];
        session_write_close();
        header("location: member-index.php");
        exit();
    } else {
        //Login failed
        header("location: login-failed.php");
        exit();
    }
} else {
    die("Query failed");
}
?>

if that credentials in dynamic that will be good. How to do that?

Copyright Notice:Content Author:「getthis」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7459577/simple-authentication-using-credentials-in-php

More about “simple authentication using credentials in php” related questions

simple authentication using credentials in php

I need to write and authenticate user based on username, password and some other credentials like some of their user information name, mail etc. or some other thing. How can we do that in php? My ...

Show Detail

HttpWebRequest not passing Credentials simple form authentication

My code is like this Doc doc = new Doc(); string url = HttpContext.Current.Request.Url.AbsoluteUri; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // required for HttpWebResponse.

Show Detail

Ember simple auth with custom server authentication (credentials undefined)

I am newbie with Ember and trying to implement a basic auth (username + password in the 'authentication' header) with a custom server using ember simple auth and ember-cli. The problem is that the

Show Detail

HTTP authentication using PHP authentication

I'm making a members area for my site and I have it already running. I authenticate members using mysql and php and this works really well. But now problems occurs, I would like to offer members a

Show Detail

Can I force sqlsrv_connect (PHP) to use windows authentication with supplied credentials?

Setup: intranet SQL Server and Domain Controller. In the DMZ we have a web server (IIS, PHP7). SQL Server has mixed mode authentication. I'm trying to migrate from an old API setup (PHP5, mssql_co...

Show Detail

How can HttpClientBuilder retrieve credentials for proxy authentication?

I have developed a Java Applet that works properly in a standard environment (no proxies). On the contrary if the applet is running on a client located behind a proxy, it correctly downloads files ...

Show Detail

SSO in PHP application using AD credentials independent of Web Server

I have a PHP application running with Nginx on a Linux server and it has a successful integration with my Active Directory using LDAP. In the current scenario, the user is able to create a new log...

Show Detail

Authentication on a simple PHP RESTful API service

I have a developed a very simple PHP API based on the PHP Slim Framework. It does nothing more then doing some SQL queries and returning an JSON object whenever I call it. I am accessing this API on

Show Detail

Save Simple Http Authentication Credentials For Later Use

I'm trying something about simple http authentication and i made a login page and i'm using this javascript function to request a protected page from server and i'm receiving http 200 and i success...

Show Detail

ASP.NET MVC authentication and credentials

I'm currently working on a simple application in ASP.NET MVC 5. I'm beginner in this. The assignment is about authentication from providers(Facebook, Google, Microsoft) and displaying user credenti...

Show Detail