I'm make a login/register pdo php script, and im wondering if i can get any help about transfer it to Prepared statments
I've already tried to make the Prepared statments but has alot of issues i am new to pdo and php so if i can get some help it would be nice
<?php
require_once 'connection.php';
session_start();
if(isset($_SESSION["user_login"])) //check condition user login not direct back to index.php page
{
header("location: index.php");
}
// REGISTERR
if(isset($_REQUEST['btn_register'])) //button name "btn_register"
{
$username = strip_tags($_REQUEST['txt_username']); //textbox name "txt_email"
$email = strip_tags($_REQUEST['txt_email']); //textbox name "txt_email"
$password = strip_tags($_REQUEST['txt_password']); //textbox name "txt_password"
$ip_address = $_SERVER['REMOTE_ADDR'];
if(empty($username)){
$errorMsg[]="Please enter username"; //check username textbox not empty
}
else if(empty($email)){
$errorMsg[]="Please enter email"; //check email textbox not empty
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errorMsg[]="Please enter a valid email address"; //check proper email format
}
else if(empty($password)){
$errorMsg[]="Please enter password"; //check passowrd textbox not empty
}
else if(strlen($password) < 6){
$errorMsg[] = "Password must be atleast 6 characters"; //check passowrd must be 6 characters
}
else
{
try
{
$select_stmt=$db->prepare("SELECT username, email FROM customers
WHERE username=:uname OR email=:uemail"); // sql select query
$select_stmt->execute(array(':uname'=>$username, ':uemail'=>$email)); //execute query
$row=$select_stmt->fetch(PDO::FETCH_ASSOC);
if($row["username"] ==$username){
$errorMsg[]="Sorry username already exists"; //check condition username already exists
}
else if($row["email"] ==$email){
$errorMsg[]="Sorry email already exists"; //check condition email already exists
}
else if(!isset($errorMsg)) //check no "$errorMsg" show then continue
{
$new_password = password_hash($password, PASSWORD_DEFAULT); //encrypt password using password_hash()
$insert_stmt=$db->prepare("INSERT INTO customers (username,email,password) VALUES
(:uname,:uemail,:upassword)"); //sql insert query
if($insert_stmt->execute(array( ':uname' =>$username,
':uemail' =>$email,
':upassword'=>$new_password))){
$registerMsg="Register Successful..... Please Click On Login Account Link"; //execute query success message
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
if(isset($_REQUEST['btn_login'])) //button name is "btn_login"
{
$username =strip_tags($_REQUEST["txt_username_email"]); //textbox name "txt_username_email"
$email =strip_tags($_REQUEST["txt_username_email"]); //textbox name "txt_username_email"
$password =strip_tags($_REQUEST["txt_password"]); //textbox name "txt_password"
if(empty($username)){
$errorMsg[]="please enter username or email"; //check "username/email" textbox not empty
}
else if(empty($email)){
$errorMsg[]="please enter username or email"; //check "username/email" textbox not empty
}
else if(empty($password)){
$errorMsg[]="please enter password"; //check "passowrd" textbox not empty
}
else
{
try
{
$select_stmt=$db->prepare("SELECT * FROM customers WHERE username=:uname OR email=:uemail"); //sql select query
$select_stmt->execute(array(':uname'=>$username, ':uemail'=>$email)); //execute query with bind parameter
$row=$select_stmt->fetch(PDO::FETCH_ASSOC);
if($select_stmt->rowCount() > 0) //check condition database record greater zero after continue
{
if($username ==$row["username"] OR $email ==$row["email"]) //check condition user taypable "username or email" are both match from database "username or email" after continue
{
if(password_verify($password, $row["password"])) //check condition user taypable "password" are match from database "password" using password_verify() after continue
{
$_SESSION["user_name"] = $row["username"];
$_SESSION["user_login"] = $row["id"]; //session name is "user_login"
$loginMsg = "Successful Login..."; //user login success message
header("refresh:2; index.php"); //refresh 2 second after redirect to "index.php" page
}
else
{
$errorMsg[]="Wrong password";
}
}
else
{
$errorMsg[]="Wrong username or email";
}
}
else
{
$errorMsg[]="Wrong username or email";
}
}
catch(PDOException $e)
{
$e->getMessage();
}
}
}
?>
I would love to get the code to be fully Prevent Sqli Injection, cause im new and i think i have some sqli injection possible on the code.