PHP:控制器不工作

时间:2017-05-25 05:03:57

标签: php laravel

这是我刚刚制作的控制器,用于检查输入密码。它似乎没有错误。但是,当我运行它时,它没有收到结果(echo"正确的密码")虽然我输入了正确的输入。     

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MainController extends Controller
{
private $data = array(
    array("TestUser","Password"),
    array("Admin","BigA1r")
);
//$this->log($u,$p);    
public static function log($username,$pass) { //login
    echo "<title>Processing request...</title>";
    echo "Logging in... Please wait.";
    for ($i = 1;$i == 35;$i++){
            $u = $data[$i][1];
            if ($u == $username){
                $p = $data[$i][2];
                if ($p == $pass){
                    setcookie("username="+$u);//+";password="+p;
                    echo "Right password";
                }else{echo "Wrong password";};
            }else{
                echo "Can't find this username. Please try a different 
name.";
            }       
    }   
}

这里的错误是什么?我无法找到它。并没有抛出任何东西。它只是停留在回声&#34;登录...请等待。&#34 ;;

2 个答案:

答案 0 :(得分:2)

您的代码中有几个问题,例如

  1. 使用self::$data代替$data。阅读How to access private variable in static function
  2. 循环条件为count(self::$data)
  3. 密码的未定义索引问题,因为索引从0开始
  4. 所以,试试下面的代码

    for ($i = 0,$l=count(self::$data);$i < $l;$i++){
       $u = self::$data[$i][0]; // use 0 not 1 for username, as index start from 0
       if ($u == $username){
           $p = self::$data[$i][1]; // use 1 not 2, as index start from 0
           if ($p == $pass){
              setcookie("username="+$u);//+";password="+p;
              echo "Right password";
           } else{echo "Wrong password";};
       } else{
           echo "Can't find this username. Please try a different name.";
       }       
    } //end for
    

    并将您的私有变量$data设为static

    private static $data = array(
        array("TestUser","Password"),
        array("Admin","BigA1r")
    );
    

    完整代码,

    class MainController extends Controller {
        private static $data = array(
            array("TestUser","Password"),
            array("Admin","BigA1r")
        );
        //$this->log($u,$p);    
        public static function log($username,$pass) { //login
            echo "<title>Processing request...</title>";
            echo "Logging in... Please wait.";
            for ($i = 0,$l=count(self::$data);$i < $l;$i++){
                $u = self::$data[$i][0];
                if ($u == $username){
                    $p = self::$data[$i][1];
                    if ($p == $pass){
                        setcookie("username="+$u);//+";password="+p;
                        echo "Right password";
                    } else {
                        echo "Wrong password";
                    }
                } else {
                    echo "Can't find this username. Please try a different name.";
                }
            } // end for loop  
        } // end function log
    } // end class MainController
    

答案 1 :(得分:0)

尝试使用:

self::$data[$i][1] and self::$data[$i][2]

要从静态方法访问类的私有变量,您需要使用self关键字。

此外,在循环中你有

for ($i = 1;$i == 35;$i++)

这可能会失败,因为条件是$ i == 35而你的开头是$ i = 1,这是永远不会满足的,应该是$ i&lt; = 35。