Skip to main content
added 1256 characters in body
Source Link
Rohan Kumar
  • 40.7k
  • 11
  • 81
  • 111
  1. Use self::$data instead of $data. Read How to access private variable in static function
  2. Loop Condition to count($this->dataself::$data)
  3. Undefined index issue for password, as Indexing starts from 0
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 

And make your private variable $data to be static like,

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

Full code,

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. Use self::$data instead of $data. Read How to access private variable in static function
  2. Loop Condition to count($this->data)
  3. Undefined index issue for password, as Indexing starts from 0
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 
  1. Use self::$data instead of $data. Read How to access private variable in static function
  2. Loop Condition to count(self::$data)
  3. Undefined index issue for password, as Indexing starts from 0
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 

And make your private variable $data to be static like,

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

Full code,

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 
Source Link
Rohan Kumar
  • 40.7k
  • 11
  • 81
  • 111

You have several issues in your code like,

  1. Use self::$data instead of $data. Read How to access private variable in static function
  2. Loop Condition to count($this->data)
  3. Undefined index issue for password, as Indexing starts from 0

So, try the below code

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