Jump to content

Recommended Posts

I’ve got data in my database and the code acknowledges that the user is there but not the password :S

the error i get is Incorrect password, please try again. even tho its the right password

<?php
// Connects to your Database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("booze") or die(mysql_error());

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{
$Username = $_COOKIE['ID_my_site'];
$Pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM member WHERE Username = '$Username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($Pass != $info['Password'])
{
}
else
{
header("Location: members.php");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['Username'] | !$_POST['Pass']) {
die('You did not fill in a required field.');
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM member WHERE Username = '".$_POST['Username']."'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database.<a href=loginpage.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['Pass'] = stripslashes($_POST['Pass']);
$info['Password'] = stripslashes($info['Password']);
$_POST['Pass'] = md5($_POST['Pass']);

//gives error if the password is wrong
if ($_POST['Pass'] != $info['Password']) {
die('Incorrect password, please try again.');
}
else
{

// if login is ok then we add a cookie
$_POST['Username'] = stripslashes($_POST['Username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['Username'], $hour);
setcookie(Key_my_site, $_POST['Pass'], $hour);

//then redirect them to the members area
header("Location: members.php");
}
}
}
else
{

// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="Username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="Pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}

?>

thanks in advance!
Link to comment
https://forums.phpfreaks.com/topic/32421-solved-log-in-action/
Share on other sites

i dont quite understand that 2b honest heres my reg script can you point out what s needs to be dun?

<?php
include('connect1.inc');?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Booze Cruise Reg</title>

</head>
<body>

<?php
if(!$_POST['register']){echo'<p align="center"><strong>Member Registration</strong></p>
<form name="form1" method="POST" action="">
<p align="center">Username: <input type="text" name="username"></p>
<p align="center">Password: <input type="text" name="password"></p>
<p align="center">Firsname: <input type="text" name="firstname"></p>
<p align="center">Surname:  <input type="text" name="surname"></p>
<p align="center">Address 1:<input type="text" name="address1"></p>
<p align="center">Address 2:<input type="text" name="address2"></p>
<p align="center">Town:    <input type="text" name="town"></p>
<p align="center">County:  <input type="text" name="county"></p>
<p align="center">Postcode: <input type="text" name="postcode"></p>
<p align="center">Tel No:  <input type="text" name="telno"></p>
<p align="center">Mobile:  <input type="text" name="mobile"></p>
<p align="center">Email:    <input type="text" name="email"></p>
<p align="center"><input type="submit" name="register" value="Enter Details"></p></form>';}

else{$username=$_POST['username'];$password=$_POST['password'];$firstname=$_POST['firstname'];$surname=$_POST['surname'];$address1=$_POST['address1'];$address2=$_POST['address2'];$town=$_POST['town'];$county=$_POST['county'];
$postcode=$_POST['postcode'];$telno=$_POST['telno'];$mobile=$_POST['mobile'];$email=$_POST['email'];

$sql = "INSERT INTO member(Username,Password,Firstname,Surname,Address1,Address2,Town,County,Postcode,TelNo,Mobile,Email)
VALUES ('$username','$password','$firstname','$surname','$address1','$address2','$town','$county','$postcode','$telno','$mobile','$email')";

mysql_query($sql)             

or die(mysql_error());

echo("You are registered! $username");}?>

</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/32421-solved-log-in-action/#findComment-150593
Share on other sites

Oh.. well, just do this:

Replace:
[code]$sql = "INSERT INTO member(Username,Password,Firstname,Surname,Address1,Address2,Town,County,Postcode,TelNo,Mobile,Email)
VALUES ('$username','$password','$firstname','$surname','$address1','$address2','$town','$county','$postcode','$telno','$mobile','$email')";[/code]
With:
[code]$password = stripslashes($password);
$password = md5($password);

$sql = "INSERT INTO member(Username,Password,Firstname,Surname,Address1,Address2,Town,County,Postcode,TelNo,Mobile,Email)
VALUES ('$username','$password','$firstname','$surname','$address1','$address2','$town','$county','$postcode','$telno','$mobile','$email')";[/code]
Link to comment
https://forums.phpfreaks.com/topic/32421-solved-log-in-action/#findComment-150595
Share on other sites

so its here where it is checking the password

while($info = mysql_fetch_array( $check ))
{
$_POST['Pass'] = stripslashes($_POST['Pass']);
$info['Password'] = stripslashes($info['Password']);
$_POST['Pass'] = md5($_POST['Pass']);

//gives error if the password is wrong
if ($_POST['Pass'] != $info['Password']) {
die('Incorrect password, please try again.');
}

so what this is saying is while checking ($check)
get pass take away slashes, repost pass (now without slashes)
put into var info the password entered stripslashes repost in var without slashes
get pass ....MD5.... back into pass

but the next part is is saying if pass var = the info in var info is the same then die??

all the above maybe poo but im just trying to make sense of things
Link to comment
https://forums.phpfreaks.com/topic/32421-solved-log-in-action/#findComment-150604
Share on other sites

If password entered in form does not equal the password in the database then die();
If it does match, create the cookie and stuff.

[edit]No... no... if you use the encrypted password from the database it will only re-encrypt it and it won't match up. Use the original password you registered with[/edit]
Link to comment
https://forums.phpfreaks.com/topic/32421-solved-log-in-action/#findComment-150607
Share on other sites

Can you use this as your login script? Just to test it

[code]<?php

// Connects to your Database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("booze") or die(mysql_error());

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site'])) //if there is, it logs you in and directes you to the members page
{
$Username = $_COOKIE['ID_my_site'];
$Pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM member WHERE Username = '$Username'")or die(mysql_error());
$info = mysql_fetch_array( $check )
if ($Pass != $info['Password'])
{
}
else
{
header("Location: members.php");
}
}

//if the login form is submitted
if (isset($_POST['submit'])) // if form has been submitted
{

// makes sure they filled it in
if(!$_POST['Username'] || !$_POST['Pass'])
{
die('You did not fill in a required field.');
}

// checks it against the database
$check = mysql_query("SELECT * FROM member WHERE Username = '".$_POST['Username']."'") or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=loginpage.php>Click Here to Register[/url]');
}
$info = mysql_fetch_array( $check )
$_POST['Pass'] = stripslashes($_POST['Pass']);
$_POST['Pass'] = md5($_POST['Pass']);

//gives error if the password is wrong
if ($_POST['Pass'] != $info['Password'])
{
die('Incorrect password, please try again. Original Password = '.$_POST['Pass'].'. DB Password = '.$info['Password']);
}
else
{
// if login is ok then we add a cookie
$_POST['Username'] = stripslashes($_POST['Username']);
$hour = time() + 3600;
setcookie("ID_my_site", $_POST['Username'], $hour);
setcookie("Key_my_site", $_POST['Pass'], $hour);

//then redirect them to the members area
header("Location: members.php");
}
}
else
{

// if they are not logged in
?>
<form action="" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="Username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="Pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32421-solved-log-in-action/#findComment-150622
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.