如何从其他文件访问PHP变量?

时间:2014-08-14 05:36:20

标签: php variables

所以我想从文件$username访问变量(quickprotect.login.php)并将其打印在文件index.php中。文件login.ini.php包含用户名配置,quickprotect.login.php包含:

<?php
$root = dirname(__FILE__);

class quickprotect {
    private $ini_file_location = "login.ini.php";
    private static $tries;
    public static $errormsg;

    public $settings;

    public function __construct() {
        session_start();
        $this->ini_file_location = $GLOBALS[root] . "/" . $this->ini_file_location;
        $this->settings = @parse_ini_file($this->ini_file_location);
        if (!$this->settings) {
            die("Failed to load the INI file located at: $this->ini_file_location.");
        }
    }

    public function login($username, $password) {

        // Performs login by initializing $_SESSION vars
        // Returns true or false, plus $_SESSION message on success/failiure
        // Contains brute force protection

        if (!isset($_SESSION['tries'])) $_SESSION['tries'] = 1;
        if ($_SESSION['tries'] <= intval($this->settings[MAX_TRIES]) || $this->settings[ALLOW_UNLIMITED_TRIES] == true) {
            //Try to prevent brute forcing
            if (sha1(trim($password)) == trim($this->settings[ADMIN_PW]) && $username === $this->settings[USERNAME]) {
                // other codes
            }
        }
    }
}

当我使用时:

include 'quickprotect.login.php';
echo "User: $username";

它给出了:

User:  

任何人都可以提供建议吗? Thx提前:))

更新:通过以下方式解决:

include '../quickprotect.class.php'
$qp = new quickprotect();
$un=   $qp->settings['USERNAME']; 
mail("mail@domain.com","Report","=== Username: $un");

2 个答案:

答案 0 :(得分:0)

如果您在其他页面上使用表单,则可以使用php $ _POST

答案 1 :(得分:-1)

你确定你写过这个吗?

echo "User: $username";

因为它应该是

echo "User: ".$username;

获取变量的实际值。