Reputation: 33
I am trying to login as user but don't know getting this error but when I try second time I will have access,But after login same this,and after logged in on second try I can't fetch email from DB it seems it not creating session.Its just passing the code not creating sessions.
if (!isset($_SESSION['email']) && isset($_POST['email'])) {
if (!empty($_POST['email']) && !empty($_POST['password'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
$password=sha1($password);
try {
$dbh = new PDO("mysql:host=$hostname; dbname=$database", $username, $pass);
$dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$email = $_POST['email'];
$password = $_POST['password'];
$stmt = $dbh -> prepare("SELECT email, password FROM tbl_user WHERE email = '".$email."' and password = '".$password."'");
$stmt -> bindParam('".$email."', $email, PDO::PARAM_STR);
$stmt -> bindParam('".$password."', $password, PDO::PARAM_STR);
$stmt -> EXECUTE();
$em = $stmt -> fetchColumn();
if ($em == true) {
// session_register("email");
// session_register("password");
$_SESSION['email'] = $_POST['email'];
$_SESSION['START'] = time();
setcookie("username", $_POST['email'], mktime()+(60*3), "/");
setcookie("LUS", time(), mktime()+(60*3), "/");
$stmt -> $dbh -> prepare("SELECT Name FROM tbl_user WHERE email = '".$email."'");
$stmt -> EXECUTE();
$em2 = $stmt -> fetchColumn();
echo "Logged in.";
} else {
echo "email or password is incorrect.";
}
} catch (Exception $e) {
echo "".$e->getMessage();
}
} elseif (empty($_POST['email']) && !empty($_POST['password'])) {
# code...
echo "Error : Enter your E-mail.";
} elseif (!empty($_POST['email']) && empty($_POST['password'])) {
# code...
echo "Error: Enter your Password";
} else {
echo "Error: Enter your E-mail & Password";
}
}
elseif (isset($_SESSION['email'])) {
# code...
echo "Welcome again you still logged in <strong>" .round((time() . $_SESSION['START'])/60) ."</strong> minutes(s) ago <a href='logout.php'>LogOut</a>";
}
elseif (!isset($_SESSION['email'])) {
# code...
echo "You must loggin first.";
//header('location:../index.php');
}
Upvotes: 0
Views: 4741
Reputation: 1545
change second prepare statment usage
$stmt -> $dbh -> prepare("SELECT Name FROM tbl_user WHERE email = '".$email."'");
to
$stmt = $dbh -> prepare("SELECT Name FROM tbl_user WHERE email = '".$email."'");
Upvotes: 1