I am trying to make a simple user - login system. However, I have everything with sessions, and stuff working but the user login is not working with the database. I can input any username and password and login.
Here is my login page:
<?php
session_start();
$db_host = "localhost"; // Place the database host here
$db_username = "data_user"; // Place the username for the MySQL database here
$db_pass = "password"; // Place the password for the MySQL database here
$db_name = "database_name"; // Place the name for the MySQL database here
if (isset($_POST['username']))
{
// MySQL Connection
$db_link = mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql. Make sure you have correctly inputed your host, username, and password.");
mysql_select_db("$db_name") or die ("no database could be found.");
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
// MySQL Query
$result = mysql_query("SELECT * FROM users WHERE
username = '$username' AND password = '$password' ");
if(!$result) {
$_SESSION['error'] = '<span style="color:red">Login Failed</span>';
} else
{
$row = mysql_fetch_assoc($result);
$_SESSION['userid'] = $row['id'];
$_SESSION['username'] = $username;
}
mysql_close($db_link);
}
header('Location: ./')
?>
I am using Bootstraps login. So this is my login html:
<div id="login" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Login</h3>
</div>
<div class="modal-body">
<form class="form-horizontal" action="login.php" method="post">
<div class="control-group">
<label class="control-label" for="inputUsername">Username</label>
<div class="controls">
<input name="username" type="text" id="inputUsername" placeholder="Username">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input name="password" type="password" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
</div>
</div>
It is the very basic login using bootstrap. I got the log out working fine so thats good, however anyways can anyone explain to me why it is not connected to the database and you can login with any username and password that you can imagine?