i trying to create a little project to allow only registered users to login to another page in site , i have 3 PHP page :
Register page login page home page
Register PHP page
<?php
if(isset($_POST['reg'])){
$name = $_POST['name'];
$age = $_POST['age'];
$address = $_POST['address'];
$password = md5($_POST['password']);
$con = new PDO("mysql:host=localhost;dbname=newschool","admin","admin");
$affected = $con->exec("insert into users (name,age,address,password) values('$name',$age,'$address','$password') ");
if($affected > 0){
echo "Your data has been added successfully";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form action="#" method="post">
<div class="form-group">
<label for="name">Username:</label>
<input type="text" id="name" name="name" class="form-control">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="number" id="age" name="age" class="form-control">
</div>
<div class="form-group">
<label for="address">Address:</label>
<textarea id="address" name="address" class="form-control"></textarea>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" class="form-control">
</div>
<button name="reg" class="btn btn-primary btn-block">Register</button>
</form>
</div>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
and login page :
<?php
session_start();
if(isset($_POST['b_login'])){
$username =$_POST['username'];
$password =$_POST['password'];
try{
$db = new PDO('mysql:host=localhost;dbname=newschool','admin','admin');
$stm = $db->prepare(" select * from users where name= ? and password = ? ");
$stm->execute([$username,$password]);
if(empty($_SESSION['userinfo']))
{
header("Location:Login.php");
}
else
{
header("Location:home.php");
}
}catch (PDOException $ex){
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form action="#" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" class="form-control">
</div>
<button name="b_login" class="btn btn-primary">Login</button>
</form>
</div>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
i store data in "users" table within MySQL database by name "newschool"
i can register user in database and i can retrieve data by code :
<?php
try{
$con = new PDO("mysql:host=localhost;dbname=newschool","admin","admin");
$users = $con->query("select * from users");
}catch(PDOException $e){
echo "try again";
}
?>
the problem is when i enter username and password in login page it to forward user to home page !!
Please note that I'm completely new to PHP so if possible a little explanation.
