I'm creating a back-end website for our school thesis and my main concern/problem is that I want the users to be able to log-in first on the login.php before they can go to the index page.
The problem is, people can just go to "sitename/index.php" and open the page even without logging-in first.
Here's the code: [logincheck.php]
<?php
session_start();
try {
$db = new PDO('mysql:host=localhost;dbname=login', "root", "");
} catch (PDOException $e) {
echo $e->getMessage();
}
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM `user` WHERE `uid` = :uid AND `pwd` = :pwd";
$statement = $db->prepare($sql);
$userData = [
'uid'=>$uid,
'pwd'=>$pwd
];
$statement->execute($userData);
if($statement->rowCount() > 0){
$SESSION['uid'] = $_POST['uid'];
header('Location: indextemplate.php');
}
else {
header('Location: login.php');
}
?>
and for my [login.php]:
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=login', "root", "");
} catch (PDOException $e) {
echo $e->getMessage();
}
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM `user` WHERE `uid` = :uid AND `pwd` = :pwd";
$statement = $db->prepare($sql);
$userData = [
'uid'=>$uid,
'pwd'=>$pwd
];
$statement->execute($userData);
if($statement->rowCount() > 0){
header('Location: index.php');
exit();
}
elseif(empty($uid&$pwd)){
header('Location: login.php?error=empty1');
exit();
}
elseif ($uid!=$idvariable&$pwd!=$idvarible){
header('Location: login.php?error=empty2');
exit();
}
?>
If I include the logincheck.php
on my index.php
it's just stuck on the log-in page even if I type in the correct username and password.