Visualizzazione dei risultati da 1 a 9 su 9
  1. #1

    problema di gestione variabili (newbie)

    Ciao a tutti.
    Vi premetto che sono una newbie (3 giorni di PHP ).

    Allora...
    Il mio file login.php deve ottenere username e password in un form e autenticare le variabili fornite con una funzione. Le variabili devono essere identiche a $HOSTUSER e $HOSTPWD che con in un file incluso (conn.inc.php) da login.php. Sfortunatamente, gli echo didebug nella funzione authenticated del file login.php mi stampa come stringhe vuole le variabili in cluse da conn.inc.php.

    Ecco il file conn.inc.php:
    <?php
    $HOSTNAME="mydb.mydomain";
    $HOSTDB="MYDB";

    $HOSTUSER="pippo";
    $HOSTPWD="CANTERINO";
    ?>

    ecco il file login.php:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <?php include("menu.php");
    include('conn.inc.php');
    ?>
    <title>Login</title>
    </head>
    <body>

    <?php echo '<form name="formname" action="login.php" method="post">';
    ?>
    <form>
    <p class="form">username :<input type="text" name="username"></p>

    <p class="form">password :<input type="password" name="password"></p>

    <input type="submit" value="invia">
    </form>

    <?php
    function authenticated($username, $password){

    // If either the username or the password are
    // not set, the user is not authenticated
    if (!isset($username) || !isset($password))
    return false;

    if ($username == '' && $password == ''){
    echo "Empty Fields!
    ";
    return false;
    }

    //debug
    echo "

    HOSTUSER: $HOSTUSER, </p>
    ";
    echo "

    HOSTPWD: $HOSTPWD, </p>
    ";
    echo "

    username: $username, </p>
    ";
    echo "

    password: $password, </p>
    ";

    if ((strcmp($HOSTUSER, $username) == 0)
    &&
    strcmp($HOSTPWD, $password) == 0) {
    echo "true!
    ";
    return true;
    }
    else
    return false;
    }

    if (!authenticated($_POST['username'],$_POST['password']))
    {
    // No credentials found - send an unauthorized
    // challenge response
    header("WWW-Authenticate: Basic realm=\"Flat Foot\"");
    header("HTTP/1.0 401 Unauthorized");

    // Set up the body of the response that is
    // displayed if the user cancels the challenge
    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head>
    <title>Web Database Applications</title>
    </head>
    <body>
    <h2>You need a username and password to
    access this service</h2>


    If you have lost or forgotten your
    password, tough!
    </body>
    </html>
    <?php

    exit;
    }

    // The response to authorized users
    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head>
    <title>Web Database Applications</title>
    </head>
    <body>
    <h2>Welcome!</h2>
    </body>
    </html>

    ----------------

    Quando mi connetto a login.php tramite Firefox, io metto la coppia username/pwd giusta (pippo+CANTERINO) ma ho l seguente output su login.php:

    HOSTUSER: ,

    HOSTPWD: ,

    username: pippo,

    password: CANTERINO,

    You need a username and password to access this service

    If you have lost or forgotten your password, tough!


    --------------------

    Dov'è l'errore? $HOSTUSER E $HOSTPWD sono globali e incluse nel file che contiene la funzione che le accede...bah... ho provato anche a metterle come globali direttamente in login.pho senza includere conn.inc.php

    ...

    help me...

  2. #2
    iniziamo col rendere piu semplice e chiaro il post....non si caposce bene ne quanti file sono ne l'inizio e la fine

    proa a ripostare il codice suddividendoli meglio del tipo
    1) file .php
    ...
    ....
    ...
    codice
    ...
    ...
    ..
    ..
    ###########################à
    2) file_2.php
    ...
    ....
    ...
    codice
    ...
    ...
    ..
    ..
    ###########################à
    etc.... poi ne riparliamo ok ;-)

  3. #3
    Originariamente inviato da nmkbeppe
    iniziamo col rendere piu semplice e chiaro il post....non si caposce bene ne quanti file sono ne l'inizio e la fine


    era abbastanza chiaro comunque lo ripeto...

    1)file conn.inc.php:
    <?php
    $HOSTNAME="mydb.mydomain";
    $HOSTDB="MYDB";

    $HOSTUSER="pippo";
    $HOSTPWD="CANTERINO";
    ?>

    2)file login.php:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <?php include("menu.php");
    include('conn.inc.php');
    ?>
    <title>Login</title>
    </head>
    <body>

    <?php echo '<form name="formname" action="login.php" method="post">';
    ?>
    <form>
    <p class="form">username :<input type="text" name="username"></p>

    <p class="form">password :<input type="password" name="password"></p>

    <input type="submit" value="invia">
    </form>

    <?php
    function authenticated($username, $password){

    // If either the username or the password are
    // not set, the user is not authenticated
    if (!isset($username) || !isset($password))
    return false;

    if ($username == '' && $password == ''){
    echo "Empty Fields!
    ";
    return false;
    }

    //debug
    echo "

    HOSTUSER: $HOSTUSER, </p>
    ";
    echo "

    HOSTPWD: $HOSTPWD, </p>
    ";
    echo "

    username: $username, </p>
    ";
    echo "

    password: $password, </p>
    ";

    if ((strcmp($HOSTUSER, $username) == 0)
    &&
    strcmp($HOSTPWD, $password) == 0) {
    echo "true!
    ";
    return true;
    }
    else
    return false;
    }

    if (!authenticated($_POST['username'],$_POST['password']))
    {
    // No credentials found - send an unauthorized
    // challenge response
    header("WWW-Authenticate: Basic realm=\"Flat Foot\"");
    header("HTTP/1.0 401 Unauthorized");

    // Set up the body of the response that is
    // displayed if the user cancels the challenge
    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head>
    <title>Web Database Applications</title>
    </head>
    <body>
    <h2>You need a username and password to
    access this service</h2>


    If you have lost or forgotten your
    password, tough!
    </body>
    </html>
    <?php

    exit;
    }

    // The response to authorized users
    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head>
    <title>Web Database Applications</title>
    </head>
    <body>
    <h2>Welcome!</h2>
    </body>
    </html>


    Ho tolto gli "ecco" e aggiunto al primo ecco il numero 1 e al secondo il numero 2, giuro.

    ciao, aiutooo

  4. #4
    ho fatto qualche annotazione...fammi sapere


    1)file conn.inc.php:
    <?php
    $HOSTNAME="mydb.mydomain";
    $HOSTDB="MYDB";

    $HOSTUSER="pippo";
    $HOSTPWD="CANTERINO";
    ?>

    2)file login.php:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <?php include("menu.php");
    include('conn.inc.php');
    ?>
    <title>Login</title>
    </head>
    <body>

    <?php echo '<form name="formname" action="login.php" method="post">';
    ?>

    #### mancano gli attributi value="" e il tag input non e chiuso
    <form>
    <p class="form">username :<input type="text" name="username"></p>

    <p class="form">password :<input type="password" name="password"></p>

    <input type="submit" value="invia">
    </form>

    <?php


    #####vonviene che questo sia un altro file e non lo stesso a mio dire....
    ####e cmq inizi ad usare le variabili prima di averle riecvute

    function authenticated($username, $password){


    if (!isset($username) || !isset($password))
    return false;

    if ($username == '' && $password == ''){
    echo "Empty Fields!
    ";
    return false;
    }

    echo "

    HOSTUSER: $HOSTUSER, </p>
    ";
    echo "

    HOSTPWD: $HOSTPWD, </p>
    ";
    echo "

    username: $username, </p>
    ";
    echo "

    password: $password, </p>
    ";

    if ((strcmp($HOSTUSER, $username) == 0)
    &&
    strcmp($HOSTPWD, $password) == 0) {
    echo "true!
    ";
    return true;
    }
    else
    ### qui manca l'apertura della graffa
    return false;
    }
    ### solo ora effettui il post
    if (!authenticated($_POST['username'],$_POST['password']))
    {

    header("WWW-Authenticate: Basic realm=\"Flat Foot\"");
    header("HTTP/1.0 401 Unauthorized");
    #### qui come mai riapri un altro documento?
    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head>
    <title>Web Database Applications</title>
    </head>
    <body>
    <h2>You need a username and password to
    access this service</h2>


    If you have lost or forgotten your
    password, tough!
    </body>
    </html>
    <?php

    exit;
    }

    #### e qui un altro ancora?

    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <html>
    <head>
    <title>Web Database Applications</title>
    </head>
    <body>
    <h2>Welcome!</h2>
    </body>
    </html>


    consiglio di dividere in piu pagine, e controllare attentamente il passaggio di parametri.....
    se ho etto boiate scusa

  5. #5
    Dopo aver sistemato il tag input come mi dicevi, nmkbeppe, ho fatto quanto segue:

    1. Ho messo la funzione authenticated in conn.inc.php
    2. Ho fatto fare il controllo di isset delle variabili $_POST['username'] e $_POST['password'] dal file login.php prima di invocare la funzione authenticated.

    Ma ad ogni modo, il debug nella funzione mi dice sempre che le variabili
    $HOSTUSER
    $HOSTPWD
    all'interno della funzione authenticated, non hanno il valore che mi aspetto, impostato nelle variabili globali... insomma le variabili
    $HOSTUSER
    $HOSTPWD
    anche se sono globali, vengono come inizializzate di nuovo in questa funzione... credo sia un problema di scope... me lo dovevo aspettare, o quantomeno controllare prima lo scope delle variabili in php... :rollo: che scema.

    per cui le ho inizializzate io stessa in modo corretto nella funzione, pur mantenendo le globali. ora funziona tutto (il che significa: a un login corretto appare il Welcome, ad un login errato appare lo sproloqui di errore, sempre su login.php). per capirci, riscrivo tutti e 2 i files (con un commento ad hoc nella funzione authenticated).

    NB: grazie per la dritta sui tag html, li ho aggiustati, ho tolto quelli in più. Ho copiato l'esempio di script da un libro della O 'rielly facendo ctrl+c ctr+v e non ho notato la presenza di quei tag, io copiavo in un documento .php dopo i tag di chiusura, dopo il form.



    1) conn.inc.php
    <?php
    $HOSTNAME="mydb.mydomain";
    $HOSTDB="mydb";

    $GUARDIAN="ONLINE";
    $GUARDIANPWD="ENILNO";
    $HOSTUSER="pippo";
    $HOSTPWD="CANTERINO";

    //debug--- qui la stampa a video mi dice i valori giusti delle variabili globali
    echo "debug di inizio conn.inc.php
    ";
    echo "

    HOSTUSER: $HOSTUSER, </p>
    ";
    echo "

    HOSTPWD: $HOSTPWD, </p>
    ";

    function authenticated($username, $password){

    // If either the username or the password are
    // not set, the user is not authenticated
    if (!isset($username) || !isset($password))
    return false;

    if ($username == '' && $password == ''){
    echo "Empty Fields!
    ";
    return false;
    }

    //senza la seguente inizializzazione, le variabili $HOSTUSER e $HOSTPWD
    //usate nel confronto di stringhe successivo risultano vuote e quindi
    //la funzione restituisce false anche se le variabili
    //$username e $password provenienti da login.php sono
    //quelle giuste, identiche alle globali $HOSTUSER e $HOSTPWD
    $HOSTUSER="pippo";
    $HOSTPWD="CANTERINO";
    //debug
    echo "

    HOSTUSER: $HOSTUSER, </p>
    ";
    echo "

    HOSTPWD: $HOSTPWD, </p>
    ";
    echo "

    username: $username, </p>
    ";
    echo "

    password: $password, </p>
    ";

    if ((strcmp($HOSTUSER, $username) == 0)
    &&
    strcmp($HOSTPWD, $password) == 0) {
    echo "true!
    ";
    return true;
    }
    else
    {
    return false;
    }
    }
    ?>


    2) login.php
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <?php include("menu.php");
    include('conn.inc.php');
    ?>
    <title>Login</title>
    </head>
    <body>

    <?php echo '<form name="formname" action="login.php" method="post">';
    ?>
    <form>
    <p class="form">username :<input type="text" name="username"></input></p>

    <p class="form">password :<input type="password" name="password"></input></p>

    <input type="submit" value="invia">
    </form>

    <?php

    if (isset($_POST['username'])&& isset ($_POST['password']))
    if (!authenticated($_POST['username'],$_POST['password']))
    {
    // No credentials found - send an unauthorized
    // challenge response
    header("WWW-Authenticate: Basic realm=\"Flat Foot\"");
    header("HTTP/1.0 401 Unauthorized");

    // Set up the body of the response that is
    // displayed if the user cancels the challenge
    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <h2>You need a username and password to
    access this service</h2>


    If you have lost or forgotten your
    password, tough!
    </body>
    </html>
    <?php

    // exit;
    }

    else{
    // The response to authorized users
    ?>
    <!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd" >
    <h2>Welcome!</h2>
    <?php

    //exit;
    }
    ?>
    </body>
    </html>

  6. #6
    l'importante e che tutto funzion, grazie a me o meno non importa.....io cmq ti dico come "lavoro" e magari ti aiuta......
    pagina di login
    Codice PHP:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Login</title>
    <link href="css/login.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <?php
    if(!isset($_REQUEST['errore'])){

    $_REQUEST['errore']='';
    }

        switch(
    $_REQUEST['errore'])
        {
        
        case
    "log":
        print(
    '<p id="errore">login non effettuata</p>');
            break;
        
        
        case
    "err":
        print(
    '<p id="errore">Username o Password errata</p>');
        break;
        
        case
    "s":
        print(
    '<p id="errore">Sessione scaduta</p>');
            break;
            default:
        echo 
    "";
    }

        
    #print($errore);
        
    ?>

    <form action="lib/controllo.php" method="post" name="login" >

        <table id="log">
            
            <tr>
                <td>



                Username</td>
                <td >
                


                <input type="text" name="nome" value=""/ class="txt"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td ><input type="password" name="psw" value=""/ class="txt"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" name="invia" value="Login"/></td>
            </tr>
        </table>
    </form>
        
        
    </body>
    </html>
    # questa invece e la pagina di controllo
    Codice PHP:
    <?php
    session_start
    ();
    // controllo se e stato premuto il tasto invio


    // reindirezione se non si ha effettuato il login

    if(!isset($_REQUEST['invia']))
    {
    header("Location:../index.php?errore=log");

    }

    // includo connessione db e funzioni generali
    include('db.php');
    include(
    'functions.php');
    //ricevo valori dal form login

    $nome=$_REQUEST['nome'];
    $psw=$_REQUEST['psw'];

    $sql='
        SELECT 
        nome, psw
        FROM utenti
        '
    ;
    $ris=(DbRead($sql));

    #var_dump_pre($risultato);



    while($riga mysql_fetch_assoc($ris)){
            
    #var_dump_pre($riga); exit();
            
    $n=$riga['nome'];
            
    $p=$riga['psw'];
            
    }
            

    if ((
    $n==$nome)and($p==$psw))
    {
    #print('ok');
    $_SESSION["login"]=1;
    header("Location: ../menu.php");
    }
    else
    {
    header("Location:../index.php?errore=err");
    }
    ?>
    poi includo in ogni file questo x verifica.....molto semplice il tutto e sopratutto lineare
    Codice PHP:
    <?php
    session_start
    ();


    if(!isset(
    $_SESSION['login'])){
    header("Location:index.php?errore=log");
    }
    if((
    $_SESSION['login'])!=1){
    header("Location:index.php?errore=s");
    }
    ?>

  7. #7
    Grazie mille mi sei stato molto di aiuto dandomi il tuo codice, mi hai fatto capire come scrivere in modo sistemato ciò che avevo in mente, ti ringrazio davvero. Una cosa sola:

    Originariamente inviato da nmkbeppe
    poi includo in ogni file questo x verifica.....molto semplice il tutto e sopratutto lineare
    Codice PHP:
    <?php
    session_start
    ();


    if(!isset(
    $_SESSION['login'])){
    header("Location:index.php?errore=log");
    }
    if((
    $_SESSION['login'])!=1){
    header("Location:index.php?errore=s");
    }
    ?>
    Cioè, metti questo codice in tutti i file del sito - tranne login.php e controllologin.php?

    ciao

  8. #8
    si...e un file che includo sempre che controlla solo una variabile di sessione così da poter evitare che qualcuno digitand www.xxxx.it/gestione/in.php entri nel pannello di inserimento saltando a piè pari l'index.....
    è abbastanza chiaro ?

  9. #9
    si si, chiaro grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.