Jump to content

login form: it logs in no matter what and it shouldnt, please help


rich___

Recommended Posts

<?php

// allows session info to be used on this page

session_start();

 

// if this script isn't receiving form data, exit fast

if(!isset($_POST['btnLogin']))

{ header("Location: login.htm");

session_write_close();

exit();

}

 

 

if(empty($_POST[txtPassword]) || empty($_POST[txtUsername])) // if empty fields, give error

 

msg

{

  echo ('please enter in both fields');

}

 

else

 

// gets username and password as typed into the login form

{

  $pass = $_POST['txtPassword'];

  $user = $_POST['txtUsername'];

}

 

$sEncryptedPassword = crypt($pass);

 

// creates a new connection object

$adoCon = new COM("ADODB.Connection");

 

// the path to the folder holding this PHP script

$sHere = dirname(__FILE__);

 

// opens the connection using a standard connection string

try

{

$adoCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;

Data Source=C:\\xampp\\htdocs\\xampp\\db1.mdb");

}

 

catch(Exception $e)

{

echo('Sorry - There was a problem with opening the database.<br />');

}

 

 

 

$sSQL = "SELECT * FROM tblUsers WHERE userName='$user' AND userPass = '$sEncryptedPassword';";

$rsMain = $adoCon->Execute( $sSQL );

 

 

 

if(crypt($pass, $sEncryptedPassword) == ($sEncryptedPassword))

{ // if logged on okay, remembers user's name as session variable

$_SESSION['user'] = $user;

header("Location: protected.php");

session_write_close();

exit();

}

 

else

 

{

header("Location: login.htm");

session_write_close();

}

 

 

 

// closes the connection, frees up resources

$adoCon->Close();

$adoCon = null;

 

 

 

?>

 

what its meant to do is to compare the password (then encryped) to the encrypted pass in the database, and if their the same, then login, else go back to the login page, but whatever happens it goes to the protected page.

Link to comment
Share on other sites

ok

 

you need to do a query on your database with the username and password, use google for the help. Then once you have done the query you need to make sure the result you get back = 1. This means there is one record in your database and therefore the user is registered. If the result = 0 then the user is not registered and therefore can't view the page.

Link to comment
Share on other sites

You never actually check anything against your db results. In fact you don't use the results at all.

 

What he meant is you have queried the database but have not used the results

<?php 
// your compared your post values from the DB 
$sSQL = "SELECT * FROM tblUsers WHERE userName='$user' AND userPass = '$sEncryptedPassword';";
$rsMain = $adoCon->Execute( $sSQL );
// after that you did nothing.... ..... here you need to allow or deny the login based on the result 
if ($rsMain) {
echo "you are valid user";
} else {
echo "you are invalid user";
}

?>

 

and you are missing quote in this line too.

if(empty($_POST['txtPassword']) || empty($_POST['txtUsername']))

Link to comment
Share on other sites

<?php
// allows session info to be used on this page
session_start();

// if this script isn't receiving form data, exit fast
if(!isset($_POST['btnLogin']))
{	header("Location: login.htm");		
	session_write_close();
	exit();
}


if(empty($_POST['txtPassword']) || empty($_POST['txtUsername'])) // if empty fields, give 

error msg
{
   echo ('please enter in both fields');
}

else

// gets username and password as typed into the login form
{ 
   $pass = $_POST['txtPassword'];
   $user = $_POST['txtUsername'];
}

$sEncryptedPassword = crypt($pass);

// creates a new connection object
$adoCon = new COM("ADODB.Connection");

// the path to the folder holding this PHP script
$sHere = dirname(__FILE__); 

// opens the connection using a standard connection string
try
{ 
$adoCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\\xampp\\htdocs\\xampp\\db1.mdb");
}

catch(Exception $e)
{	
echo('Sorry - There was a problem with opening the database.<br />');
}



$sSQL = "SELECT * FROM tblUsers WHERE userName='$user' AND userPass = '$spass';";
$rsMain = $adoCon->Execute( $sSQL );

// only checks the password if the user exists
//if( isset($aValidUsers[$user]) )
// checks to see if the username/password pair is valid by encrypting
// the password and comparing against the real encrypted password

if (isset($rsMain)) {	


if(crypt($pass, $sEncryptedPassword) == ($sEncryptedPassword))
{	// if logged on okay, remembers user's name as session variable
	$_SESSION['user'] = $user;		
	header("Location: protected.php");
	session_write_close();
	exit();
}		
else	

{
header("Location: login.htm");
session_write_close();
}
header("Location: login.htm");
session_write_close();
}







// closes the connection, frees up resources
$adoCon->Close();
$adoCon = null;


?>


Link to comment
Share on other sites

sry mate uve lost me. thers nothing i can show u but the code, unless u tell me to try something can i can post up errors if they appear. i know whats wrong with it, i dont use the query to my advantage, but i know what i would like to do, which is compare the user name and pass from the db to whats been entered, and if its correct then goto protected.php, othewise fail it, but ive no idea where to begin to start coding that, any ideas?

Link to comment
Share on other sites

now you confused me too lol

<?php
session_start();
$pass= 'mypassword';
$sEncryptedPassword = crypt('mypassword');
if(crypt($pass, $sEncryptedPassword) == ($sEncryptedPassword))
{	echo "Password verified!";

//	$_SESSION['user'] = $user;		
//	header("Location: protected.php");
//	session_write_close();
//	exit();
}		
else{
//	header("Location: login.htm");
//	session_write_close();
echo  'failed';
}
?>

 

try that code first so you can have a good start..

Link to comment
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.