I have two page login.php and user.php(where the message = Wrong Combination which i one to echo his) am trying to display a message in login.php which is from user.php that is base on wrong combination of email and password please how can I display the message in my login.php below is my code
The user.php page below
class Users {
function login($find='') {
if(($rows["email"] == $email)&&($rows["password"] == $password)){
$_SESSION['email'] = $email;
$_SESSION['password'] = $password;
if (isset($_POST['remember'])){
setcookie("cookname", $_SESSION['email'], time()+60*60*24*100, "/");
setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
}
$_SESSION['user_id'] = $user_id;
return true;
}
else {
$message = 'Wrong Combination';
return false;
}
}
}
The login.php page below
<?php
include_once("user.php");
if (isset($_POST['submit']))
{
$find = $user->login($_POST);
if ($find)
{
header ("location: panel.php");
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<form name="form1" method="post" action="">
<!-- I want to echo it here I echo the variable message but did not work -->
<?php echo "$message";?>
input id="email" />
<input id="password" />
</form>
</body>
</html>
Your $message
variable has local scope within the login function of the Users class, so you can't use it outside of that function as written currently. You could save it into your session, ie:
$_SESSION['message'] = 'Wrong Combination';
or better yet make it a member variable of the Users class:
class Users {
protected message = '';
public function getMessage(){ return $this->message; }
function login($find='') {
if(($rows["email"] == $email)&&($rows["password"] == $password)){
$_SESSION['email'] = $email;
$_SESSION['password'] = $password;
if(isset($_POST['remember'])){
setcookie("cookname", $_SESSION['email'], time()+60*60*24*100, "/");
setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
}
$_SESSION['user_id'] = $user_id;
return true;
}else{
$this->message = 'Wrong Combination';
return false;
}
}
}
Then inside the login form where you want to display the message:
<?php
$message = $user->getMessage();
if (!empty($message))
echo $message;
?>
By the way, I don't see where you are instantiating the Users class. I assume you are creating $user
in code not shown from your use of it in login.php.