In this Post we will learn Login and Registration Example in PHP and PostgreSQL.
Step1 -> Create registration page register.php.
$host = "localhost"; $port = "5432"; $dbname = "test"; $user = "postgres"; $password = "postgres"; $connection_string = "host={$host} port={$port} dbname={$dbname} user={$user} password={$password} "; $dbconn = pg_connect($connection_string); if(isset($_POST['submit'])&&!empty($_POST['submit'])){ $sql = "insert into public.user(name,email,password,mobno)values('".$_POST['name']."','".$_POST['email']."','".md5($_POST['pwd'])."','".$_POST['mobno']."')"; $ret = pg_query($dbconn, $sql); if($ret){ echo "Data saved Successfully"; }else{ echo "Soething Went Wrong"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>PHP PostgreSQL Registration & Login Example </title> <meta name="keywords" content="PHP,PostgreSQL,Insert,Login"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Register Here </h2> <form method="post"> <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" id="name" placeholder="Enter name" name="name" requuired> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control" id="email" placeholder="Enter email" name="email"> </div> <div class="form-group"> <label for="pwd">Mobile No:</label> <input type="number" class="form-control" maxlength="10" id="mobileno" placeholder="Enter Mobile Number" name="mobno"> </div> <div class="form-group"> <label for="pwd">Password:</label> <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd"> </div> <input type="submit" name="submit" class="btn btn-primary" value="Submit"> </form> </div> </body> </html>
Step2 -> Create login page login.php
$host = "localhost"; $port = "5432"; $dbname = "test"; $user = "postgres"; $password = "postgres"; $connection_string = "host={$host} port={$port} dbname={$dbname} user={$user} password={$password} "; $dbconn = pg_connect($connection_string); if(isset($_POST['submit'])&&!empty($_POST['submit'])){ $hashpassword = md5($_POST['pwd']); $sql ="select *from public.user where email = '".pg_escape_string($_POST['email'])."' and password ='".$hashpassword."'"; $data = pg_query($dbconn,$sql); $login_check = pg_num_rows($data); if($login_check > 0){ echo "Login Successfully"; }else{ echo "Invalid Details"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>PHP PostgreSQL Registration & Login Example </title> <meta name="keywords" content="PHP,PostgreSQL,Insert,Login"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Login Here </h2> <form method="post"> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control" id="email" placeholder="Enter email" name="email"> </div> <div class="form-group"> <label for="pwd">Password:</label> <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd"> </div> <input type="submit" name="submit" class="btn btn-primary" value="Submit"> </form> </div> </body> </html>
Step 3 -> Create Database test and following table.
CREATE TABLE public."user" ( id serial, name character varying(250), email character varying(250), password character varying(250), mobno bigint, CONSTRAINT id PRIMARY KEY (id) ) WITH ( OIDS = FALSE )