I'm trying hard to get a user based on his id or email after he's logged in trough a form. The function is defined in a User.class.php and i want to call it on another php file called profile.php, but it continues giving me syntax errors and I don't see how to fix it..
Errors: Deprecated: Non-static method User::getUserId() should not be called statically Notice: Undefined variable: email in
User class is made clean with getters and setters,functions
Below is Profile.php and user class code:
<?php
include_once("classes/User.class.php");
include_once("classes/db.class.php");
try {
$conn = Db::getInstance();
$user = User::getUserId($email);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP MySQL Query Data Demo</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="container">
<h1>Employees</h1>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Fullname</th>
<th>Username</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<div class="profile">
<h2><?php echo $user[0]['first_name'] ?> <?php echo $user[0]['last_name'] ?></h2>
<p class="profile__text"><?php echo $user[0]['avatar'] ?></p>
<p class="profile__text"><?php echo $user[0]['email'] ?></p>
<p class="profile__text">***********</p>
<p class="profile__text"><?php echo $user[0]['address'] ?></p>
</div>
</tbody>
</table>
</body>
</div>
</html>
include_once('Db.class.php');
class User {
private $email;
private $username;
private $fullname;
private $password;
public function getFullname()
{
return $this->fullname;
}
public function setFullname($fullname)
{
$this->fullname = $fullname;
if(empty ($fullname)){
throw new Exception("Please fill in your fullname");
}
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
if(empty ($username)){
throw new Exception("Please fill in your username");
}
}
public function setEmail($email)
{
$this->email = $email;
if(empty ($email)){
throw new Exception("Please fill in your E-mail adress");
}
}
public function getEmail()
{
return $this->email;
}
public function setPassword($password)
{
if(strlen($password) < 8){
throw new Exception("Password must be at least 8 characters long.");
}
//B-crypt the password
$hash = password_hash($password,PASSWORD_DEFAULT);// standaard 10 keer als je geen options mee geeft
$this->password = $hash;
return true;
}
public function getPassword()
{
return $this->password;
}
public function register(){
//connection
$conn = Db::getInstance();
//query (insert)
$statement = $conn->prepare("insert into users (email, username, fullname, password)
values(:email, :username, :fullname, :password)");
$statement->bindParam(':fullname',$this->fullname);
$statement->bindParam(':email',$this->email);
$statement->bindParam(':username',$this->username);
$statement->bindParam(':password',$this->password);
//execute
$result = $statement->execute();
//return true/false
return $result;
}
public function login() {
if(!isset($_SESSION['loggedin'])) {
//header('Location:login.php');
echo $feedback = "thanks for creating an account.";
}
}
// ------------------------------------ LOGIN
public function canILogin($email, $password) {
//session_start()
//already loggedin
if (isset($_SESSION['email'])) {
header('Location: index.php');
}
//connection
$conn = Db::getInstance();
//query
$statement = $conn->prepare("select * from users where email = :email");
$statement->bindParam(":email", $email);
//execute
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
if(password_verify($password, $result['password'])){
return true;
}
else{
throw new Exception('Ooopss something goes wrong... Try again!');
}
}
//checken of we zijn ingelogd
public static function checkLogin() {
if(!isset($_SESSION)) {
session_start();
}
if(!isset($_SESSION['username'])) {
//header("Location: login.php");
}
}
public function getUserId($email) {
$conn = Db::getInstance();
$statement = $conn->prepare("select * from users where email = '".$email."';");
$statement->execute();
$result = $statement->fetch();
$userId = $result['id'];
return $userId;
}
public function getAllFromUser($email) {
$conn = Db::getInstance();
$statement = $conn->prepare("select * from users where email = '".$email."';");
$statement->execute();
$result = $statement->fetch();
return $userId;
}
} // User class end