ok I have my register.php file obviously i blanked out my username and pass the first code is my register.php code
and ill add my login.php code as well but it doesnt work it always says wrong pass or username no matter what

my register.php works perfectly fine I just posted the code anyway

<?php // Connects to your Database 
 mysql_connect("", "", "") or die(mysql_error()); 
 mysql_select_db("") 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['pass'] | !$_POST['pass2'] | !$_POST['email']  ) {
 		die('You did not complete all of the required fields');
 	}
 
 // checks if the username is in use
 	if (!get_magic_quotes_gpc()) {
 		$_POST['username'] = addslashes($_POST['username']);
 	}
 $usercheck = $_POST['username'];
 $check = mysql_query("SELECT username FROM User WHERE username = '$usercheck'") 
 or die(mysql_error());
 $check2 = mysql_num_rows($check);
 
 //if the name exists it gives an error
 if ($check2 != 0) {
 		die('Sorry, the username '.$_POST['username'].' is already in use.');
 				}
 // this makes sure both passwords entered match
 	if ($_POST['pass'] != $_POST['pass2']) {
 		die('Your passwords did not match. ');
 	}
 
 	// here we encrypt the password and add slashes if needed
 	$_POST['pass'] = md5($_POST['pass']);
 	if (!get_magic_quotes_gpc()) {
 		$_POST['pass'] = addslashes($_POST['pass']);
 		$_POST['username'] = addslashes($_POST['username']);


 			}

 // 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 User WHERE email = '$emailcheck'") 
 or die(mysql_error());
 $check2 = mysql_num_rows($check);
 
 //if the name exists it gives an error
 if ($check2 != 0) {
 		die('Sorry, the email '.$_POST['email'].' is already in use.');
 	} 

// now we insert it into the database
 	$insert = "INSERT INTO User (username, password, email)
 			VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['email']."')";
 	$add_member = mysql_query($insert);
 	?>

 <?php } 
 else {	 ?>
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <table border="0">
 <tr><td>Username:</td><td>
 <input type="text" name="username" maxlength="20">
 </td></tr>
 <tr><td>Password:</td><td>
 <input type="password" name="pass" maxlength="8">
 </td></tr>
 <tr><td>Confirm Password:</td><td>
 <input type="password" name="pass2" maxlength="8">
 </td></tr>
 <tr><td>Email:</td><td>
 <input type="text" name="email" maxlength="30">
 </td></tr>
 <tr><th colspan=2><input type="submit" name="submit" 
value="Register"></th></tr> </table>
 </form>
 <?php
 } ?> 

 <h1>Registered</h1>
 <p>Thank you, you have registered - you may now login</a>.
<a href="http://getagripsite.com>Return to home click here</a></p>

ok now here is my login.php code and it does not work can anyone help me?

<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>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

register.php includes a form that targets itself via <?php echo $_SERVER; ?>, so that on refresh, the script (with postdata loaded) runs the registration. Your login script, according to what you've provided here, is just a form that does nothing. You need some kind of session control in there in order for anything to happen.

Check out a tutorial like http://www.developertutorials.com/tutorials/php/php-sessions-login-script-050620-1094/ to understand what needs to happen.

Member Avatar for P0lT10n

But show us the code of checklogin.php...
check if you used myusername or username and the same for password...

Firstly crosscheck whether register entry is done in database table or not?
Dude your php coding is in checklogin.php. So post that code...

and registering works fine, once its done I go into my database as it has the records of the user password and email ya know so registering works perfectly fine

sorry about that, here is my checkloing.php

<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // 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 "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

can you debug using below code?

echo $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; exit;
$result=mysql_query($sql);

post your query in phpmyadmin sql tab, and see what you are getting.
post ur try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.