Hi, I'm building a basic log in system just to learn the ropes of PHP. Currently I'm a bit stuck. I'm trying to get a connection through a function file sorted for the rest of it to work but it's proving far more complicated then I thought. This is my best attempt. Login.php.
<?php
$conn = new dbmember();
$conn->openDB();
//$con=mysqli_connect("x","x","x","x"); redundant as I want to do this through a class!
$user=$_POST['user'];
$password=$_POST['password'];
if(isset($_POST['submit'])){
//To ensure that none of the fields are blank when submitting the form if
if(isset($_POST['user']) && isset($_POST['password']))
{
$user = stripslashes($user);
$password = stripslashes($password);
L98 $user = $this->mysqli_real_escape_string($conn, $user);
$password = $this->mysqli_real_escape_string($conn, $password);
//SQL Injection Ahoy! I know...but future versions aim to be robust!
$sql="SELECT * FROM users WHERE username='{$user}' AND password='{$password}' LIMIT 1;";
$result=mysqli_query($con->conn, $sql);
$row=mysqli_fetch_array($result);
if($row[0]==1)
{
session_start();
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
$_SESSION['loggedin'] = "true";
header("location:index.php");
}
else
{
print ('<div id="error">Acess denied, wrong username or password?</div>');
}
}
else
{
print ('<div id="error">Enter something!</div>');
}
}
?>
functions.php
require("assets/configs/db_config.php");
class dbmember()
$var conn
function openDB() {
//1. Create a database connection
$this->conn = mysqli_connect("localhost" , "login", "pass","db_db");
if (!$this->conn)
{
$this->error_msg = "connection error could not connect to the database:! ";
}
return $this;
}
Fatal error: Using $this when not in object context in C:\xampp\htdocs\c\login.php on line 98
Please advise