Forum Moderators: coopster

Message Too Old, No Replies

Login counter

Login counter,auto delete user

         

JuicyScript

4:03 am on Oct 8, 2009 (gmt 0)

10+ Year Member



can some one help me with the following problem.Am tryin to track the number of times a user logs in to my site and automatically delete the user/ban him, when he logs in the third time.I dont knw how the ip/cookie fin works but what if the user loggs in with another ip the second and third time.

i have somefin like this
$Limit = counter;

mysql_query("DELETE FROM table WHERE counter >= '$limit'") or die(mysql_error());

i dont knw if it makes sense...Am new to php and i need help please

TheMadScientist

6:48 am on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you want to ban them when they actually log in, or on failed login attempts?

Also, do you want to ban the actual user, or do you want to ban the IP address of someone trying to login to an account and failing 3 times in a row?

NOTE: My personal thought is: I'm glad they don't do any of the preceding here at WebmasterWorld, because sometimes I'm a bit lazy and I forgot the login information for this account, so I used my old one for quite a while before I went to the trouble of trying to access this one again, and you might not be seeing this post if they had banned me after the 3rd failed login attempt to an account from an IP, because I used a really old password for this account and tried probably 30 times with different variations of the password I normally use now before I finally had to request my password and hope it came to an e-mail address that was still active to get in. Since you have actually tried what you are requesting with some code I'll see if I can help you through it if it's what you really want to do, but I strongly advise against it, unless there is some reason for what you want I am not seeing.

TheMadScientist

7:11 am on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sometimes when I ask questions I think of an answer a few minutes later...
Here's how to ban someone on the 3rd login:

1.) Set a col in your database called logins.
(Should be and INT(1) NOT NULL default 0)

2.) When a user logs in set the col to the current value +1.

3.) When the user accesses the site, check username, password, login count.

Here's what a portion of the code looks like:
When the visitor tries to access get username,password,logins:

$sql="SELECT username,password,logins FROM table WHERE username='$username'";
$qry=mysql_query($sql);
$result=mysql_fetch_array($qry);

/* check the total number of logins */
if($result['logins']==3) {
$TooManyLogins="Yes"
}

else {
$sql2="UPDATE table SET logins=logins+1";
mysql_query($sql2);
}

if($username===$result['username'] && $password===$result['password']) {
if($TooManyLogins==="Yes") {
echo "Your Logins have expired";
exit();
}

else {
The rest of your code here.
}
}

JuicyScript

10:46 am on Oct 8, 2009 (gmt 0)

10+ Year Member



Thanks MadScientist
This is exactly what i need.Am using it for a online voting process that is going to be held in my school.We generate a username and password for the user.Then he goes ahead and login to cast his vote.Thanks a million i will go ahead and try the code now.

JuicyScript

6:57 am on Oct 9, 2009 (gmt 0)

10+ Year Member



For some reason my script does not work can you help me PLEASE madscientist
<?
session_start();
session_destroy();
?>

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

checklogin.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="logins"; // Database name
$tbl_name="logins"; // 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
$username=$_POST['username'];
$password=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

//$sql1="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
//$result1=mysql_query($sql1);

// Mysql_num_row is counting table row
//$count=mysql_num_rows($result1);
// If result matched $myusername and $mypassword, table row must be 1 row

$sql="SELECT username,password,logins FROM $tbl_name WHERE username='$username'and logins='$logins'";
$query=mysql_query($sql);
$result = MYSQL_QUERY($query);

/* check the total number of logins */
if($result['$logins']==3) {
$TooManyLogins="Yes";
}

else {
$sql2="UPDATE table SET $logins=$logins+1";
mysql_query($sql2);
}

if($username===$result['username'] && $password===$result['password']) {
if($TooManyLogins==="Yes") {
echo "Your Logins have expired";
exit();
}

else {
if($logins==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
}
}
?>

TheMadScientist

9:44 am on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This should get you closer... It's late out here and I'm on my way to bed, so I didn't explain very well and there might be a slight error or two, but I restructured a bit and corrected some minor errors, so it should get you closer.

// You don't have the number of logins yet, so you cannot select where logins = $logins.

$sql="SELECT username,password,logins FROM $tbl_name WHERE username='$username'";
$query=mysql_query($sql);

/* mysql_fetch_array is what I usually use. What you had won't work. */

$result = mysql_fetch_array($query);

/* check the total number of logins :: Removed the $ from logins below. Should be a string. Let's check to see if they're the right user before we do anything with logins... makes more sense. */

if($username===$result['username'] && $password===$result['password']) {

/* If logins is greater than or = 3 is a bit safer. */
if($result['logins']>=3) {
echo "Your Logins have expired";
exit();
}

else {
$sql2="UPDATE $tbl_name SET $logins=$logins+1";
mysql_query($sql2);

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:login_success.php");
}
}

else {
echo "Wrong Username or Password";
}

JuicyScript

6:57 pm on Oct 9, 2009 (gmt 0)

10+ Year Member



One last thing...Everything is working alryt except this script
if($result['logins']>=3) {
echo "Your Logins have expired";
exit();
}
And the mysql_fetch_array gives me an error"
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program_Files\wamp\www\Votingsystem\login-exec.php on line 59
Query failed"

//Create query
$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($qry);

/* If logins is greater than or = 3 is a bit safer. */
if($result['logins']>=3) {
echo "Your Logins have expired";
exit();
}else {
$sql2="UPDATE members SET logins=logins+1 WHERE login like '$login'" ;
mysql_query($sql2); }
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
session_write_close();
header("location: member-index.php");
exit();
}else {
//Login failed
header("location: login-failed.php");
exit();
}
}else {
die("Query failed");
}
?>

