-1

我试图创建我使用 php 类登录我已经检查了所有内容以及该站点的其他答案,但它向我显示警告“为 foreach 提供的参数无效”这是我的登录表单和类代码,请检查并帮助

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

班级代码

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


?>

请帮忙

4

2 回答 2

4

function _construct()

魔术方法的两个下划线。这应该是__construct()

这导致您看到的错误的原因是您的构造函数在您调用时没有运行new Login(),因此$this->_errors从未正确初始化为array(). 然后,当您的foreach($this->_errors ... )循环运行时,它会尝试迭代null并且您会收到该错误。

于 2013-04-10T04:00:53.340 回答
-2

foreach 要执行,$this->_errors应该是一个数组。

尝试改变

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

foreach($this->_errors[] as $key=>$value)
        echo $value."<br>";
    }
于 2013-04-10T04:00:42.863 回答