I am working on a new website, and I rewrote some of my previously working code for the new one to recreate the login system. Unfortunately, the rewritten version does not work so let me explain what's going on.
I have "index.php", which holds the "titlemenu.php" that is used on every single page. I intend to run the login box in this title menu (top bar) so people can log in despite what page their own, and when they successfully log in, it refreshes the page they are currently on so they're logged in.
So far, bingo - It works. They're logged in and view members only pages. However, the title bar is a different story. It still shows "LOG IN" and "REGISTER", despite being logged in. Immediatelly, I though this was a problem with that specific php file, so I logged into that php only, bypassing index. When I try to sign in on that blank page only, I get: Fatal Error: Call to a non-member function prepare() on line 18 I know this usually means your variable is not declared or not being ran, so I decided to troubleshoot it. I did a var_dump on every variable I used, which returned valid results. I checked index.php, which has no errors and runs fine, and shows users as logged in. "Welcome Back, RhapidFyre!" is what it says on the index, but the login box shows that I am not logged in yet.
loginheader.php
<?include "641a/database.php";?><div id="login"><?
$mydbid = $_SESSION['user'];
$myquery = "SELECT * FROM logins WHERE dbid = $mydbid";
$myrow = mysqli_fetch_assoc($myquery);
$myname = $myrow['nickname'];
if($_GET['do'] == "logout") {
unset($_SESSION['user']);
?><script type="text/javascript">
window.location.replace("index.php");
</script><?
}
if($_GET['do'] == 'login') {
$query = 'SELECT * FROM logins WHERE username = :username';
$query_params = array(':username' => $_POST['username']);
var_dump($query);
var_dump($query_params);
try
{
$stmt = $db->prepare($query); //LINE 18
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
?><script type="text/javascript">
window.location.replace("redirect.php?error=username");
</script><?
}(Further irrelevant code follows)
database.php
<? session_start();
$dbusername = "********";
$dbpassword = "********";
$dbhost = "localhost";
$dbname = "********";
$link = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbname);
// PDO Initialization
try {
$db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbusername, $dbpassword);
}
// If connection fails, die.
catch(PDOException $ex){
die("Failed to connect to the MySQL Server!" . $ex->getMessage());
}
Snippet of index.php
<html>
<head>
<title>My Website Template</title>
<link rel="stylesheet" type="text/css" href="base.css"/>
</head>
<body>
<div id="header">
<?include "pages/titles.php";?>
<?include "pages/loginheader.php";?>
</div>