I'm trying to make a log in form for my website where the username and password are already stored within the table user in mydb. I have created checklogin.php to create a connection from my website to mydb and then check the credentials for the log in.
This is the code for checklogin.php:
<?php
$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name
//Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die(mysql_error());
//User name and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
if($count==1)
{ // register $myusername, $mypassword and redirect file to loginSuc.php
session_register("myusername");
session_register("mypassword");
header("location:loginSuc.php");
}
else
{
echo "wrong username or password";
}
?>
Now I've added the missing component to the previous code, when I run I am now getting errors pointing to lines 13, 14 and 19.
The errors are:
Notice: Undefined index: myusername in C:\wamp\www\HTML 5, CSS 3 & PHP\checklogin.php on line 13/14/19 with the string "wrong username or password" appearing underneath
Once again stuck as to how to fix this -- FIXED
Fixed the errors with lines 13,14 and 19 by adding some extra code. Have now got error:
"Parse error: syntax error, unexpected T_IF in C:\wamp\www\HTML 5, CSS 3 & PHP\checklogin.php on line 27"
<?php
$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name
//Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die(mysql_error());
//User name and password sent from form
$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($count==1)
{ // register $myusername, $mypassword and redirect file to loginSuc.php
session_register("myusername");
session_register("mypassword");
header("location:loginSuc.php");
}
else
{
echo "wrong username or password";
}
?>
ABOVE FIXED
right so now ive got the checkilogin.php bringing up no errors im now using:
<?php
$host="127.0.0.1:3306"; // host name
$username="root"; // mysql usrname
$password=""; // mysql password
$db_name="mydb"; // db name
$tbl_name="user"; // table name
//Connect to server and select databasemysql_connect
mysql_connect($host, $username, $password, $db_name) or die(mysql_error());
if($_POST)
{
$myusername=$_POST["myusername"];
$mypassword=$_POST["mypassword"];
$sql="SELECT * FROM ".$tbl_name." WHERE myusername=".$myusername." and mypassword=".$mypassword;
$result=mysql_query($sql);
// register $myusername, $mypassword and redirect file to loginSuc.php
session_register("myusername");
session_register("mypassword");
header("location:loginSuc.php");
}
else
{
echo "wrong username or password";
}
?>
But I'm still getting issues. Whenever I try to log in through the form in my loginpage.html, it goes to checklogin.php but checklogin.php simply shows "wrong password or username". I get this when I press the submit button with no username or password, when I have put the incorrect username and password and even when I use the username and password that I have put as an example in the user table in mydb. (In the user table I have userid, username and password fields).
Here is the php at the beginning of my loginpage.html:
<?php
session_start();
if(!session_is_registered(myusername))
{
header("location:checklogin.php");
}
?>
and here is the form (i used html5 and twitter bootstrap grid code):
<form class="form-horizontal" method="post" action="checklogin.php">
<div class="form-group">
<label for="myusername" class="control-label col-xs-3">Username</label>
<div class="col-xs-6">
<input type="text" class="form-control" id="myusername" placeholder="Library card number">
</div>
</div>
<div class="form-group">
<label for="mypassword" class="control-label col-xs-3">Password</label>
<div class="col-xs-6">
<input type="password" class="form-control" id="mypassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-24">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</form>