• Super User •

    Sessione non viene aperta

    Salve... sono sempre io... ho finalmente finito il sistema di registrazione/login e anche quello per inserire le news... l'ultima cosa che mi resta da fare è proteggere certe pagine da accessi indesiderati (come la pagina per inserire news ad esempio) per farlo ho pensato di usare le sessioni. In pratica quando un utente fa il login viene avviata una sessione, e per le pagine protette ho impostato che se la sessione non è attiva di fare un redirect immediato a index.php della serie

    if(!isset($_SESSION['lin']))
    {
    continue
    }
    else
    {
    header('Location: index.php');
    
    ```questa è la pagina di che convalida il login e che (in teoria) starta la sessione:
    
    

    <?php
    header('Location: restricted.php');
    ?>
    <html>
    <head>
    </head>
    <body>
    <?php
    $uname=$_POST["username"];
    $tpass=sha1($_POST["password"]);
    require('config.php');
    mysql_select_db("cms", $connection);
    $query = mysql_query("SELECT id FROM users WHERE name = '$uname' and pass = '$tpass' LIMIT 1");
    mysql_query($query);
    if(mysql_num_rows($query) == 1)
    {
    $lin = mysql_fetch_array($query);
    $_SESSION['login'] = $lin['name'];
    exit;
    }
    else
    {
    die('Nome Utente o Password errati');
    }
    ?>
    </body>
    </html>

    
    

    <?php
    require('config.php');
    if(!isset($_SESSION['lin']))
    {
    print "not set";
    }
    ?>
    <head>
    <title>Area privata</title>
    </head>

    <body>
    Pagina privata!<br />
    <br />
    <br />
    <a href="logout.php">Logout</a><br />
    </body>
    </html>

    
    PS: config.php è il file che contiene i parametri per la connessione al database :)

  • ModSenior •

    Ciao,

    nel config è presente session_start() ?


  • Super User •

    No...

    config.php:

    <html>
    <head>
    </head>
    <body>
    <?php
    
    
    /* Set your mysql parameters */
    $dbhost="localhost"; //host (usually localhost)
    $dbuser="root"; //mysql username (usually root)
    $dbpass=""; //mysql password
    
    $connection = mysql_connect ("$dbhost", "$dbuser", "$dbpass");
    if (!$connection)
    {
        die ('Could not connect: ' . mysql_error());
    }
    
    
    ?>
    </body>
    </html>
    

  • ModSenior •

    Per poter usare le sessioni devi fare session_start, una volta sola nelle pagine in cui devi utilizzare queste variabili...


  • Super User •

    Infatti... dovè che la uso 2 volte? :mmm:


  • ModSenior •

    Il problema è che non la usi proprio...


  • Super User •

    Quindi devo metter un session_start(); sia in validator.php (la pagina che controlla in login) che in restricted.php (la pagina che controlla se la variabilie è attiva)?


  • ModSenior •

    Si, o in alternativa metterla nel config, in modo che sia valida per tutti.
    Il session_start deve comunque stare prima di qualsiasi istruzione di output, altrimenti ti genera un errore.


  • Super User •

    Oddio... mi da quest'errore su restricted.php o.O

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\nfs\config.php:5) in C:\xampp\htdocs\nfs\config.php on line 6

    Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\nfs\config.php:5) in C:\xampp\htdocs\nfs\validator.php on line **3

    **config.php

    <html>
    <head>
    </head>
    <body>
    <?php
    session_start();
    
    /* Set your mysql parameters */
    $dbhost="localhost"; //host (usually localhost)
    $dbuser="root"; //mysql username (usually root)
    $dbpass=""; //mysql password
    
    $connection = mysql_connect ("$dbhost", "$dbuser", "$dbpass");
    if (!$connection)
    {
        die ('Could not connect: ' . mysql_error());
    }
    
    
    ?>
    </body>
    </html>
    ```validator.php
    
    

    <?php
    require('config.php');
    header('Location: restricted.php');
    ?>
    <html>
    <head>
    </head>
    <body>
    <?php
    $uname=$_POST["username"];
    $tpass=sha1($_POST["password"]);

    mysql_select_db("cms", $connection);
    $query = mysql_query("SELECT id FROM users WHERE name = '$uname' and pass = '$tpass' LIMIT 1");
    mysql_query($query);
    if(mysql_num_rows($query) == 1)
    {
    $lin = mysql_fetch_array($query);
    $_SESSION['login'] = $lin['name'];
    exit;
    }
    else
    {
    die('Nome Utente o Password errati');
    }
    ?>
    </body>
    </html>

    
    

    <?php
    require('config.php');
    if(!isset($_SESSION['lin']))
    {
    print "note set";
    }
    ?>
    <head>
    <title>Area privata</title>
    </head>

    <body>
    Pagina privata!<br />
    <br />
    <br />
    <a href="logout.php">Logout</a><br />
    </body>
    </html>


  • ModSenior •

    COme ti ho già detto prima, il session_start deve essere la prima istruzione del file, tu stai mandando in output <html><body> e dopo apri la sessione...

    Comunque se nel config ci metti pure tag html, rimettendoli anche negli altri file, se guardi il sorgente di output finale vedrai molto codice ripetuto...


  • Super User •

    OK ho modificato il config.php togliendo i tag html

    config.php:

    <?php
    session_start();
    
    /* Set your mysql parameters */
    $dbhost="localhost"; //host (usually localhost)
    $dbuser="root"; //mysql username (usually root)
    $dbpass=""; //mysql password
    
    $connection = mysql_connect ("$dbhost", "$dbuser", "$dbpass");
    if (!$connection)
    {
        die ('Could not connect: ' . mysql_error());
    }
    
    
    ?>
    

    però rimane l'errore:

    Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\nfs\config.php:18) in C:\xampp\htdocs\nfs\validator.php on line **3

    **in validator.php ho questo sulla terza riga:

    header('Location: restricted.php');


  • ModSenior •

    Metti l'include del config.php come prima riga su tutti i file.


  • Super User •

    Ma ho già require('config.php'); non è la stessa cosa? :bho:


  • ModSenior •

    Si, ma deve essere in cima,perchè stai mandando output prima del session_start...


  • Super User •

    Dove sto mandando output prima del session start?


  • Super User •

    Salve, ho risolto il problema riscrivendo il codice così:

    <?php session_start();
    require('config.php');
    ?>
    <html>
    <head>
    </head>
    <body>
    <?php
    $uname=$_POST["username"];
    $tpass=sha1($_POST["password"]);

    mysql_select_db("cms", $connection);
    $query = mysql_query("SELECT id FROM users WHERE name = '$uname' and pass = '$tpass' LIMIT 1");
    mysql_query($query);
    if(mysql_num_rows($query) == 1)
    {
    print "Logged in";
    $_SESSION['login']=$uname;
    }
    else
    {
    die('Nome Utente o Password errati');
    }

    ?>
    </body>
    </html>

    in poche parole ho messo

    $_SESSION['login']=$uname;

    al posto di

    $_SESSION['login'] = $lin['name'];

    lo ammetto... non è il massimo dell'eleganza ma prima per qualche motivo non funzionava! :mmm: Secondo voi questa "strategia" ha qualche difetto?


  • Super User •

    Ragazzi... ho capito dove avevo sbagliato... 😄

    in pratica io mettevo

    $query = mysql_query("SELECT id FROM users WHERE name = '$uname' and pass = '$tpass' LIMIT 1");

    e poi

    $_SESSION['login'] = $lin['name'];

    però mi basta sostituire name con id che funziona alla grande... comunque grazie a tutti per il vostro aiuto 😉

    Ora un dilemma... nella sessione è meglio tenere memorizzato id o nome? Nel dubbio entrambi :rollo: