ristic Posted June 26, 2013 Share Posted June 26, 2013 Hi, i've got a problem which i cant find a way around it, tho i think everything is OK with my php code, so i need an opinion if anyone can spot something... Anyway, this is my php code: <?php session_start(); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Logovanje</title> </head> <body> <?php if (isset($_POST['prijaviSe'])) { include('otvori_konekciju.php'); $korisnickoIme = $_POST['korisnickoIme']; $lozinka = md5($_POST['lozinka']); $rezultat = mysql_query("SELECT * FROM korisnik WHERE korisnicko_ime = '$korisnickoIme' AND lozinka = '$lozinka'"); include('zatvori_konekciju.php'); if ($rezultat && mysql_num_rows($rezultat) > 0) { $red = mysql_fetch_assoc($rezultat); $_SESSION['id'] = $red['id']; $_SESSION['korisnickoIme'] = $red['korisnicko_ime']; header('Location: index.php'); } else { echo '<span style="color: red;">Korisnicko ime ili lozinka nisu ispravni.</span><hr />'; } } ?> <form action="logovanje.php" method="POST"> <table> <tr> <td>Korisnicko ime:</td> <td><input type="text" name="korisnickoIme" /></td> </tr> <tr> <td>Lozinka:</td> <td><input type="password" name="lozinka" /></td> </tr> </table> <hr /> <input type="submit" value="Prijavi se" name="prijaviSe" /> </form> </body> </html> and my table with 2 usernames, admin and test, with their password in next column as u can see, in my php code, my entered password is hashed and then compared with password in the table...but when i type in my login window : "test" , "test"; it prints me out the error message...that's why i changed admins pass to 'admin' (before it was some random hash code like the password for test) and it's still not working...i really don't know what's the problem, and because of that i cant continue my work....Can anyone help? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/279601-php-login-wont-work/ Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 echo some information during the whole process so we can see what is happening. Be sure to echo out the value of the username and the password that you are using in your query. Quote Link to comment https://forums.phpfreaks.com/topic/279601-php-login-wont-work/#findComment-1438028 Share on other sites More sharing options...
ristic Posted June 26, 2013 Author Share Posted June 26, 2013 (edited) i wrote for input "test" for username and "test" for password, and the values it echoed are the same like those in the table: test 098f6bcd4621d373cade4e832627b4f6 i have no idea why the IF part wont go through, seems like that the condition in IF( ) is not TRUE....and it is jumping to ELSE part... Edited June 26, 2013 by ristic Quote Link to comment https://forums.phpfreaks.com/topic/279601-php-login-wont-work/#findComment-1438081 Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 Can I see the code with the echo statements added? Add some extra echos to show how you proceeded through the code. Quote Link to comment https://forums.phpfreaks.com/topic/279601-php-login-wont-work/#findComment-1438092 Share on other sites More sharing options...
ristic Posted June 26, 2013 Author Share Posted June 26, 2013 <?php session_start(); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Logovanje</title> </head> <body> <?php if (isset($_POST['prijaviSe'])) { include('otvori_konekciju.php'); $korisnickoIme = $_POST['korisnickoIme']; $lozinka = md5($_POST['lozinka']); echo "<p>$korisnickoIme"; // this does get written echo "<p>$lozinka <p>"; $rezultat = mysql_query("SELECT * FROM korisnik WHERE korisnicko_ime = '$korisnickoIme' AND lozinka = '$lozinka'"); include('zatvori_konekciju.php'); if ($rezultat && mysql_num_rows($rezultat) > 0) { $red = mysql_fetch_assoc($rezultat); $_SESSION['id'] = $red['id']; echo "This is something random in IF part <p>"; // this does NOT get written $_SESSION['korisnickoIme'] = $red['korisnicko_ime']; header('Location: index.php'); } else { echo "ELSE part of the code"; // written echo "<p>$korisnickoIme"; echo "<p>$lozinka <p>"; echo '<span style="color: red;">Korisnicko ime ili lozinka nisu ispravni.</span><hr />'; } } ?> <form action="logovanje.php" method="POST"> <table> <tr> <td>Korisnicko ime:</td> <td><input type="text" name="korisnickoIme" /></td> </tr> <tr> <td>Lozinka:</td> <td><input type="password" name="lozinka" /></td> </tr> </table> <hr /> <input type="submit" value="Prijavi se" name="prijaviSe" /> </form> </body> </html> output: test 098f6bcd4621d373cade4e832627b4f6 ELSE part of the code test 098f6bcd4621d373cade4e832627b4f6 Korisnicko ime ili lozinka nisu ispravni. Quote Link to comment https://forums.phpfreaks.com/topic/279601-php-login-wont-work/#findComment-1438097 Share on other sites More sharing options...
ginerjm Posted June 27, 2013 Share Posted June 27, 2013 Do this - replace this line: echo "ELSE part of the code"; // written with: echo "ELSE part of the code - error message is: ".MySQL_error(); Your query is not returning a result OR it completely failed. The 'MySQL_error()' will tell us which one it is. Quote Link to comment https://forums.phpfreaks.com/topic/279601-php-login-wont-work/#findComment-1438190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.