How to implement Authentication with PHP and MySQL

Determining New users
although the web is anonymous we will as good be in a position to check out and use the little information we can receive because the servers and the online browsers.Net browsers mainly identify itself to the server utilizing JavaScript.
Each and every laptop linked to the internet has a designated IP deal with accordingly from this IP expertise we are competent to infer slightly extra about the user like the region, the device they’re utilizing and likewise if the user comes again traveling the website online once more.
Am going to speak extra on PHP and SQL authentication, via we mean asking a user to prove their identity so that we are able to inform if either they’re authorized or no longer.

Enforcing controlled entry 
this code will explain the implementation of access control on an internet site, the consumer is requested to enter the data for selecting themselves to the approach, it queries the database and tests if the user login credentials are in the database if the method outcomes positive outcome of the user then they can proceed with the transaction but if now not the method returns an error to inform the user that he has no entry to the method.
Authentication.php-using PHP for Simple Authentication Mechanism



<?php
    $name=$_POST['name'];
    $password=$_POST['password'];
    
    if((!isset($name))|| (!isset($name))) {
        //Visitors need to enter a name and password
    }
    ?>
    <h1>Please Log in</h1>
    <p>Secret Page.</p>
    <form method="post" action="secretdb.php">
    <p>Username:<input type="text" name="name"></p>
    <p>Password:<input type="password" name="password"></p>
    <p><input type="submit" name="submit" value="Log In"></p>
    </form>
    
    <?php
    } else {
        //connect to mysql_affected_rows
        $mysql=mysqli_connect("localhost","debianlinx","debianlinx");
        if (!selected) {
            echo "cannot select the database";
            exit;
        }
        //query the database to see if there is a record which matches
        $query="select count(*) from authorized_users where
                name='".$name."' and 
                password='".$password."'";
                
            $result=mysqli_query($mysql,$query);
            
            if(!$result) {
                echo "query cannot be run";
                exit;
            }
            $row=mysqli_fetch_row($result).
            $count=$row(0);
            
            if ($count>0) {
                
                //the new user name and password are correct
                echo "<h1>welcome</h1>
                    <p>these is the secret page.</p>";
            }else {
                //new users credentials incorect!!
                echo "<h1><p>unfortunately you are denied access"</p></h1>;
                
            }
            }
            ?>

here is the databased used above

 

MySQL Queries to create the database and the sample data used

create database auth;
use auth;
create table authorized_users(name varchar(25),
                            password varchar(25),
                            primary key (name)
                            );
                            
//sample data
INSERT INTO AUTHORIZED_USERS VALUES('username','password');

INSERT INTO AUTHORIZED_USERS VALUES('testuser',sha1('password'));
grant selecton auth.*
                    to 'debianlinx'
                    identified by 'debianlinx';
    flush privileges;

Authentication PHP MySQL

 

Total
0
Shares
Leave a Reply

Your email address will not be published.

Previous Post

Error Handling and Exceptions in PHP

Next Post
linux

How to Calculate Dates in PHP

Related Posts