Php 从另一个页面的页面回显类中的字符串?

Php 从另一个页面的页面回显类中的字符串?,php,function,echo,inner-classes,Php,Function,Echo,Inner Classes,我有两个页面login.php和user.php(其中message=错误的组合,我要呼应他),我试图在login.php中显示一条来自user.php的消息,这是基于电子邮件和密码的错误组合。请如何在我的login中显示消息。下面是我的代码 下面是user.php页面 class Users { function login($find='') { if(($rows["email"] == $email)&&($rows["passwor

我有两个页面login.php和user.php(其中message=错误的组合,我要呼应他),我试图在login.php中显示一条来自user.php的消息,这是基于电子邮件和密码的错误组合。请如何在我的login中显示消息。下面是我的代码

下面是user.php页面

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;
        }
    }
}
<?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>
下面是login.php页面

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;
        }
    }
}
<?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>

输入id=“电子邮件”/>

您的
$message
变量在用户类的登录函数中具有局部作用域,因此您不能在当前编写的函数之外使用它。您可以将其保存到会话中,即:

$\u会话['message']=“组合错误”

或者最好将其作为Users类的成员变量:

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;
    }
  }
}
然后在要显示消息的登录表单中:

<?php 
  $message = $user->getMessage();
  if (!empty($message)) 
    echo $message;
?>


顺便说一下,我看不出您在哪里实例化Users类。我假设您正在login.php中使用的代码中创建
$user

在另一个主题中,将用户登录凭据存储在cookie中是一个安全漏洞。我建议通读整个owasp.org PHP安全备忘单,以获得一个很好的概述。以下是与“记住我”饼干相关的内容: