在php类中无效参数[关闭]

在php类中无效参数[关闭]

问题描述:

i was trying to create i login using php classes i have checked every thing also other answers from this site but it shows me warning "Invalid argument supplied for foreach" here is my code of login form and class please check and help

<?php
session_start();

if(isset($_POST['login']))
{

    include('/class.login.php');
    $login= new Login();

    if($login->isLoggedIn())
        echo "yepeee";
    else
        $login->showErrors();
}


$token= $_SESSION['token']= md5(uniqid(rand(),true));

?>

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
username <input type="text" name="username">
<br>
pass<input type="password" name="password">
<input type="hidden" name="token" value="<?=$_SESSION['token'];?>">
<input type="submit" name="login">
</form>

class code

<?php

class Login
{
    private $_id;
    private $_username;
    private $_password;
    private $_passmd5;

    private $_errors;
    private $_access;
    private $_login;
    private $_token;


    function _construct()
    {
        $this->_errors= array();
        $this->_login= isset($_POST['login'])? 1 : 0;
        $this->_access=0;
        $this->_token=$_POST['token'];

        $this->_id=0;
        $this->_username=($this->_login)? $this->filter($_POST['username']): $_SESSION['username'];
        $this->_password=($this->_login)? $this->filter($_POST['password']): '';
        $this->_password=($this->_login)? md5($this->_password): $_SESSION['password'];

    }

    function isLoggedIn()
    {
        ($this->_login)? $this->verifyPost() : $this->verifySession();
        return $this->_access;
    }

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

    function verifyPost()
    {
        try
        {
            if(!$this->isTokenValid())
                throw new Exception("Invalid Token");
            if(!$this->isDataValid())
                throw new Exception("invalid from data");
            if(!$this->verifyDatabase())
                throw new Exception("invalid username pass");

        $this->_access=1;
        $this->registerSession();

        }
        catch(Exception $e)
        {
            $this->_errors[]= $e->getMessage();
        }
    }

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

    function verifyDatabase()
    {
        mysql_connect("localhost", "root", "")or die("cannot connect to server"); 
        mysql_select_db("test")or die("cannot select DB");

        $data=mysql_query("select id from user 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; }
    }

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

    function isTokenValid()
    {
        return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 :1;
    }

    function registerSession()
    {
        $_SESSION['ID']=$this->_id;
        $_SESSION['username']=$this->_username;
        $_SESSION['password']=$this->_passmd5;

    }

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

    function showErrors()
    {
     echo "Error";
     foreach($this->_errors as $key=>$value)
        echo $value."<br>";
    }
}


?>

Please help

function _construct()

Two underscores for magic methods. This should be __construct().

The reason this results in the error you are seeing is that your constructor is not being run when you call new Login() and thus $this->_errors is never properly initialized to array(). Then when your foreach($this->_errors ... ) loop runs it is trying to iterate over null and you get that error.

For foreach to perform, $this->_errors should be an array.

the Try to change

foreach($this->_errors as $key=>$value)
        echo $value."<br>";
    }

To

foreach($this->_errors[] as $key=>$value)
        echo $value."<br>";
    }