Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    Problemi autenticazione php su db mysql

    Salve a tutti,da poco mi sono avvicinato a php e mysql.Volevo implementare un sistema semplice di login,tramite il quale un utente,una volta autenticatosi, accede a delle risorse.Visto che è la prima volta,utilizzo easyphp 5.2.10 . Quando cerco di autenticarmi tramite root,la query viene effettuata,ma la funzione mysql_num_rows mi ritorna 0 come parametro e quindi fallisce l'autenticazione. Cosa sto sbagliando? vi posto il codice in questione

    codice:
    <?php 		 	     function escapeString($string) 		{ 		  if(get_magic_quotes_gpc()) 		  { 		    $string = stripslshes($string); 			return mysql_escape_string($string); 		  } 		  else 		  { 		    return mysql_escape_string($string); 		  } 		} 		 		 		session_start();         echo "<div align=\"center\"> sessione utente partita </div>"; 		 		pagina dopo il login 		if(isset($_SESSION['login'])) 		{ 		  header("location:risorse.php"); 		} 		 		 		if(isset($_POST['submit']) && (trim($_POST['submit']) == 'Login')) 		{ 	 			  		 			if(!isset($_POST['username']) || ($_POST['username'])== "") 			{ 			  echo " 			   <div align=\"center\"> 					<table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\"> 						<tr> 							<td align=\"center\"> 							    
    
    <h2> Errore 001 : Devi inserire il nome utente </h2></p> 							</td> 						</tr> 					</table> 			   </div> 			  	 				"; 			} 			 	  			 			else 			{ 			 $username = trim(filter_var($_POST['username'],FILTER_SANITIZE_STRING)); 			 $password = trim(filter_var($_POST['password'], FILTER_SANITIZE_STRING)); 			 $password = escapeString($password); 			  			 $username = $_POST['username']; 			 echo $username." ".$password; 		   			 if($username == 'root') 			 { 			   echo "ok root"; 			   include 'config_root.php'; 			   $root = 1; 			 }              else 			 { 			    			   include 'config_utente.php'; 			   $root=0; 			 } 			  			  			 $sql = "SELECT * FROM utenti WHERE username='$username' and password ='$password'"; 			 			 			 $result = mysql_query($sql); 			  			 			 if($sql=FALSE) 			 { 			   echo"errore".mysql_error(); 			   echo "<div align=\"center\"> 					<table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\"> 						<tr> 							<td align=\"center\"> 							    
    
    <h2> Errore 003 : Username o Password errata </h2></p> 							</td> 						</tr> 					</table> 			   </div> "; 			    			 } 			 $num_righe = mysql_num_rows($result); 			 echo $num_righe; 			 if(!$num_righe) 			 { 			   echo " ".mysql_error(); 			   echo " 			   <div align=\"center\"> 					<table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\"> 						<tr> 							<td align=\"center\"> 							    
    
    <h2> Errore 004 : Username o Password errata</h2></p> 							</td> 						</tr> 					</table> 			   </div> 			  	 				"; 			  			 } 			 else 			 { 			   header("location:risorse.php"); 			 } 			 			} 		  	 		  		}
    Ho anche aggiunto degli utenti nella tabella utenti del database login creato da me,quindi una volta autenticato non dovrebbe tornarmi 0 come risultato di mysql_num_rows. Grazie a tutti in anticipo
    Giuly Sei La Mia Vita

  2. #2

    Re: Problemi autenticazione php su db mysql

    Riposta il codice formattato un po' meglio, così non si capisce una mazza

  3. #3
    Hai ragione,scusami . Ecco il codice.Premetto che nella mia tabella utenti ho già provveduto a creare l'utente root,senza alcun risultato

    <?php
    function escapeString($string)
    {
    if(get_magic_quotes_gpc())
    {
    $string = stripslshes($string);
    return mysql_escape_string($string);
    }
    else
    {
    return mysql_escape_string($string);
    }
    }
    session_start();
    echo "<div align=\"center\"> sessione utente partita </div>";

    if(isset($_SESSION['login']))
    {
    header("location:risorse.php");
    }
    if(isset($_POST['submit']) && (trim($_POST['submit']) == 'Login')) {
    if(!isset($_POST['username']) || ($_POST['username'])== "")
    {
    echo "
    <div align=\"center\">
    <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\"
    border=\"1\">
    <tr>
    <td align=\"center\">


    <h2> Errore 001 : Devi inserire il nomeutente </h2></p>

    </td> </tr>
    </table>
    </div>
    ";
    }
    else
    {

    $username = trim(filter_var($_POST['username'],FILTER_SANITIZE_STRING)); $password = trim(filter_var($_POST['password'], FILTER_SANITIZE_STRING)); $password = escapeString($password); $username = $_POST['username']; echo $username." ".$password; if($username == 'root') { echo "ok root"; include 'config_root.php'; $root = 1; } else { include 'config_utente.php'; $root=0; } $sql = "SELECT * FROM utenti WHERE username='$username' and password ='$password'"; $result = mysql_query($sql); if($sql=FALSE) { echo"errore".mysql_error(); echo "<div align=\"center\"> <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\"> <tr> <td align=\"center\">

    <h2> Errore 003 : Username o Password errata </h2></p> </td> </tr> </table> </div> "; } $num_righe = mysql_num_rows($result); echo $num_righe; if(!$num_righe) { echo " ".mysql_error(); echo " <div align=\"center\"> <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\"> <tr> <td align=\"center\">

    <h2> Errore 004 : Username o Password errata</h2></p> </td> </tr> </table> </div> "; } else { header("location:risorse.php"); } } }
    Codice PHP:
    <?php                   function escapeString($string)         {           if(get_magic_quotes_gpc())           {             $string stripslshes($string);             return mysql_escape_string($string);           }           else           {             return mysql_escape_string($string);           }         }                           session_start();         echo "<div align=\"center\"> sessione utente partita </div>";                  pagina dopo il login         if(isset($_SESSION['login']))         {           header("location:risorse.php");         }                           if(isset($_POST['submit']) && (trim($_POST['submit']) == 'Login'))         {                                         if(!isset($_POST['username']) || ($_POST['username'])== "")             {               echo "                <div align=\"center\">                     <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\">                         <tr>                             <td align=\"center\">                                 

    <h2> Errore 001 : Devi inserire il nome utente </h2></p>                             </td>                         </tr>                     </table>                </div>                                    "
    ;             }                                             else             {              $username trim(filter_var($_POST['username'],FILTER_SANITIZE_STRING));              $password trim(filter_var($_POST['password'], FILTER_SANITIZE_STRING));              $password escapeString($password);                            $username $_POST['username'];              echo $username." ".$password;                         if($username == 'root')              {                echo "ok root";                include 'config_root.php';                $root 1;              }              else              {                                include 'config_utente.php';                $root=0;              }                                          $sql "SELECT * FROM utenti WHERE username='$username' and password ='$password'";                                        $result mysql_query($sql);                                         if($sql=FALSE)              {                echo"errore".mysql_error();                echo "<div align=\"center\">                     <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\">                         <tr>                             <td align=\"center\">                                 

    <h2> Errore 003 : Username o Password errata </h2></p>                             </td>                         </tr>                     </table>                </div> "
    ;                              }              $num_righe mysql_num_rows($result);              echo $num_righe;              if(!$num_righe)              {                echo " ".mysql_error();                echo "                <div align=\"center\">                     <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\">                         <tr>                             <td align=\"center\">                                 

    <h2> Errore 004 : Username o Password errata</h2></p>                             </td>                         </tr>                     </table>                </div>                                    "
    ;                            }              else              {                header("location:risorse.php");              }                          }                                  }
    Giuly Sei La Mia Vita

  4. #4
    Hai ragione,scusami . Ecco il codice.Premetto che nella mia tabella utenti ho già provveduto a creare l'utente root,senza alcun risultato

    <?php

    function escapeString($string)
    {
    if(get_magic_quotes_gpc())
    {
    $string = stripslshes($string);
    return mysql_escape_string($string);
    }
    else
    {
    return mysql_escape_string($string);
    }
    }


    session_start();
    echo "<div align=\"center\"> sessione utente partita </div>";


    if(isset($_SESSION['login']))
    {
    header("location:risorse.php");
    }


    if(isset($_POST['submit']) && (trim($_POST['submit']) == 'Login'))
    {



    if(!isset($_POST['username']) || ($_POST['username'])== "")
    {
    echo "
    <div align=\"center\">
    <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\">
    <tr>
    <td align=\"center\">


    <h2> Errore 001 : Devi inserire il nome utente </h2></p>
    </td>
    </tr>
    </table>
    </div>

    ";
    }

    else
    {
    $username = trim(filter_var($_POST['username'],FILTER_SANITIZE_STRING));
    $password = trim(filter_var($_POST['password'], FILTER_SANITIZE_STRING));
    $password = escapeString($password);

    $username = $_POST['username'];
    echo $username." ".$password;

    if($username == 'root')
    {
    echo "ok root";
    include 'config_root.php';
    $root = 1;
    }
    else
    {

    include 'config_utente.php';
    $root=0;
    }


    $sql = "SELECT * FROM utenti WHERE username='$username' and password ='$password'";

    $result = mysql_query($sql);
    if($sql=FALSE)
    {
    echo"errore".mysql_error();
    echo "<div align=\"center\">
    <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\">
    <tr>
    <td align=\"center\">


    <h2> Errore 003 : Username o Password errata </h2></p>
    </td>
    </tr>
    </table>
    </div> ";

    }
    $num_righe = mysql_num_rows($result);
    if(!$num_righe)
    {
    echo " ".mysql_error();
    echo "
    <div align=\"center\">
    <table width=\"400\" height=\"100\" cellpadding=\"10\" cellspacing=\"0\" border=\"1\">
    <tr>
    <td align=\"center\">


    <h2> Errore 004 : Username o Password errata</h2></p>
    </td>
    </tr>
    </table>
    </div>

    ";

    }
    else
    {
    header("location:risorse.php");
    }

    }

    }
    Giuly Sei La Mia Vita

  5. #5
    if($sql=FALSE)
    Hai messo un = in meno, così com'è ASSEGNI false a $sql...

    if($sql==FALSE)

    e sei a posto.
    Altra cosina...tu fai
    Codice PHP:
    $username trim(filter_var($_POST['username'],FILTER_SANITIZE_STRING)); 
    ma subito dopo ri scrivi con
    Codice PHP:
    $username $_POST['username']; 
    e quindi rendi inutile la prima istruzione e apri il sistema a potenziali sql injections


  6. #6
    Grazie Dascos per la risposta . Ho risolto aggiungendo il segno di "=" dove mancava e togliendo la spunta null nel campo password nell'utente root,che avevo creato tramite pannello phpmyadmin all'interno della mia tabella,per questo non riuscivo ad accedere alla tabella .
    e grazie
    Giuly Sei La Mia Vita

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.