Visualizzazione risultati 1 fino 3 di 3

Discussione: Tenere loggato un utente.

  1. #1
    Guest

    Predefinito Tenere loggato un utente.

    Come posso tenere loggato un utente per ogni pagina che visita dopo che ha fatto il login?

    Questo è il codice che include alla pagina nel box di destra la pagina di login:

    Codice PHP:
    <!-- Menu di destra con login e opzioni //-->

    <td width="5"><td class="menuright">
    <div>
    <?php
    include("main_login.php");
    ?>
    </div></td>

    <!-- Menu di destra con login e opzioni Fine//-->
    e vorrei che se l'utente è loggato, in quel menù includa un altro file... come posso fare?

    Grazie!

    PS: Il codice della pagina di login è questo ( main_login.php )

    Codice PHP:
    <HTML>
    <
    HEAD>
    <
    TITLE>Login</TITLE>
    <
    style type="text/css">
    A:hover {text-decoration: underline; color: gold;}
    A:link {text-decoration: underline; color: red;}
    A:visited {text-decoration: underline; color: red;}
    </
    style>
    </
    HEAD>
    <
    BODY>
    <
    table border="0" cellspacing="1" cellpadding="0" width="230" align="left" bgcolor="blue"><tbody>
    <
    tr><form action="login.php" method="post"> <input name="phpMyAdmin" type="hidden" value="V0N0db6Fl62QWr6eBkly75xAGn3" /><td>
    <
    table border="0" cellspacing="1" cellpadding="3" width="100%" bgcolor="#0160C9"><tbody>
    <
    tr>
    <
    td width="78">Username</td>
    <
    td width="6">:</td>
    <
    td width="294"><input id="myusername" name="myusername" type="text" /></td>
    </
    tr>
    <
    tr>
    <
    td>Password</td>
    <
    td>:</td>
    <
    td><input id="mypassword" name="mypassword" type="password" /></td>
    </
    tr>
    <
    tr>
    <
    td></td>
    <
    td></td>
    <
    td><input name="login" type="submit" value="Login" /></td>
    <
    strong>Login Membri</strong>
    </
    tr>
    <
    tr>
    <
    td></td>
    <
    td></td>
    <
    td><a href="password_dimenticata.php">Password dimenticata</a></td>
    </
    tr>
    <
    tr>
    <
    td></td>
    <
    td></td>
    <
    td><a href="registrazione2.php">Non sei registrato? Clicca Qui!</a></td>
    </
    tr>
    </
    tbody></table>
    </
    td>
    </
    form></tr>
    </
    tbody></table>
    </
    body>
    </
    html>
    e la pagina dove va è questa ( login.php ):

    Codice PHP:
    <?php
    session_start
    ();
    include_once
    'include\dbCon.php';
    $username = $_POST['username'];
    $password = $_POST['password'];
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;

    if((
    $_SESSION['username'] =='willos82')and ($_SESSION['password'] =='14071982@')){
    print
    "<META HTTP-EQUIV='REFRESH' CONTENT='0; URL=admin.php'>";
    }elseif(
    verificaLogin($username,$password)){
    print
    "<META HTTP-EQUIV='REFRESH' CONTENT='3; URL=inNews.php'>";

    }

    function
    controlloLogin(){
    $errore = "";

    if(
    strlen($_POST['username'])==0){
    $erroreUsername = "il campo non può essere nullo. ";
    $errore = "errore";
    }

    if(
    strlen($_POST['password'])==0){
    $errorePassword = "il campo non può essere nullo. ";
    $errore = "errore";
    }

    if(!
    verificaLogin($_POST['username'],$_POST['password'])){
    $errore = "errore";
    $erroreLogin = "username o password errati";
    }

    if(
    $errore == "errore"){
    $_SESSION['erroreLogin'] = $erroreLogin;
    $_SESSION['erroreUsername'] = $erroreUsername;
    $_SESSION['errorePassword'] = $errorePassword;
    return
    false;
    }else{
    return
    true;
    }
    }



    function
    verificaLogin($usr, $pwd){

    $sql = "select * from utenti where username = '".$usr."' and password = '".$pwd."';";
    echo
    "Attendere il redizionanamento";
    $rs = connessione($sql);
    if(
    mysql_num_rows($rs)==1){
    $row = mysql_fetch_array($rs);
    $_SESSION['IDUtente'] = $row['id'];
    $_SESSION['username'] = $row['username'];
    return
    true;
    }else{
    echo
    "Non 6 nei nostri db Registrati!!";

    return
    false;
    }

    }
    ?>

  2. #2
    Guest

    Predefinito

    Avvii la sessione e verifichi il valore di $_SESSION['IDUtente']. Se ha un valore vuol dire che è loggato altrimenti no
    Codice PHP:
    if($_SESSION['IDUtente'] > 0) {
    //utente loggato, fai quello che devi fare
    }else //utente non loggato

  3. #3
    Guest

    Predefinito Perfetto

    Perfetto funziona!
    Grazie.

    PS: Bello il tuo sito =).

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •