I am totally new to PHP and mySQL and I built a small login form for my website. As the descriptions on the internet for such a thing are much more extensive, I just want to ask you if this is a secure way to do it, as it was just a few lines and it works:
First I create a table in phpMyAdmin with username and password (hashed with md5). After that I run the login on the website with the following script, where the $_POST stuff comes from a form.
<?php
session_start();
$db = @mysqli_connect("...", "...", "...") or
die("Connection failed!");
mysqli_select_db($db,'...');
if(isset($_POST['username']))
{
$_SESSION['user'] = $_POST['username'];
$_SESSION['password'] = md5($_POST['password']);
$user = $_SESSION['user'];
$password = $_SESSION['password'];
$sql = "SELECT * FROM logins WHERE username = \"$user\"";
$result = $db->query($sql);
$row = $result->fetch_assoc();
if($row["password"] == $password)
{
$_SESSION['logged'] = "loggedin";
}
}
?>
The Logout Script is very easy as well.
<?php
session_start();
session_destroy();
unset($_SESSION['user']);
unset($_SESSION['password']);
header('Location: ../index.php');
?>
Moreover I restrict every private content with
<?php
if (isset($_SESSION['logged']))
{
$temp = $_SESSION["user"];
echo "Hello $temp, nice to see you!";
}
?>
or I make a redirection.
So here are my questions:
- Is this a secure way to do it? Can It be hacked easily?
- What sense does md5 make if a reverse lookup is possible?
Thank You!