Page 1 of 1

Problem with login or validation

Posted: Wed Feb 12, 2014 4:29 pm
by SergioPerez
Hi All ! I'm Tom and i'm 14 years old, also I'm beggining my php adventure with website. I've got proplem which I can't solve.
Here is my login.html http://jsfiddle.net/M87Pm/2/
I've made some echos to see what is going on but nothing has been sawn.
If anybody could help me ? Thanks for all advice, answers and clues.

Here is my validation script.

Code: Select all



<?php

ob_start();

$host="correct"; // Host name
$username="correct"; // Mysql username
$password="correct"; // Mysql password
$db_name="correct"; // Database name

$tbl_name="User"; // Table name


// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");


// Define $myusername and $mypassword

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];


// To protect MySQL injection (more detail about MySQL injection)

$myusername = stripslashes($myusername);

$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);

$mypassword = mysql_real_escape_string($mypassword);


$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);


// Mysql_num_row is counting table row

$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row


if($count==1){


// Register $myusername, $mypassword and redirect to file "login_success.php"


$_SESSION['myusername']="myusername";

$_SESSION['mypassword']="mypassword";


header("location:login_success.php");

}

else {

echo "Niepoprawny login lub hasło";

echo $myusername;

echo $mypassword;

}


ob_end_flush();

?> 

Re: Problem with login or validation

Posted: Wed Feb 12, 2014 4:36 pm
by Celauran
Your form has neither action nor method, and there doesn't appear to be any JS attached to handle that either. $_POST is probably an empty array. Change the link to a submit button, add action and method to your form, and proceed from there.

Code: Select all

<form id="signup" action="checklogin.php" method="post">
	<div class="header">
		<h3>Logowanie</h3>
	</div>

	<div class="sep"></div>

	<div class="inputs">
		<input type="myusername" placeholder="Login" autofocus />
		<input type="mypassword" placeholder="Hasło" />
		<input type="submit" id="Submit" value="Zaloguj">
	</div>
</form>

Re: Problem with login or validation

Posted: Wed Feb 12, 2014 4:38 pm
by Celauran
A few other notes:
You're using mysql_query, which is deprecated, throws errors in recent versions of PHP, and is slated for removal. Especially if you're just getting started, don't learn bad habits. Take a look at PDO instead.
Your query suggests that you're storing your passwords in plain text. Don't do that. Hash them using bcrypt, hash the password provided in the login form, and compare the hashes.

Re: Problem with login or validation

Posted: Wed Feb 12, 2014 4:49 pm
by SergioPerez
So I should do something like this ?

Code: Select all

 <a id="Submit" name="form1" href="checklogin.php">         
      Zaloguj
            </a>  
What about javascript? Could you give me simple example or tutorial ?

Re: Problem with login or validation

Posted: Wed Feb 12, 2014 6:13 pm
by Celauran
What about the example I posted above? Is that not working either?

Re: Problem with login or validation

Posted: Wed Feb 12, 2014 6:46 pm
by SergioPerez
Yes, looks perfect, but after writing login and password (chcecked with database, just in case), script still reject it and still the is no echo from it. I think it could be the problem with names compatibility in verificator and login. What are you think about it ?

Re: Problem with login or validation

Posted: Wed Feb 12, 2014 8:54 pm
by SergioPerez
Here is how does it look like http://jsfiddle.net/3BGD4/ focus on button. In reality button submit should looks like this http://jsfiddle.net/mplungjan/HKyWY/. On my website panel looks like in the first link with button Zaloguj from second link. Maybe this is problem ?

Re: Problem with login or validation

Posted: Wed Feb 12, 2014 9:10 pm
by Celauran
The action on the two forms is different. The second form uses a JS listener to submit the form, whereas the first uses a submit button. Both can work.

Something like this should be fine assuming your validation script is actually located at checklogin.php

Code: Select all

<body>
    <div class="container">
        <form id="signup" action="checklogin.php" method="post">
            <div class="header">
                 <h3>Logowanie</h3>

            </div>
            <div class="sep"></div>
            <div class="inputs">
                <input type="text" name="myusername" placeholder="Login" autofocus>
                <input type="password" name="mypassword" placeholder="Has&#322;o">
                <input type="submit" id="Submit" value="Zaloguj">
            </div>
        </form>
    </div>
</body>
Something like this?