doujiena0025 2015-04-05 17:00
浏览 65

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

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>
  • 写回答

1条回答 默认 最新

  • douhuzhi0907 2015-04-05 17:45
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等