2012-05-11 15 views
0

私はそれを理解できませんが、例外メッセージを投げたときに問題が周辺にあると感じました。私は登録クラスでほぼ同じコードを持っています。 $ this-> errors [] = "some error"のようなエラー配列をnormalyに渡すだけです。php:警告:foreach()に無効な引数が指定されました

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


} 

?> 
+2

を構築する - あなたが使用しているその正しいものをPHPのバージョンならば、)(__cunstructない構築>? – swapnesh

+0

それは問題ではない__cunstruct()です。 5.3.8を使用する。どうも。 –

+0

nvm私はあなたが意味するものを見て、 –

答えて

2

コンストラクタがない__cunstruct__constructと呼ばれています。

0

は、私はあなたの__cunstruct機能で、アレイにの$ this - >エラーを設定するが、それが設定されない場合があり__constructされていないので、されて参照してください。

0

あなたは

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

のための連想配列を必要とするしかし、あなたは誰を持っていません。

foreach($this->errors as $value) 
+0

$ key => $ valueを非連想配列で使用することはできます(PHPではすべての配列が実際に結合的です)。 $ keyは明示的なキーの代わりに暗黙的なインデックスになります。 – Brian

0
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']; 

    } 

あなたがタイプミスを持っている.... :あなたが使用する必要があります連想配列うちで

は、(の$ this - >エラー[] =は$ e-> getMessage()の) _ それがあるべきない _construct

関連する問題