Newish to PHP so still in the learning stages. I have been asked to help on a website that was built years ago, and as such the DB is still using MD5, which I need to sort I’ve written some code, which mostly works fine just stuck on the last part. In summary this is what I’m trying to do
User logs in ( so sql gets info from DB) Check password from this result & if starts with XYZ go to this page, if not go to other page. Code is below, being done on local serve till I get working
<!DOCTYPE html>
<html>
<body>
<?php
function begnWith($str, $begnString) {
$len = strlen($begnString);
return (substr($str, 0, $len) ===
$beg string);
}
// if(begnWith($text,"$2y"))
//header("Location:
http://www.bbc.co.uk");
// else
//echo "Old MD5";
?>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "login";
// Create connection
$conn = new mysqli($servername,
$username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
$sql = "SELECT * FROM users
WHERE username='davsas';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result-
>fetch_assoc()) {
// echo "<br> id: ". $row["id"]. " -
Name: ". $row["username"]. " " .
$row["password"] . "<br>";
$datas[] = $row;
}
}
foreach ($datas[0] as $data) {
echo $data['password'];
}
if(begnWith($data['password'],"$2y"))
header("Location:
http://www.bbc.co.uk");
else
echo "Old MD5";
//foreach ($datas[0] as $data) {
//echo $data;
//}
//foreach ($datas as $data) {
// echo $data['password'];
//}
$conn->close();
?>
</body>
</html>