Jump to content

PHP login wont work


ristic

Recommended Posts

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

 

2n2hit.jpg

 

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.

Link to comment
Share on other sites

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 by ristic
Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.