TheMadScientist

11:22 pm on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One last thing...Everything is working alryt except this script
if($result['logins']>=3) {
echo "Your Logins have expired";
exit();
}
And the mysql_fetch_array gives me an error"
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program_Files\wamp\www\Votingsystem\login-exec.php on line 59
Query failed"

//Create query
$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($qry);

/* If logins is greater than or = 3 is a bit safer. */
if($result['logins']>=3) {
echo "Your Logins have expired";
exit();
}else {
$sql2="UPDATE members SET logins=logins+1 WHERE login like '$login'" ;
mysql_query($sql2); }
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
session_write_close();
header("location: member-index.php");
exit();
}else {
//Login failed
header("location: login-failed.php");
exit();
}
}else {
die("Query failed");
}

1.) The error means you tried to select something and nothing was found matching your select statement.

2.) This section of code:
$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($qry);

Does not return (get and pass to the PHP script) anything from the database, all it does is run the query... You will need to run some version of:

'mysql_fetch_blah', which in this case is either mysql_fetch_array() or mysql_fetch_row()... One of the biggest differences is mysql_fetch_array() returns the associative indices (names of the cols) as well as the numerical value of the indices, where mysql_fetch_row() only returns numerical. IOW if you want to be able to use col names ($result['logins']) rather than just a numerical 'identifier' ($result[N] EG $result[2]) to determine the array piece to evaluate you need to use mysql_fetch_array().

##### @ ##### @ #####

The preceding stated let's edit and trouble shoot:
//Create query

/* and echo the query so you can see what's being POSTed, what you are sending to the db and compare it to the stored db information manually to ensure there is a match. */

echo $sql="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";

/* If the preceding does not work, echo back the 'passed' (POSTed) information and compare it to a 'hard coded' login and password into the select to see where + what the difference is:

echo "Login Variable: ".$login."<br /><br />Password Variable: ".$_POST['password']."<br /><br />";

echo $sql="SELECT * FROM members WHERE login='TestLogin' AND passwd='".md5('TheActualPassword')."'";
*/

$qry=mysql_query($sql);
$result=mysql_fetch_array($qry)

/* After being able to see the sql and compare it to the db information, let's see exactly what's stored in the $result array. */
print_r($result);

// Then let's exit until we know this part of the script works correctly.
exit();

/* Once that part is working, let's check to see if there was a successful query + result first, because if not and we know the select is correct and functioning, we know we had an unsuccessful login attempt: Using a negative comparison (preceded with !) should do the trick. The following basically says if No $result= 'the query' the login failed. */

if(!$result=mysql_fetch_array($qry)) {
//Login failed
header("location: login-failed.php");
exit();
}

/* If logins is greater than or = 3 is a bit safer... Since we know we had a 'good query' to get here we need to check the number of logins to see if we should let them in. */

elseif($result['logins']>=3) {
echo "Your Logins have expired";
exit();
}

/* If we did not 'exit' in either of the two preceding if statements, we can default to this else statement, because we had to have a good query (login) and logins are not = to or > 3. (In a 'super secure' environment, it would probably be better to do things a bit differently, but this way is easiest and should be sufficient for your needs.) */

else {
// Edited to the exact login used and accessed by the SELECT.

$sql2="UPDATE members SET logins=logins+1 WHERE login='".$result['login']."'" ;
// We don't need anything to be returned to PHP with this portion of the script, so just using mysql_query() without a 'fetch' is fine.
mysql_query($sql2);

/* Check whether the query was successful or not.
We don't really need to do this, because I moved it up in the script where it makes more sense and commented it out... If we made it here, we know there was a valid login and the login count was < 3.
if($result) {
if(mysql_num_rows($result) == 1) {
//Login Successful
*/

session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
session_write_close();
header("location: member-index.php");
exit();
}

JuicyScript

10:17 pm on Oct 10, 2009 (gmt 0)

10+ Year Member



Can you please do somefin about this script
if(!$result=mysql_fetch_array($qry)) {
//Login failed
header("location: login-failed.php");
exit();
}

It execute the header("location:login-fail.php");
whether the username and password is right or wrong

i was thinking of somefin like this
if(!$username===$result['username'] && !$password===$result['password']) {
But i coded my script to encrypt the password...passwd='".md5($_POST['password'])."' so i can' get it ryt.

TheMadScientist

10:34 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you are saying is essentially the same thing I am saying, so my best advice is to make sure you are comparing apples to apples, not apples to oranges... If you encode (hash) the POSTed password value, you must compare it to a hashed value of the stored password to get a correct match. (I think you should be storing the password value as a hashed (encoded) value for security anyway.)