Notice: Undefined index:

Discussion in 'PHP' started by gts, Jul 22, 2009.

  1. #1
    Hello excuse for my bad English, this script out this error:


    Notice: Undefined index: user in C:\Programmi\EasyPHP 3.0\www\b\index.php on line 3

    Notice: Undefined index: pass in C:\Programmi\EasyPHP 3.0\www\b\index.php on line 4

    Notice: Undefined index: login in C:\Programmi\EasyPHP 3.0\www\b\index.php on line 22

    <?php
    require "config.php";
    $cognome = $_COOKIE['user'];
    $password = $_COOKIE['pass'];
    $q = mysql_query("SELECT * FROM dipendenti WHERE cognome='$cognome' AND password='$password'", $con);
    $n = mysql_affected_rows();
    if($n == 1) echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=gestione.php\">";
    ?>
    <html>
    <head>
    <title>Area Dipendenti / Amministrativa</title>
    <link rel="stylesheet" type="text/css" href="estilo.css" />
    <link rel="stylesheet" type="text/css" href="sdmenu/sdmenu.css" />
    <script type="text/javascript" src="tablecloth/tablecloth.js"></script>
    </head>
    <body>
    
    <h2>Accesso all'Area Gestionale.</h2>
    
    <?php
    
    if($_POST['login']) {
     require "config.php";
     $cognome = $_POST['cognome'];
     $password = $_POST['password'];
     $q = mysql_query("SELECT * FROM dipendenti WHERE cognome='$cognome' AND password='$password'", $con);
     $n = mysql_affected_rows();
     if($n == 0) echo "<p align=\"center\">Errore durante il login, verificare i dati immessi.</p>";
     else echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=login.php?c=$cognome&p=$password\">";
    }
    
    ?>
    <p align="center">Effettua il login inserendo i tuoi dati. Non sei registrato? <a href="register.php">Clicka qui</a>.</p>
    
    <form method="post">
    <table align="center" width="50px">
    	<tr>
    		<td>Cognome: <input type="text" name="cognome"></td>
    	</tr>
    	<tr>
    		<td>Password: <input type="password" name="password"></td>
    	</tr>
    	<tr>
    		<td><center><input type="submit" name="login" value="Accedi"></center></td>
    	</tr>
    </table>
    </form>
    
    </body>
    </html>
    Code (markup):
    thanks
     
    gts, Jul 22, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How about if you change:

    
    $cognome = $_COOKIE['user'];
    $password = $_COOKIE['pass'];
    
    PHP:
    to
    
    if(isset($_COOKIE['user']) && isset( $_COOKIE['pass'])){
    $cognome = $_COOKIE['user'];
    $password = $_COOKIE['pass'];
    }
    
    PHP:
     
    wd_2k6, Jul 22, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What wd said, since if cookies, sessions, etc etc are not set its going to trip an error when it can't find the key. Course that being said, make sure you turn off error reporting (least on the screen side of things) on your production server.
     
    kblessinggr, Jul 22, 2009 IP
  4. Ralle

    Ralle Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    Otherwise you can just add this code to the top:
    if(!isset($_COOKIE['user'])) { $_COOKIE['user'] = ''; }
    if(!isset($_COOKIE['pass'])) {$_COOKIE['pass'] = ''; }
    PHP:
     
    Ralle, Jul 22, 2009 IP
  5. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Course if you were going that far you might as well just set your own variables

    
    $user_id = isset($_COOKIE['user'])? $_COOKIE['user']:'';
    $password = isset($_COOKIE['pass'])? $_COOKIE['pass']:'';
    
    PHP:
     
    kblessinggr, Jul 22, 2009 IP
  6. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #6
    From the example (users and passwords) it would seme like Sessions would be more appropriate than Cookies anyhow
     
    wd_2k6, Jul 22, 2009 IP
  7. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You risk adware or something else snooping on your login for that matter (if one were to save a cookie for login purposes, I would normally hash the combined value first which could be verified on the server upon next visit).
     
    kblessinggr, Jul 22, 2009 IP
  8. gts

    gts Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks you are really good people.
    not like the Italian forum
    This script will not be errors but not to insert the data into DB
    Thanks
    <html>
    <head>
    <title>Area Dipendenti / Amministrativa</title>
    <link rel="stylesheet" type="text/css" href="estilo.css" />
    <link rel="stylesheet" type="text/css" href="sdmenu/sdmenu.css" />
    <script type="text/javascript" src="tablecloth/tablecloth.js"></script>
    </head>
    <body>
    
    <h2>Registrazione all'Area Gestionale.</h2>
    
    <?php
    
    if(!empty($_POST['register']))  {
     require "config.php";
     $cognome = $_POST['cognome'];
     $password = $_POST['password'];
     $data_registrazione = time();
     $q = mysql_query("SELECT * FROM dipendenti WHERE cognome='$cognome'", $con);
     $n = mysql_affected_rows();
     if($n > 0) echo "<p align=\"center\">Esiste gi&agrave; un dipendente registrato a nome di $cognome.</p>";
     else {
     mysql_query("INSERT INTO dipendenti (cognome,password,data_registrazione) VALUES ('$cognome','$password','$data_registrazione')", $con);
     echo "<p align=\"center\">Registrazione effettuata con successo! Per accedere <a href=\"index.php\">clicka qui</a>.</p>";
     exit();
     }
    }
    
    ?>
    <p align="center">Effettua la registrazione inserendo i tuoi dati. Gi&agrave; registrato? <a href="index.php">Clicka qui</a>.</p>
    
    <form method="post">
    <table align="center" width="50px">
    	<tr>
    		<td>Cognome: <input type="text" name="cognome"></td>
    	</tr>
    	<tr>
    		<td>Password: <input type="password" name="password"></td>
    	</tr>
    	<tr>
    		<td><center><input type="submit" name="register" value="Registrati"></center></td>
    	</tr>
    </table>
    </form>
    
    </body>
    </html>
    Code (markup):
     
    gts, Jul 23, 2009 IP