Visualizzazione risultati 1 fino 5 di 5

Discussione: Problema con sessioni e file .txt

  1. #1
    Guest

    Angry Problema con sessioni e file .txt

    Ciao a tutti...
    Ormai è da giorni che cerco di capire come poter utilizzare le sessioni con i file di testo e visto che non ho trovato nulla sui motori di ricerca, mi affido a voi.
    Io ho un database .txt di nome user.txt in cui ho i dati degli utenti, scritto in questo modo:

    Codice:
    id||nome||email||password
    e una pagina di login scritta così:

    Codice PHP:
    <?php session_start();

    // Data file
    $data = "users.txt";


    // Enable / Disable user registration: 1 = enabled, 0 = disabled
    $registration = 1;

    // Main cascading stylesheets
    $style = '
    html, body {
    height: 99%;
    font-family: Arial, Verdana;
    font-size: 12px;
    }
    #titlebar {
    position: relative;
    height: 20px;
    width: 260px;
    border-top: 1px solid #000000;
    border-left: 1px solid #000000;
    border-right: 1px solid #000000;
    background-color: #ffb71b;
    color: #000000;
    text-align: center;
    font-size: 16px;
    }
    #foot {
    position: relative;
    height: 20px;
    width: 260px;
    color: #000000;
    text-align: center;
    font-size: 10px;
    }
    input.text {
    width: 220px;
    height: 20px;
    border: 1px solid #000000;
    background-color: #ffeecb;
    }
    input.btn {
    width: 60px;
    height: 20px;
    border: 1px solid #000000;
    background-color: #ffb71b;
    color: #000000;
    }
    input.btn:hover {
    cursor: pointer;
    }
    a {
    color: #ffb71b;
    text-decoration: none;
    }
    a:hover {
    text-decoration: underline;
    }
    '
    ;
    $log_style = '
    #wrapper {
    position: absolute;
    height: 185px;
    width: 260px;
    text-align: center;
    color: #000000;
    left: 50%;
    top: 50%;
    margin-left: -130px;
    margin-top: -85px;
    }
    #form {
    position: relative;
    height: 165px;
    width: 260px;
    border: 1px solid #000000;
    background-color: #fff9eb;
    color: #000000;
    text-align: left;
    }
    '
    ;
    $reg_style = '
    #wrapper {
    position: absolute;
    height: 270px;
    width: 260px;
    text-align: center;
    color: #000000;
    left: 50%;
    top: 50%;
    margin-left: -130px;
    margin-top: -135px;
    }
    #form {
    position: relative;
    height: 250px;
    width: 260px;
    border: 1px solid #000000;
    background-color: #fff9eb;
    color: #000000;
    text-align: left;
    }
    '
    ;

    // Do not edit below this line

    $users = $path . $data;

    if (
    file_exists($users)) {
    $records = count(file($users));
    }
    else {
    fopen($users, 'w') or die("Can't open user database.");
    }
    if (isset(
    $_GET['ls_logout'])) {
    unset(
    $_SESSION['ls_id']);
    unset(
    $_SESSION['ls_user']);
    unset(
    $_SESSION['ls_email']);
    }
    if (isset(
    $_POST)) {
    $login = FALSE;
    $register = FALSE;
    $errors = '';
    foreach (
    $_POST as $key => $value) {
    if (
    $key == "ls_reg") { $login = FALSE; $register = TRUE; }
    else if (
    $key == "ls_log") { $login = TRUE; $register = FALSE; }
    else if (
    $key == "ls_user") {
    if (!
    eregi('^[[:alnum:]\.\'\-]{3,15}$', $value)) { $u_invalid = 1; }
    $user = $value;
    }
    else if (
    $key == "ls_email") {
    if (!
    eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9_-]+){1}(\.[a-zA-Z0-9]+){1,2}', $value)) { $e_invalid = 1; }
    $email = $value;
    }
    else if (
    $key == "ls_pass") {
    if (!
    eregi("^[[:alnum:]\.\'\-]{3,15}$", $value)) { $p_invalid = 1; }
    $pass = md5($value);
    }
    else if (
    $key == "ls_repeat") { $repeat = md5($value); }
    }
    }
    if (
    $login == TRUE) {
    if (
    file_exists($users)) {
    $lines = file($users);
    foreach (
    $lines as $line_num => $line) {
    $array = explode("||",str_replace("\n","",$line));
    $c_id = $array[0];
    $c_user = $array[1];
    $c_email = $array[2];
    $c_pass = $array[3];
    if (
    $c_user == $user && $c_pass == $pass) {
    $_SESSION['ls_id'] = $c_id;
    $_SESSION['ls_user'] = $c_user;
    $_SESSION['ls_email'] = base64_decode($c_email);
    }
    }
    if (!isset(
    $_SESSION['ls_id']) || !isset($_SESSION['ls_user']) || !isset($_SESSION['ls_email'])) {
    $errors[] = "Invalid Login.";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Login Error</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">
    <?php echo $style; ?>
    <?php echo $log_style; ?>
    </style>
    </head>
    <body>
    <div id="wrapper">
    <div id="titlebar">Errors</div>
    <div id="form">
    <div style="padding:10px;">
    <ul style="padding:0px;margin:15px;">
    <?php
    foreach ($errors as $msg) {
    echo
    "<li style=\"padding:0px;margin:0px;\">$msg</li>";
    }
    $errors = '';
    ?>
    </ul>
    <div style="text-align:center;padding:20px;">
    <a href="<?php echo $_SERVER['HTTP_REFERER']; ?>" rel="">Click Here</a> to go back.
    </div>
    </div>
    </div>
    </div>
    </body>
    </html>
    <?php
    exit();
    }
    }
    }
    else if (
    $register == TRUE) {
    if (
    file_exists($users)) {
    $lines = file($users);
    foreach (
    $lines as $line_num => $line) {
    $array = explode("||",str_replace("\n","",$line));
    $c_id = $array[0];
    $c_user = $array[1];
    $c_email = $array[2];
    $c_pass = $array[3];
    if (
    $user == $c_user) { $u_taken = 1; }
    if (
    base64_encode($email) == $c_email) { $e_taken = 1; }
    }
    if (
    $user == NULL) { $errors[] = "User cannot be blank."; }
    if (
    $u_invalid == 1) { $errors[] = "User <strong>$user</strong> is invalid. 3-15 alphanumeric characters required."; }
    if (
    $u_taken == 1) { $errors[] = "Username <strong>$user</strong> is already taken."; }
    if (
    $email == NULL) { $errors[] = "Email cannot be blank."; }
    if (
    $e_invalid == 1) { $errors[] = "Email address <strong>$email</strong> is invalid."; }
    if (
    $e_taken == 1) { $errors[] = "Email address <strong>$email</strong> is already taken."; }
    if (
    $pass == md5(NULL)) { $errors[] = "Password cannot be blank."; }
    if (
    $p_invalid == 1) { $errors[] = "Password is invalid. 3-15 alphanumeric characters required."; }
    if (
    $repeat == md5(NULL)) { $errors[] = "Password verification cannot be blank."; }
    if (
    $pass != $repeat) { $errors[] = "Password and verification do not match."; }
    }
    if (empty(
    $errors)) {
    $newline = $records++;
    $e_email = base64_encode($email);
    $data = "$newline||$user||$e_email||$pass||\n";
    $fh = fopen($users, 'a') or die("Can't open user database.");
    fwrite($fh, $data);
    fclose($fh);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Registration Success</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">
    <?php echo $style; ?>
    <?php echo $log_style; ?>
    </style>
    </head>
    <body>
    <div id="wrapper">
    <div id="titlebar">Success</div>
    <div id="form">
    <div style="text-align:center;padding:20px;">
    You are now registered.<br />
    <a href="<?php echo $_SERVER['REQUEST_URI']; ?>" rel="">Click Here</a> to log in.
    </div>
    </div>
    </div>
    </body>
    </html>
    <?php
    exit();
    }
    else {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Registration Error</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">
    <?php echo $style; ?>
    <?php echo $reg_style; ?>
    </style>
    </head>
    <body>
    <div id="wrapper">
    <div id="titlebar">Errors</div>
    <div id="form">
    <div style="padding:10px;">
    <ul style="padding:0px;margin:15px;">
    <?php
    foreach ($errors as $msg) {
    echo
    "<li style=\"padding:0px;margin:0px;\">$msg</li>";
    }
    $errors = '';
    ?>
    </ul>
    <div style="text-align:center;padding:20px;">
    <a href="<?php echo $_SERVER['HTTP_REFERER']; ?>" rel="">Click Here</a> to go back.
    </div>
    </div>
    </div>
    </div>
    </body>
    </html>
    <?php
    exit();
    }
    }
    else if (isset(
    $_GET['ls_register'])) {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>New User Registration</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style type="text/css">
    <?php echo $style; ?>
    <?php echo $reg_style; ?>
    </style>
    </head>
    <body>
    <div id="wrapper">
    <div id="titlebar">Register</div>
    <div id="form">
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    <div style="text-align:left;padding:20px;width:220px;">
    <label>User:<br />
    <input type="text" name="ls_user" value="" class="text" /></label><br />
    <label>Email Address:<br />
    <input type="text" name="ls_email" value="" class="text" /></label><br />
    <label>Password:
    <input type="password" name="ls_pass" value="" class="text" /></label><br />
    <label>Password Repeat:
    <input type="password" name="ls_repeat" value="" class="text" /></label><br />
    <div style="text-align:center;margin:20px 0px 20px 0px;">
    <input type="submit" name="ls_reg" value="Register" class="btn" />
    </div>
    </div>
    </form>
    </div>
    </div>
    </body>
    </html>
    <?php
    exit();
    }
    else if (!isset(
    $_SESSION['ls_id']) && !isset($_SESSION['ls_user']) && !isset($_SESSION['ls_email'])) {
    ?>
    il resto del messaggio lo continuo dopo
    Ultima modifica di greekpolis : 30-01-2011 alle ore 16.41.03

  2. #2
    Guest

    Predefinito

    Codice PHP:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Log In Required</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <style type="text/css">
    <?php echo $style; ?>
    <?php echo $log_style; ?>
    </style>
    </head>
    <body>
    <div id="wrapper">
    <div id="titlebar">Log In Required</div>
    <div id="form">
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    <div style="text-align:left;padding:20px;width:220px;">
    <label>User:<br />
    <input type="text" name="ls_user" value="" class="text" /></label><br />
    <label>Password:
    <input type="password" name="ls_pass" value="" class="text" /></label><br />
    <div style="text-align:center;margin:20px 0px 0px 0px;">
    <input type="submit" name="ls_log" value="Log In" class="btn" />
    </div>
    <?php
    if ($registration == 1) {
    ?>
    <div style="text-align:right;"><a href="<?php echo $_SERVER['PHP_SELF'] ?>?ls_register" rel="">Register</a></div>
    <?php
    }
    ?>
    </div>
    </form>
    </div>
    </div>
    </body>
    </html>
    <?php
    exit();
    }
    ?>


    ed infine una pagina di nome pagina index.php scritta così:

    Codice PHP:
    <?php require("login.php"); ?>
    #il logout
    <a href="<?php $_SERVER['PHP_SELF']; ?>?ls_logout" rel="">Logout</a>
    </html>
    Il codice è funzionante, ma quando voglio modificare le variabili, per esempio:
    Il nome è: Mario e nel codice php scrivo:

    Codice PHP:
    <?php echo $_SESSION['ls_user']; ?>
    Nella pagina viene la scritta Mario, ma se attraverso il foglio txt cambio il nome mario in Alex nella pagina del sito rimane sempre mario e per poter farlo modificare devo fare il logout e rientrare...

  3. #3
    L'avatar di javascripter
    javascripter non è connesso Moderatore
    Data registrazione
    14-02-2010
    Messaggi
    1,114

    Predefinito

    Perchè quel codice, se si è loggati, non riaggiorna le variabili di sessione. È utile per risparmiare risorse e diminuire il tempo di caricamento della pagina.
    Per risolvere potresti creare uno script che aggiorna i dati in sessione dopo aver modificato il txt.

    Mi permetto di fare qualche critica...
    Il codice è scritto in modo pessimo; non ferma i loop una volta trovati i dati, fa uso di funzioni ormai deprecate ( ereg(i) ) e soprattutto è disordinato.
    Se l'hai scritto tu, dovresti migliorarlo.
    Ultima modifica di javascripter : 30-01-2011 alle ore 18.44.39

  4. #4
    Guest

    Predefinito

    Per prima cosa grazie per il consiglio e per la critica (che accetto volentieri), non sono stato io a scrivere il codice, ma l'ho trovato in una discussione in questo forum...
    Ora cerco di aggiustarlo e se avrò ulteriori problemi (speriamo di no) mi rivolgerò sicuramente in questo forum
    -
    Se non ti è di troppo disturbo non è che potresti dirmi come fare il codice per aggiornare i dati in sessione dopo aver modificato il txt?
    Ultima modifica di darkwolf : 31-01-2011 alle ore 17.04.10 Motivo: come da regolamento, nion fare doppi post, usa il tasto edita!

  5. #5
    Guest

    Smile Login php con sessioni

    Io avrei bisogno di un semplice script di login in php che si basi su un database .txt e sulle sessioni e, se non è di troppo disturbo, potreste spiegarmi come far si che le variabili di sessione siano collegate al database .txt e che quindi quando le modifico si aggiorni anche il database (sempre .txt)...
    Grazie in anticipo.

    Matt2397

Regole di scrittura

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