Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15

Discussione: session_start

  1. #1

    session_start

    Inanzitutto come vi sembra questo script,poi ditemmi perchè ricevo questi errori,grazie in una eventuale risposta!

    <?php

    define('INCLUDE_CHECK',true);

    require 'connect.php';
    require 'functions.php';
    // Those two files can be included only if INCLUDE_CHECK is defined


    session_name('tzLogin');
    // Starting the session

    session_set_cookie_params(2*7*24*60*60);
    // Making the cookie live for 2 weeks

    session_start();

    if($_SESSION['id'] && !isset($_COOKIE['tzRemember']) && !$_SESSION['rememberMe'])
    {
    // If you are logged in, but you don't have the tzRemember cookie (browser restart)
    // and you have not checked the rememberMe checkbox:

    $_SESSION = array();
    session_destroy();

    // Destroy the session
    }


    if(isset($_GET['logoff']))
    {
    $_SESSION = array();
    session_destroy();

    header("Location: demo.php");
    exit;
    }

    if($_POST['submit']=='Login')
    {
    // Checking whether the Login form has been submitted

    $err = array();
    // Will hold our errors


    if(!$_POST['username'] || !$_POST['password'])
    $err[] = 'All the fields must be filled in!';

    if(!count($err))
    {
    $_POST['username'] = mysql_real_escape_string($_POST['username']);
    $_POST['password'] = mysql_real_escape_string($_POST['password']);
    $_POST['rememberMe'] = (int)$_POST['rememberMe'];

    // Escaping all input data

    $row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM tz_members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));

    if($row['usr'])
    {
    // If everything is OK login

    $_SESSION['usr']=$row['usr'];
    $_SESSION['id'] = $row['id'];
    $_SESSION['rememberMe'] = $_POST['rememberMe'];

    // Store some data in the session

    setcookie('tzRemember',$_POST['rememberMe']);
    }
    else $err[]='Wrong username and/or password!';
    }

    if($err)
    $_SESSION['msg']['login-err'] = implode('
    ',$err);
    // Save the error messages in the session

    header("Location: demo.php");
    exit;
    }
    else if($_POST['submit']=='Register')
    {
    // If the Register form has been submitted

    $err = array();

    if(strlen($_POST['username'])<4 || strlen($_POST['username'])>32)
    {
    $err[]='Your username must be between 3 and 32 characters!';
    }

    if(preg_match('/[^a-z0-9\-\_\.]+/i',$_POST['username']))
    {
    $err[]='Your username contains invalid characters!';
    }

    if(!checkEmail($_POST['email']))
    {
    $err[]='Your email is not valid!';
    }

    if(!count($err))
    {
    // If there are no errors

    $pass = substr(md5($_SERVER['REMOTE_ADDR'].microtime().rand(1,100000)),0,6);
    // Generate a random password

    $_POST['email'] = mysql_real_escape_string($_POST['email']);
    $_POST['username'] = mysql_real_escape_string($_POST['username']);
    // Escape the input data


    mysql_query(" INSERT INTO tz_members(usr,pass,email,regIP,dt)
    VALUES(

    '".$_POST['username']."',
    '".md5($pass)."',
    '".$_POST['email']."',
    '".$_SERVER['REMOTE_ADDR']."',
    NOW()

    )");

    if(mysql_affected_rows($link)==1)
    {
    send_mail( 'demo-test@tutorialzine.com',
    $_POST['email'],
    'Registration System Demo - Your New Password',
    'Your password is: '.$pass);

    $_SESSION['msg']['reg-success']='We sent you an email with your new password!';
    }
    else $err[]='This username is already taken!';
    }

    if(count($err))
    {
    $_SESSION['msg']['reg-err'] = implode('
    ',$err);
    }

    header("Location: demo.php");
    exit;
    }

    $script = '';

    if($_SESSION['msg'])
    {
    // The script below shows the sliding panel on page load

    $script = '
    <script type="text/javascript">

    $(function(){

    $("div#panel").show();
    $("#toggle a").toggle();
    });

    </script>';

    }
    ?>


    ricevo questi errori:

    Warning: session_start(): Cannot send session cookie - headers already sent by (output started at y:\home\login\www\index.php:2) in y:\home\login\www\index.php on line 17

    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at y:\home\login\www\index.php:2) in y:\home\login\www\index.php on line 17

    Notice: Undefined index: id in y:\home\login\www\index.php on line 19

    Notice: Undefined index: username in y:\home\login\www\index.php on line 89

    Notice: Undefined index: username in y:\home\login\www\index.php on line 94

    Fatal error: Call to undefined function: checkemail() in y:\home\login\www\index.php on line 99

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2007
    Messaggi
    154
    prova a mettere come prima riga di codice(E' IMPORTANTE CHE SIA LA PRIMA.. ):
    codice:
    <?
    ob_start();
    ?>
    così ti dovrebbe funzionare

  3. #3
    Quoto e aggiungo che la funzione checkEmail non è stata dichiarata, prova a controllare di aver scritto il nome giusto (E' un errore molto frequente )


    PS: Quando devi postare codice php, mettilo tra i tag [ PHP] e [/ PHP] (senza spazi), così risulata molto più leggibile

  4. #4
    adesso purtroppo mi da altri errori aiuto

    Notice: Undefined index: id in y:\home\login\www\index.php on line 22

    Notice: Undefined index: submit in y:\home\login\www\index.php on line 43

    Notice: Undefined index: submit in y:\home\login\www\index.php on line 86

    Notice: Undefined index: msg in y:\home\login\www\index.php on line 153

  5. #5
    Non conosco questi errori, quindi non ti posso auitare, guarda però questa vecchia discussione dove viene i
    espsto e, mi par di capire, risolto, un problema simile al tuo.
    In ogni caso quelli non sono veri errori, ma solo delle notifiche, quindi lo script dovrebbe funzionare ugualmente.
    Un ultima cosa: al posto di exit;, ormai obsoleto e assolutamente sconsigliato, usa die();

  6. #6
    piuttosto che "nascondere" o bypassare gli errori (i notice) sarebbe meglio correggerli

    inoltre è probabile che prima del <?php ci stiano degli invii/spazi ... o nel file principale o nei file che includi

  7. #7
    non mi piace questa idea di nascondere gli errori( però ho provato di tutto è non ci riesco a farlo funzionare,è nessuno non mi aiuta(

  8. #8
    Prova mettendo nel codice session_start() prima di session_name('tzLogin');

  9. #9
    direi che dovresti iniziare a togliere l'ob_start, poi controllare ce ci sono spazi o altro prima dei setcookie/headers ed infine utilizzare

    www.php.net/isset

    per verificare se le chiavi degli array superglobali a cui si riferiscono gli errori ci sono è possono essere usate

    Notice: Undefined index: id in y:\home\login\www\index.php on line 22

    Notice: Undefined index: submit in y:\home\login\www\index.php on line 43

    Notice: Undefined index: submit in y:\home\login\www\index.php on line 86

    Notice: Undefined index: msg in y:\home\login\www\index.php on line 153
    gli errori non stanno dicendo altro che "la chiave (id|submit|msg) non è presente nell'array in cui la cerchi"

    quindi fai tipo

    if (isset($_SESSION['id']) .....

    (cosa che già fai con i cookie)

    poi

    if (isset($_POST['submit']) ......

    e via dicendo

  10. #10
    Originariamente inviato da E.d.i.73
    Prova mettendo nel codice session_start() prima di session_name('tzLogin');
    Provando a mettere session_start() prima di session_name('tzLogin');mi da questi errori:
    Notice: Undefined index: id in y:\home\login\www\index.php on line 18

    Notice: Undefined index: submit in y:\home\login\www\index.php on line 39

    Notice: Undefined index: submit in y:\home\login\www\index.php on line 82

    Notice: Undefined index: msg in y:\home\login\www\index.php on line 149

    <?php

    define('INCLUDE_CHECK',true);

    require 'connect.php';
    require 'functions.php';
    // Those two files can be included only if INCLUDE_CHECK is defined

    session_start();

    session_name('tzLogin');
    // Starting the session

    session_set_cookie_params(2*7*24*60*60);
    // Making the cookie live for 2 weeks


    if($_SESSION['id'] && !isset($_COOKIE['tzRemember']) && !$_SESSION['rememberMe'])
    {
    // If you are logged in, but you don't have the tzRemember cookie (browser restart)
    // and you have not checked the rememberMe checkbox:

    $_SESSION = array();
    session_destroy();

    // Destroy the session
    }


    if(isset($_GET['logoff']))
    {
    $_SESSION = array();
    session_destroy();

    header("Location: demo.php");
    exit;
    }

    if($_POST['submit']=='Login')
    {
    // Checking whether the Login form has been submitted

    $err = array();
    // Will hold our errors


    if(!$_POST['username'] || !$_POST['password'])
    $err[] = 'All the fields must be filled in!';

    if(!count($err))
    {
    $_POST['username'] = mysql_real_escape_string($_POST['username']);
    $_POST['password'] = mysql_real_escape_string($_POST['password']);
    $_POST['rememberMe'] = (int)$_POST['rememberMe'];

    // Escaping all input data

    $row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM tz_members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));

    if($row['usr'])
    {
    // If everything is OK login

    $_SESSION['usr']=$row['usr'];
    $_SESSION['id'] = $row['id'];
    $_SESSION['rememberMe'] = $_POST['rememberMe'];

    // Store some data in the session

    setcookie('tzRemember',$_POST['rememberMe']);
    }
    else $err[]='Wrong username and/or password!';
    }

    if($err)
    $_SESSION['msg']['login-err'] = implode('
    ',$err);
    // Save the error messages in the session

    header("Location: demo.php");
    exit;
    }
    else if($_POST['submit']=='Register')
    {
    // If the Register form has been submitted

    $err = array();

    if(strlen($_POST['username'])<4 || strlen($_POST['username'])>32)
    {
    $err[]='Your username must be between 3 and 32 characters!';
    }

    if(preg_match('/[^a-z0-9\-\_\.]+/i',$_POST['username']))
    {
    $err[]='Your username contains invalid characters!';
    }

    if(!checkEmail($_POST['email']))
    {
    $err[]='Your email is not valid!';
    }

    if(!count($err))
    {
    // If there are no errors

    $pass = substr(md5($_SERVER['REMOTE_ADDR'].microtime().rand(1,100000)),0,6);
    // Generate a random password

    $_POST['email'] = mysql_real_escape_string($_POST['email']);
    $_POST['username'] = mysql_real_escape_string($_POST['username']);
    // Escape the input data


    mysql_query(" INSERT INTO tz_members(usr,pass,email,regIP,dt)
    VALUES(

    '".$_POST['username']."',
    '".md5($pass)."',
    '".$_POST['email']."',
    '".$_SERVER['REMOTE_ADDR']."',
    NOW()

    )");

    if(mysql_affected_rows($link)==1)
    {
    send_mail( 'demo-test@tutorialzine.com',
    $_POST['email'],
    'Registration System Demo - Your New Password',
    'Your password is: '.$pass);

    $_SESSION['msg']['reg-success']='We sent you an email with your new password!';
    }
    else $err[]='This username is already taken!';
    }

    if(count($err))
    {
    $_SESSION['msg']['reg-err'] = implode('
    ',$err);
    }

    header("Location: demo.php");
    exit;
    }

    $script = '';

    if($_SESSION['msg'])
    {
    // The script below shows the sliding panel on page load

    $script = '
    <script type="text/javascript">

    $(function(){

    $("div#panel").show();
    $("#toggle a").toggle();
    });

    </script>';

    }
    ?>

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.