I am trying to build a basic website that can access a mysql database. When I was hosting it on my local wamp server, everything worked perfectly, but now that I have posted it online, I am having an issue. When visiting the log in page on a browser for the first time (or in incognito mode) the user must log in twice before being permitted access.
I was able to find someone dealing with the same issue (Problems with PHP, MySQL based log-in system), but after changing the URL the form posts to (using "loginchecker.php" as well as the whole URL including http://.), I was still encountering the problem. The first set of code is my index.php file and the second is the page that verifies the user's log in credentials. Also just as a sidenote, this is mostly a learning experience for me, so I'm not concerned about security loopholes for now. Thanks in advance for any help!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Log In</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link href="css/styles.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"> </script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"> </script>
<![endif]-->
</head>
<body>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
<div id="wrapper">
<h1>Login</h1>
<br />
<form id="form" action="loginchecker.php" method="post" enctype="multipart/form- data">
Username: <input type="text" name="username" /> <br /><br />
Password: <input type="password" name="password" /> <br /><br />
<input type="submit" value="Login" name="Submit" />
</form>
</body>
</html>
And the second page:
<?php
session_start();
if (isset($_POST['username'])) {
include_once("dbconnect.php");
$usname = strip_tags($_POST["username"]);
$paswd = strip_tags($_POST["password"]);
$usname = mysqli_real_escape_string($dbCon, $usname);
$paswd = mysqli_real_escape_string($dbCon, $paswd);
$sql = "SELECT id, username, password FROM members WHERE username = '$usname' AND activated = '1' LIMIT 1";
$query = mysqli_query($dbCon, $sql);
$row = mysqli_fetch_row($query);
$uid = $row[0];
$dbUsname = $row[1];
$dbPassword = $row[2];
// Check if the username and the password they entered were correct
if ($usname == $dbUsname && $paswd == $dbPassword) {
// Set session variables
$_SESSION['username'] = $usname;
$_SESSION['id'] = $uid;
date_default_timezone_set('America/New_York');
$date = date("Y/m/d");
$time = date("H:i:s");
$in = 'in';
// Log the user's successful log in to the database
$toLog = "INSERT INTO accesslog (UserID, Username, Date, Time) VALUES ('". $_SESSION['id'] ."', '". $_SESSION['username'] ."', '". $date ."', '". $time. "')";
mysqli_query($dbCon, $toLog);
// Now direct to users feed
echo '<meta http-equiv="refresh" content="0.2; URL=user.php" />';
header("Location: user.php");
} else {
echo "Oops that username or password combination was incorrect. <br /> Please try again.";
}
}
?>