Condividi:        

[PHP] Proteggere pagine con login

Problemi di HTML? Di PHP, ASP, .NET, JSP, Perl, SQL, JavaScript, Visual Basic..?
Vuoi realizzare programmi in C, C++, Java, Ruby o Smalltalk, e non sai da che parte cominciare?
Entra qui e troverai le risposte!

Moderatori: Anthony47, Triumph Of Steel, archimede

[PHP] Proteggere pagine con login

Postdi nykky » 07/07/06 16:58

Ciao, volevo proteggere alcune pagine del mio sito web aziendale interno con uno script PHP trovato su HTML.IT. Però non riesco ad effettuare la login, e dopo tre tentativi mi propone la pagina d'errore. Il sito gira su Apache 2.0.54 e PHP 4.4.0
Questo il codice di index.php3
Codice: Seleziona tutto
<?
include "BasicAuthenticator.php";

$realm = "My Secret Pages";
$errorMessage = "Private. Keep Out!";

$auth = new BasicAuthenticator($realm, $errorMessage);

// Define a set of valid usernames and passwords.
$auth->addUser("test", "test");
$auth->addUser("user", "pass");

// And now perform the authentication
$auth->authenticate();?>
<HTML>
<body>
Prova
</body>
</HTML>

Questo il codice di BasicAuthenticator.php
Codice: Seleziona tutto
<?

// PHP BasicAuthenticator Class Version 1.3 (24th March 2001)
// 
// Copyright David Wilkinson 2001. All Rights reserved.
//
// This software may be used, modified and distributed freely
// providing this copyright notice remains intact at the head
// of the file.
//
// This software is freeware. The author accepts no liability for
// any loss or damages whatsoever incurred directly or indirectly
// from the use of this script. The author of this software makes
// no claims as to its fitness for any purpose whatsoever. If you
// wish to use this software you should first satisfy yourself that
// it meets your requirements.
//
// URL:   http://www.cascade.org.uk/software/php/auth/
// Email: davidw@cascade.org.uk

class BasicAuthenticator
{
    var $realm = "<private>";
    var $message;
    var $authenticated = -1;
    var $users;
   
    function BasicAuthenticator($realm, $message = "Access Denied")
    {
        $this->realm = $realm;
        $this->message = $message;
    }
         
   
    function authenticate()
    {
        if ($this->isAuthenticated() == 0)
        {
            Header("HTTP/1.0 401 Unauthorized");
            Header("WWW-Authenticate: Basic realm=\"$this->realm\"");
            echo $this->message;
            exit();
        }
        else
        {
            Header("HTTP/1.0 200 OK");
        }
    }
   
   
    function addUser($user, $passwd)
    {
        $this->users[$user] = $passwd;
    }
   
   
    function isAuthenticated()
    {
        global $PHP_AUTH_USER;
        global $PHP_AUTH_PW;

       if ($this->authenticated < 0)
       {
           if(isset($PHP_AUTH_USER))
           {
               $this->authenticated = $this->validate($PHP_AUTH_USER, $PHP_AUTH_PW);
           }
           else
           {
               $this->authenticated = 0;
            }
        }
       
        return $this->authenticated;
    }
   
   
    function validate($user, $passwd)
    {
        if (strlen(trim($user)) > 0 && strlen(trim($passwd)) > 0)
        {
            // Both $user and $password are non-zero length
            if (isset($this->users[$user]) && $this->users[$user] == $passwd)
            {
                return 1;
            }
        }
        return 0;
    }
}
?>

Mi potete aiutare?
Grazie
nykky
Moderatore
 
Post: 1933
Iscritto il: 23/07/03 10:42

Sponsor
 

Postdi Triumph Of Steel » 07/07/06 17:33

Prova a cambiare nella classe:
Codice: Seleziona tutto
if(isset($PHP_AUTH_USER))

con
Codice: Seleziona tutto
if(isset($_SERVER['PHP_AUTH_USER']))

e
Codice: Seleziona tutto
if(isset($PHP_AUTH_PW))

con
Codice: Seleziona tutto
if(isset($_SERVER['PHP_AUTH_PW']))
Avatar utente
Triumph Of Steel
Moderatore
 
Post: 7852
Iscritto il: 22/08/01 01:00

Postdi nykky » 10/07/06 08:12

Grazie, ora provo ;)
nykky
Moderatore
 
Post: 1933
Iscritto il: 23/07/03 10:42

Postdi nykky » 10/07/06 10:40

Triumph Of Steel ha scritto:Prova a cambiare nella classe:
Codice: Seleziona tutto
if(isset($PHP_AUTH_USER))

con
Codice: Seleziona tutto
if(isset($_SERVER['PHP_AUTH_USER']))


ok, questo l'ho cambiato

Triumph Of Steel ha scritto:e
Codice: Seleziona tutto
if(isset($PHP_AUTH_PW))

con
Codice: Seleziona tutto
if(isset($_SERVER['PHP_AUTH_PW']))

questo però non lo trovo :cry:
nykky
Moderatore
 
Post: 1933
Iscritto il: 23/07/03 10:42

Postdi Triumph Of Steel » 10/07/06 12:22

hai ragione :P
allora fai così

Codice: Seleziona tutto
if(isset($_SERVER['PHP_AUTH_USER']))
           {
               $this->authenticated = $this->validate($PHP_AUTH_USER, $PHP_AUTH_PW);
           }


diventa
Codice: Seleziona tutto
if(isset($_SERVER['PHP_AUTH_USER']))
           {
               $this->authenticated = $this->validate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
           }
Avatar utente
Triumph Of Steel
Moderatore
 
Post: 7852
Iscritto il: 22/08/01 01:00

Postdi nykky » 10/07/06 13:22

Funziona :D
Mille grazie :D
nykky
Moderatore
 
Post: 1933
Iscritto il: 23/07/03 10:42


Torna a Programmazione


Topic correlati a "[PHP] Proteggere pagine con login":

consumo pagine web
Autore: nikita75
Forum: Software Windows
Risposte: 4

Chi c’è in linea

Visitano il forum: Nessuno e 10 ospiti