Good evening! I believe I might have a problem with my php setup. I installed XAMPP and at the same time added mysql. I have taken code from website http://www.phpeasystep.com/phptu/6.html setting up the mysql database, the main_login.php, checklogin.php (with user and password info added), login_success.php, and checklogin.php through cut and paste. I am assuming that the code is correct. I saved them into a folder C:\xampp\htdocs\UnnamedSite2. When I attempt to run the code, after entering the user name and password, I get the following:
Notice: Undefined index: myusername in C:\xampp\htdocs\UnnamedSite2\checklogin.php on line 14
Notice: Undefined index: mypassword in C:\xampp\htdocs\UnnamedSite2\checklogin.php on line 15 Wrong Username or Password
What might be the problem?
main_login.php
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
checklogin.php
<?php
$host="localhost"; // Host name
$username="web"; // Mysql username
$password="foo.bar"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // 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");
// username 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 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_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
login_success.php
<html>
<body>
Login Successful
</body>
</html>
Logout.php
<?php
session_start();
session_destroy();
?>
<form></form>tags outside of<table></table>. It's easy to make mistake if you mix the form tags with the table elements.