跨越PHP文件,但方法不会回显变量

时间:2017-02-08 09:47:39

标签: php

这是一项大学任务,所以我知道你不会这样做,因为它太容易入侵了。我在这个网站上搜索得太厉害了,我走到了尽头。所以这里是我有多个PHP文件,我试图拉过变量并打印出硬编码的用户信息。但是我在函数中的最后一次登录会抛出一个错误,无论我改变了什么,我都会遇到不同的错误。这主要是用PHP完成的,并且避免使用SESSION。任何帮助或方向表示赞赏。 我的索引表格很简单。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sample</title>
</head>
<body>
<style>
</style>
<form action="method.php"; method="POST">
Name<input type="text" name="userName">
Password<input type="text" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>

我的method.php也很直接并且有效。

<?php # person

$name1= $_POST['userName'];
$password=$_POST['password'];
//echo 'testclass.php#' . $name;

include 'testclass.php';

?>

但是我的方法凭证在这方面不起作用,它找不到名字1或密码?

<?php

class UserController {
//private $name1;
//private $password;
private $isLoggedIn = false;

// Credentials
public function credentials() {
    $credentials = array(
        array(
            "username" => "Goat",
            "password" => "1234"
        ),
        array(
            "username" => "Goat",
            "password" => "1234"
        )
    );
    return $credentials;
}

// Basic login
public function login() {
    foreach ($this->credentials() as $credential) {
        //not finding name 1 and password even though its been called in this php file.
        if ($this->name1 == $credential['username'] && $this->password == $credential['password']) {
            $this->isLoggedIn = true;
        }
    }
if ($isLoggedIn = true){
    echo "YAYAYA";
}
else{
    echo "sad";
}
}
}
$user = new UserController();
$user->credentials();
$user->login();

?>

非常感谢。

1 个答案:

答案 0 :(得分:-1)

$this->name1不作为类属性存在。您无法在UserController中访问那些变量。您需要将参数传递给登录函数。

public function login($username, $password) 
{
    foreach ($this->credentials() as $credential) 
    {
        if ($username == $credential['username'] && $password == $credential['password']) {
            $this->isLoggedIn = true;
        }
    }
}

$user = new UserController();
$user->login($_POST['username'], $_POST['password']);