Page 1 of 1

Another example of authenticating against a database [PHP]

Posted: Mon Nov 28, 2022 9:32 am
by kimboslice
Almost forgot to post this up, heres another example of authenticating against a database, this time in PHP with Bcrypt passwords

config:

Code: Select all

auth-user-pass-verify 'C:/PROGRA~1/PHP/v8.1.12/php.exe C:/path/to/Auth.php' via-file
Auth.php:

Code: Select all

<?php
if (!isset($argv[1]))
	{
	print 'not ok';
	exit(1);
	}
	
$data = file($argv[1], FILE_IGNORE_NEW_LINES);
// Add your [address], [dbuser], [pass], [database]
$conn = new mysqli("127.0.0.1", "root", "password", "openvpn");
if ($conn->connect_error) // maybe remove this when you have the script setup
{
    die("Connection failed: " . $conn->connect_error);
}
// Create  db named openvpn and a table named users
// Then add two columns, one named password and another named user
// populate them with [username], [bcrypt-hashed-pass]
$stmt = $conn->prepare("SELECT password FROM users WHERE user = ?");
$stmt->bind_param("s", $data[0]);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
if (!$result) { echo 'not ok'; exit(1); } // User non-existent
if (password_verify($data[1], $result['password']))
{
	echo 'ok';
	exit(0);
}
else
{
	echo 'not ok';
	exit(1);
}
?>

Re: Another example of authenticating against a database [PHP]

Posted: Tue Dec 06, 2022 3:07 am
by ramprasad.padala
Not a relevant post, sorry