Reputation: 3
I am a new user and I have a simple question. How do I set a password for a user to log in to my site with PHP? I have no MySQL database just a iOS code/host app. I know this isn’t the most secure option, but only me and my friends are on the site so it doesnt matter. I tried the following:
$password = [
'john' => 'password1',
'paul' => 'password2',
'george' => 'password3',
'robert' => 'password4'
];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$pass = $password[ $_POST['username'] ];
if (password_verify($_POST['password'], $pass)) {
echo "Log in successful<br>";
}
else {
echo "Invalid log in<br>";
}
}
This is my HTML login form code for reference:
<body>
<center> <h1>Login</h1> </center>
<form>
<div class="container">
<label>Username:</label>
<input type="text" placeholder="Enter Username" name="" required>
<label>Password:</label>
<input type="password" placeholder="Enter Password" name="" required>
<button type="submit">Login</button>
<input type="checkbox" checked="checked"> Remember me
<button type="button" class="cancelbtn"> Cancel</button>
<a href="">Forgot password?</a>
</div>
</form>
</body>
</html>
But it didn’t work… So any help is appreciated.
Upvotes: 0
Views: 50
Reputation: 1424
I hope you are in best of health. There are lots of issues in your code. I am adding some points.
You can use the below code. I have tested it.
<?php
$password = [
'john' => 'password1',
'paul' => 'password2',
'george' => 'password3',
'robert' => 'password4'
];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$pass = @$password [ $_POST['username'] ];
if (!isset($pass))
{
echo "Invalid log in<br>";
}
else
{
if ($_POST['password'] == $pass)
{
echo "Log in successful<br>";
}
else
{
echo "Invalid log in<br>";
}
}
}
?>
<body>
<center> <h1>Login</h1> </center>
<form action="" method="POST">
<div class="container">
<label>Username:</label>
<input type="text" placeholder="Enter Username" name="username" required>
<label>Password:</label>
<input type="password" placeholder="Enter Password" name="password" required>
<button type="submit">Login</button>
<input type="checkbox" checked="checked"> Remember me
<button type="button" class="cancelbtn"> Cancel</button>
<a href="">Forgot password?</a>
</div>
</form>
</body>
</html>
Upvotes: 1