Good evening,
I have XAMPP setup on my home PC and i am learning to build my own websites. I have setup php scripting for user accounts.
I have come accross a problem of which i have no idea how to solve. I will always attempt to solve issues on my own but i cant fathom what this is....
I have my login/register php scripts all setup.
I can register absolutely fine and my SQL database is updated with the registration details (username, password, email, phone, town).
My issue is that when i try and login to the user account that i register with it sends me to my login_fail.php page and i cant understand why its doing this. I think it may be due to my ELSE statement at the bottom of the checklogin?
My checklogin.php script is shown below:
<?php
// setting the variables
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // 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
$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 main login
session_start();
$_SESSION["loggedin"] = 'true';
header("location: main_login.php");
die();
}
else {
header("location: login_fail.php");
die();
}
?>
My register.php is below:
<?php
// Connects to your Database
mysql_connect("localhost", "root", "") or
die(mysql_error());
mysql_select_db("test") or die(mysql_error());
//This code runs if the form has been submitted
if (isset($_POST['submit']))
//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['password'] | !$_POST['email'] | !$_POST['phone'] | !$_POST['town'] ) {
header("location: login_fail.php");
die();
}
// checks if the email is in use
if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$emailcheck = $_POST['email'];
$check = mysql_query("SELECT email FROM members WHERE email = '$emailcheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
header("location: login_fail.php");
die();
}
// here we encrypt the password and add slashes if needed
$_POST['password'] = md5($_POST['password']);
if (!get_magic_quotes_gpc()) {
$_POST['password'] = addslashes($_POST['password']);
$_POST['username'] = addslashes($_POST['username']);
}
// now we insert it into the database
$insert = "INSERT INTO members (username, password, email, phone, town)
VALUES ('".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['phone']."', '".$_POST['town']."')";
$add_member = mysql_query($insert);
header("location: register_success.php");
die();
?>
My login table (dont know if its of any use?
<table width="300" border="0" margin-bottom= "5%" align="left" cellpadding="0" cellspacing="1" bgcolor="none">
<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>
In my SQL database the passwords are encrypted still.
If i change a user password in mySQL i can login fine, but just after registration (using the original credentials) i cant login...
Any ideas?
Much appreciated.
Stan.