3 Replies - 1333 Views - Last Post: 04 October 2009 - 04:14 AM Rate Topic: -----

#1 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

register and login script not working together

Post icon  Posted 04 October 2009 - 03:53 AM

i have just made a login script which uses sessions
and i have got a register script that dont use sessions


the register page works and adds what i want to the datebase
but then when i try and login it says Wrong Username or Password

if i add the info into the db by hand it lets me login
i need the login to use sessions i did have 1 that uses cookies ( but i need sessions)

register.php


<?php
// Connects to your Database
mysql_connect("mysql10.000webhost.com", "nick", "password") 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['pass'] | !$_POST['pass2'] ) {
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 members 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']);
}

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


<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.</p>
<?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="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><td>country:</td><td>
<input type="country" name="country" maxlength="30">
</td></tr>
<tr><td>gender:</td><td>
<input type="gender" name="gender" maxlength="30">
</td></tr>
<tr><td>date of birth:</td><td>
<input type="dateofbirth" name="dateofbirth" maxlength="30">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
</form>

<?php
}
?> 
<body>
<p><a href="login.php">Login</a></p>
<p>&nbsp;</p>
<p><a href="Registration.php">Reg </a></p>
<p><a href="log_out.php">Logout</a></p>
</body>
</html>



login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Login</title>
</head>
<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>
<body>
</body>
</html>




checklogin.php


<?php
$host="mysql10.000webhost.com"; // Host name
$username="nick"; // Mysql username
$password="pass"; // Mysql password
$db_name="a9020403_test"; // Database name
$tbl_name="test"; // 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";
}
?>



Is This A Good Question/Topic? 0
  • +

Replies To: register and login script not working together

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: register and login script not working together

Posted 04 October 2009 - 04:08 AM

first you need to assign your username and password to variables
change this code:
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);


To:
$password = addslashes($_POST['pass']);
$username = addslashes($_POST['username']);


then use it in your sql query

and also add mysql_die() to your query
...$add_member = mysql_query($insert)or die(mysql_error()); mysql_error() let you know to error when you are executing the query.

:)

This post has been edited by noorahmad: 04 October 2009 - 04:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: register and login script not working together

Posted 04 October 2009 - 04:10 AM

I think it is because you encrypt the password in the register script but when checking the username and password against whats in the database in your login script you dont encryt the password there so they will never match.

tommyflint
Was This Post Helpful? 1
  • +
  • -

#4 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: register and login script not working together

Posted 04 October 2009 - 04:14 AM

fixed :D

how can i make the d.o.b a drop down and the gender ?


<tr><td>gender:</td><td>
<input type="gender" name="gender" maxlength="30">
</td></tr>
<tr><td>date of birth:</td><td>
<input type="dateofbirth" name="dateofbirth" maxlength="30">






would this work for the d.o.b

 <label>
  <select name="dateofbirth">
   <option>1990</option>
   <option>1991</option>
   <option>1992</option>
   <option>1993</option>
   <option>1994</option>
   <option>1995</option>
   <option>1996</option>
   <option>1997</option>
   <option>1998</option>
   <option>1999</option>
   <option>2000</option>
   <option>2001</option>
   <option>2002</option>
   <option>2003</option>
   <option>2004</option>
  </select>
  </label>




got it to work now got drop downs for the gender and d.o.b :D im learning so fast lol

This post has been edited by nick1200: 04 October 2009 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1