So after 6 years learning to code PHP I finally finished writing my website (Not Online Yet) and I read a post about MYSQL_* functions being depreciated and having security problems so I have decided to learn how to re-write it using the MYSQLi_* functions.
Although I have re-written the code using mysqli_ functions and is working as it should (Login & Sessions) I am unsure if this is secure against those nasty SQL injection scripts. (I think binding the value of a $_POST into the prepared statement is enough to stop SQL injecton?)
I would really appreciate any advise given...
Kind Regards.
include/mysqli_connection.php (Connection Script)
<?php
// Connection Details
$MySQLi_Server = 'localhost';
$MySQLi_Username = '#####';
$MySQLi_Password = '#####';
$MySQLi_Database = '#####';
// Connect To Server
$MySQLi_Connection = new PDO("mysql:host=$MySQLi_Server;dbname=$MySQLi_Database", $MySQLi_Username, $MySQLi_Password);
// Tables
$MySQLi_Users_Table = 'users';
?>
include/global.php (Included On Every User Restricted Page)
<?php
// Session (Start)
session_start();
// Check (Logged In)
if(!isset($_SESSION['authenticated'])){
// Session (Destroy)
session_destroy();
// Redirect (Failed)
die(header('Location: ../login.php'));
}
// Session (Regenerate ID)
session_regenerate_id(TRUE);
// Include MySQLi Connection
include_once('mysqli_connection.php');
?>
login_process.php (Login Form Submits To This Script)
<?php
// Session (Start)
session_start();
// Session (Destroy)
session_destroy();
// Check Form Elements (Exists)
if((!isset($_POST['username'])) || !isset($_POST['password'])){
die('Ooops, Please Enter Your Username & Password!');
}
// Form Elements (Variables)
$Username = trim($_POST['username']);
$Password = trim(md5($_POST['password']));
// Include MySQLi Connection
include_once('include/mysqli_connection.php');
// Login (Query)
$LoginUser = $MySQLi_Connection->prepare("SELECT * FROM $MySQLi_Users_Table WHERE username=:username AND password=:password");
$LoginUser->bindParam(':username', $Username);
$LoginUser->bindParam(':password', $Password);
$LoginUser->execute();
$LoginUserResult = $LoginUser->fetch(PDO::FETCH_NUM);
if($LoginUserResult < 1){
// Redirect (Failed)
die(header('Location: index.php'));
} else {
// Session (Start)
session_start();
// Session (Data)
$_SESSION['authenticated'] = 'Y';
$_SESSION['username'] = $Username;
// Redirect (Successful)
die(header('Location: members_area/dashboard.php'));
}
?>
members_area/dashboard.php
<?php
// Include Global Settings
include('../include/global.php');
// Display Message (Logged In)
echo 'Username : ('.$_SESSION['username'].')';
?>