【问题标题】:php : Warning: Invalid argument supplied for foreach()php : 警告:为 foreach() 提供的参数无效
【发布时间】:2012-05-11 13:14:38
【问题描述】:

我只是想不通.. 但是当我抛出异常消息时,我感觉到问题就在那里。我在注册课程中得到了几乎相同的代码。通常只给错误数组提供消息,例如 $this->errors[] = "some error"。

<?php


class class_login 
{
    private $id;
    private $username;
    private $password;
    private $passmd5;

    private $errors;
    private $access;
    private $login;
    private $ltoken;

    public function __cunstruct()
    {
        $this->errors = array();

        $this->login  = isset($_POST['login'])? 1:0;
        $this->access = 0;
        $this->ltoken  = $_POST['ltoken'];
        $this->id     = 0;
        $this->username = ($this->login)? $this->filter($_POST['lusername']) : $_SESSION['username'];
        $this->password = ($this->login)? $this->filter($_POST['lpassword']) : '';
        $this->passmd5 = ($this->login)? md5($this->password) : $_SESSION['password'];

    }

    public function isLoggedIn()
    {
        ($this->login)? $this->verifyPost() : $this->verifySession();

        return $this->access;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9]/','',$var);
    }

    public function verifyPost()
    {
        try
        {
            if(!$this->tokenValid())
                throw new Exception('Invalid Form Submission!');
            if(!$this->isDataValid())
                throw new Exception('Ivalid Form Data!');
            if(!$this->verifyDatabase())
                throw new Exception('Invalid Username/Password!');

            $this->access = 1;
            $this->registerSession();
        }
        catch(Exception $e)
        {
            $this->errors[] = $e->getMessage();
        }
    }

    public function verifySession()
    {
        if($this->sessionExist() && $this->verifyDatabase())
        $this->access = 1;
    }

    public function verifyDatabase()
    {
        include('db_connect.php');

        $data = mysql_query("SELECT ID FROM users WHERE username = '($this->username)' AND password = '($this->passmd5)'");

        if (mysql_num_rows($data))
        {
            list($this->id) = @array_values(mysql_fetch_assoc($data));

            return true;
        }
        else
            return false;

       }

    public function isDataValid()
    {
        return (preg_match('/[^a-zA-Z0-9](5,12)$/',  $this->username) && preg_match('/[^a-zA-Z0-9](5,12)$/',  $this->password))? 1:0;
    }

    public function tokenValid()
    {
        return (!isset($_SESSION['ltoken']) || $this->ltoken != $_SESSION['ltoken'])? 0 : 1;
    }

    public function registerSession()
    {
        $_SESSION['ID']       = $this->id;
        $_SESSION['username'] = $this->username;
        $_SESSION['password'] = $this->passmd5;
    }

    public function sessionExist()
    {
        return (isset($_SESSION['username']) && isset($_SESSION['password']))? 1 : 0;
    }

    public function show_errors()
    {
        foreach($this->errors as $key=>$value)
            echo $value."</br>";
    }


}

?>

【问题讨论】:

  • 它应该是 ->construct 而不是 __cunstruct(),如果它正确,你使用的是哪个 PHP 版本?
  • 它是__cunstruct(),这不是问题。使用 5.3.8。谢谢。
  • nvm 我明白你的意思了,谢谢
  • 你在哪里看到它是__cunstruct

标签: php foreach


【解决方案1】:

构造函数被称为__construct,而不是__cunstruct

【讨论】:

    【解决方案2】:

    我看到您在 __cunstruct 函数中将 $this->errors 设置为数组,但由于它不是 __construct,它可能永远不会被设置。

    【讨论】:

      【解决方案3】:

      你需要一个关联数组

      foreach($this->errors as $key=>$value)
      

      但你没有人。 ($this->errors[] = $e->getMessage();)

      如果没有关联数组,您必须使用:

      foreach($this->errors as $value)
      

      【讨论】:

      • 您仍然可以将 $key => $value 与非关联数组一起使用(在 PHP 中,所有数组都是真正关联的)。 $key 将只是隐式索引而不是显式键。
      【解决方案4】:
       public function __cunstruct() <------ The error is probably here. It is __construct
          {
              $this->errors = array();
      
              $this->login  = isset($_POST['login'])? 1:0;
              $this->access = 0;
              $this->ltoken  = $_POST['ltoken'];
              $this->id     = 0;
              $this->username = ($this->login)? $this->filter($_POST['lusername']) : $_SESSION['username'];
              $this->password = ($this->login)? $this->filter($_POST['lpassword']) : '';
              $this->passmd5 = ($this->login)? md5($this->password) : $_SESSION['password'];
      
          }
      

      你打错了.... _construct 而不是 _construct

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 1970-01-01
        相关资源
        最近更新 更多