Jump to content

[SOLVED] Redundant login check?


soycharliente

Recommended Posts

Is this double check necessary or is it just redundant? I've been using this code for ages now and never really noticed it.

 

<?php
if ($_POST["Submit"] == "Login")
{
foreach ($_POST as $key => $val) { $_POST[$key] = myEscape($val); }
$un = $_POST['Username'];
$pw = md5($_POST['Password']);
dbconnect();
$sql = "SELECT * FROM `users` WHERE `username`='{$un}' AND `password`='{$pw}'";
$result = mysql_query($sql) OR DIE ("Unable to validate login.");
if (mysql_num_rows($result) > 0)
{
	$r = mysql_fetch_assoc($result);
	$user = $r['username'];
	$pass = $r['password'];
	if ($un == $user && $pw == $pass) //THIS CHECK RIGHT HERE
	{
		// do stuff
	}
}
dbclose();
}
?>

Link to comment
Share on other sites

<?php
function myEscape($string)
{
dbconnect();
$new = get_magic_quotes_gpc() ? stripslashes($string) : $string;
$safe = mysql_real_escape_string($new);
dbclose();
return $safe;
}
?>

 

So I could just have this and it would work jsut the same?

<?php
if ($_POST["Submit"] == "Login")
{
   foreach ($_POST as $key => $val) { $_POST[$key] = myEscape($val); }
   $un = $_POST['Username'];
   $pw = md5($_POST['Password']);
   dbconnect();
   $sql = "SELECT * FROM `users` WHERE `username`='{$un}' AND `password`='{$pw}'";
   $result = mysql_query($sql) OR DIE ("Unable to validate login.");
   if (mysql_num_rows($result) > 0)
   {
      // do stuff
   }
   dbclose();
}
?>

 

Link to comment
Share on other sites

That will work.

 

I recommend not opening and closing the database connection for each operation. Your code will run significantly faster if you just open a connection at the start of the script and close it at the end or let it be closed automatically when the script ends.

 

If you have 5 $_POST variables, the posted code is connecting - dbconnect(); and disconnecting - dbclose(); 6 times.

Link to comment
Share on other sites

I sometimes run myEscape outside the dbconnect. I don't remember why. I had to do it that way because I had to be connected to run the function. I guess I should just move it inside  the dbconnect() and use it that way from now on.

 

That makes a lot of sense. Thanks.

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.