Welcome Guest, Not a member yet? Register   Sign In
Personal library and Undefined property $input
#1

[eluser]luca89pe[/eluser]
Hi, i'm new in codeigniter (i started last week using this really nice framework for a simplce project).
First of all, thank you for this usefull framework!
Then my problem is:
i want to make a simple authentication script (i don't need many users, only 1 user that can be add news and somethings, so i thought code a simple library that i can use in every page i need my authentication system.
I created my Login.php with this code:
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login{
    
    private $user = "username";
    private $pass = "password";

    public function login() {
        if (!isset($_SERVER['PHP_AUTH_USER'])) {
            header("WWW-Authenticate: Basic realm=\"Effettua il Login!\"");
            Header("HTTP/1.0 401 Unauthorized");
            exit;
        }
#Verifichiamo se il Login è corretto...
        else if (($this->input->server('PHP_AUTH_USER') == "prova") && (hash("sha256", $this->input->server('PHP_AUTH_PW')) == "prova"))
            return 1;
#Se il Login non è corretto...
        else
            return 0;
    }

}

?>
Now in the controller where i need authentication, i wrote this (for debugging):
Code:
function index() {
        if($this->login->login())
            echo "OK!";
        else
            echo "KO!";
    }
but it doesn't work: when i call the page, authentication dialog appears, i compile it with user and psw, and then i get this error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Login::$input

Filename: libraries/Login.php

Line Number: 18
i saw that it appears in controllers when u don't set the parent::construct() (because input isn't initialized), but how is for libraries?

Thank you and sorry for my bad english!
#2

[eluser]DarkManX[/eluser]
you login library (class) doesnt extends from anything and it has the properties you coded. you need to refer to the CI object to use other library like input.

Code:
$CI =& get_instance();
$CI->input->post('foo');
#3

[eluser]luca89pe[/eluser]
Oh, i'm so thanks to you, now it works!! Smile


Luca
#4

[eluser]DarkManX[/eluser]
you should save the instance of the ci-object to your library property in the controctor, so you dont have to copy&paste; for each method. something like that:

Code:
function __construct(){
$this->CI =& get_instance();
}
#5

[eluser]luca89pe[/eluser]
Thank you to DarkManX too!
I wrote this:
Code:
function __construct() {
        $this->CI = & get_instance();
    }
and if i write this:
Code:
if (!isset($_SERVER['PHP_AUTH_USER'])) {
            header("WWW-Authenticate: Basic realm=\"Effettua il Login!\"");
            Header("HTTP/1.0 401 Unauthorized");
            exit;
        }
#Verifichiamo se il Login è corretto...
        else if (($this->CI->input->server('PHP_AUTH_USER') == $user) && (hash("sha256", $this->CI->input->server('PHP_AUTH_PW')) == $pass))
            return 1;
#Se il Login non è corretto...
        else
            return 0;
    }
but if i try changing this:
Code:
if (!isset($_SERVER['PHP_AUTH_USER'])) {
with this:
Code:
if (!isset($this->CI->input->server['PHP_AUTH_USER'])) {
it shows the authentication dialog in loop!
#6

[eluser]CroNiX[/eluser]
because the input method returns boolean FALSE if it doesn't exist....so it will always be set making your isset() check useless.

Try
Code:
if ($this->CI->input->server('PHP_AUTH_USER') !== FALSE)

also input->server() has (), not [] as it's not an array. It's a method returning a value.
#7

[eluser]luca89pe[/eluser]
i had to leave this project some times ago, but now i can work on this again!
So, this is my code now:

Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

function __construct() {
        $this->CI = & get_instance();
    }

class Login{

    public function login() {
        if ($this->CI->input->server('PHP_AUTH_USER') != FALSE){
            header("WWW-Authenticate: Basic realm=\"Effettua il Login!\"");
            Header("HTTP/1.0 401 Unauthorized");
            exit;
        }
#Verifichiamo se il Login è corretto...
        else if (($this->input->server('PHP_AUTH_USER') == "prova") && (hash("sha256", $this->input->server('PHP_AUTH_PW')) == "prova"))
            echo "OK!";
#Se il Login non è corretto...
        else
            echo "KO!";
    }

}

?>
But it still not work, this is the error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Login::$CI

Filename: libraries/Login.php

Line Number: 13

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Login.php

Line Number: 13


Fatal error: Call to a member function server() on a non-object in /home/mhd-01/**********************/codeIgniter/application/libraries/Login.php on line 13
#8

[eluser]InsiteFX[/eluser]
Code:
class your_library {

    protected $CI;

    public function __contruct()
    {
        $this->CI =& get_instance();
    }
}
#9

[eluser]luca89pe[/eluser]
Ty for your reply, didn't noticed i wrote that out the class declaration!!!!!!!!
But still not work Sad

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Login::$input

Filename: libraries/Login.php

Line Number: 22


Fatal error: Call to a member function server() on a non-object in /home/mhd-01/************************/codeIgniter/application/libraries/Login.php on line 22
i think this is because it doesn't show the dialog, so it doesn't get the input!
Why it doesn't show dialog Sad
#10

[eluser]CroNiX[/eluser]
It's because in places you are still checking for $this->input->post when it should be $this->CI->input->post (in your elseif). The error messages are pretty clear. It's the exact same problem as your original.




Theme © iAndrew 2016 - Forum software by © MyBB