Forum Moderators: coopster

Message Too Old, No Replies

One time password

I want let a user use a password just once

         

marovios

10:43 pm on May 26, 2005 (gmt 0)

10+ Year Member



Hi, does anyone know how to create a one time password?, i'm using MySQL and PHP and the idea is to give a user a password that could only be used once, if any user logs in with that password again it should be useless. Is this possible?.

Thanks in advance.

coopster

11:18 pm on May 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, marovios.

Are you talking about a one time password, or one time userid?

A common practice on passwords is to create an additional column in the table that forces the user to change their password on the next successful signon. This comes in very handy when resetting passwords or even on initial account setup (if it isn't a self-registration process).

4string

12:46 am on May 27, 2005 (gmt 0)

10+ Year Member



Couldn't you just add a boolean field to your table for 'password_used'. Then add to your login script update password_used to 1 when they login. Make your login script check the password= $_POST['password'] and the password_used=0 else die.

Is that kind of what you're after?

marovios

9:02 am on May 27, 2005 (gmt 0)

10+ Year Member



Thanks both guys.

coopster, yes, i'm talking about one time password. I won't let them change the passwords a i gave them, i just want the user access a restricted page only once. And then, if this guy try to access that page again, he won't be able as the pass should be useless because it was already used before. It's just one page, each user will be allowed to access that page only once, that's the idea.

Hi 4string, that's the idea. I'm a newbie you know so, regarding this: "Then add to your login script update password_used to 1 when they login" this should be done automatically along with the users first time login, how can i do this?.

Thanks both.

4string

12:49 pm on May 27, 2005 (gmt 0)

10+ Year Member



Your login script should be something like:


$query = mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password' AND password_used = '0' LIMIT 1") or die();

//get results
$row = mysql_fetch_array($query);

//If the query returns a row, you need to do another query to update 'password_used' to true.

if(mysql_num_rows($query) > 0){
$user_id = $row['user_id'];

$do = mysql_query("UPDATE user SET password_used = 1 WHERE user_id = '$user_id' LIMIT 1");
//do other login stuff...
} else {
//show an error
echo 'This password is invalid or has already been used once before.';
}

That should work, but I'm pretty new at this too! You should get the idea anyway. Good luck!

marovios

1:32 pm on May 27, 2005 (gmt 0)

10+ Year Member



Thank you very much 4string!.
I will try that one.

marovios

8:54 pm on May 27, 2005 (gmt 0)

10+ Year Member



Hi guys, sorry about this. I know is something stupid, but i cannot make it work, where should i add that code line to make invalid the password once used?.
This is my login script so far:

<?php require_once('Connections/rsLogin.php');?>
<?php
// *** Validate request to login to this site.
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
$GLOBALS['PrevUrl'] = $accesscheck;
session_register('PrevUrl');
}

if (isset($_POST['username'])) {
$loginUsername=$_POST['username'];
$password=$_POST['password'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "http://www.yahoo.com";
$MM_redirectLoginFailed = "http://www.google.com";
$MM_redirecttoReferrer = false;
mysql_select_db($database_rsLogin.php, $rsLogin.php);

$LoginRS__query=sprintf("SELECT username, password FROM login WHERE username='%s' AND password='%s'",
get_magic_quotes_gpc()? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc()? $password : addslashes($password));

$LoginRS = mysql_query($LoginRS__query, $rsLogin.php) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = "";

//declare two session variables and assign them
$GLOBALS['MM_Username'] = $loginUsername;
$GLOBALS['MM_UserGroup'] = $loginStrGroup;

//register the session variables
session_register("MM_Username");
session_register("MM_UserGroup");

if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
<html>
<head>
</head>

<body>
<form action="<?php echo $loginFormAction;?>" method="POST" name="login" id="login">
<p>username
<input name="username" type="text" id="username">
</p>
<p>password
<input name="password" type="text" id="password">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

Any clue? Thanks.

blueninja

4:18 am on May 28, 2005 (gmt 0)

10+ Year Member



How bout this:

<?php require_once('Connections/rsLogin.php');?>
<?php
// *** Validate request to login to this site.
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
$GLOBALS['PrevUrl'] = $accesscheck;
session_register('PrevUrl');
}

if (isset($_POST['username'])) {
$loginUsername=$_POST['username'];
$password=$_POST['password'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "http://www.yahoo.com";
$MM_redirectLoginFailed = "http://www.google.com";
$MM_redirecttoReferrer = false;
mysql_select_db($database_rsLogin.php, $rsLogin.php);

$LoginRS__query=sprintf("SELECT username, password FROM login WHERE username='%s' AND password='%s'",
get_magic_quotes_gpc()? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc()? $password : addslashes($password));

$LoginRS = mysql_query($LoginRS__query, $rsLogin.php) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = "";

$RemoveRS__query=sprintf("DELETE FROM login WHERE username='%s' AND password='%s' LIMIT 1",
get_magic_quotes_gpc()? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc()? $password : addslashes($password));

mysql_query($RemoveRS__query, $rsLogin.php) or die(mysql_error());

//declare two session variables and assign them
$GLOBALS['MM_Username'] = $loginUsername;
$GLOBALS['MM_UserGroup'] = $loginStrGroup;

//register the session variables
session_register("MM_Username");
session_register("MM_UserGroup");

if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
<html>
<head>
</head>

<body>
<form action="<?php echo $loginFormAction;?>" method="POST" name="login" id="login">
<p>username
<input name="username" type="text" id="username">
</p>
<p>password
<input name="password" type="text" id="password">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

marovios

4:59 pm on May 28, 2005 (gmt 0)

10+ Year Member



Thank you very much. I'm on it!

marovios

5:35 pm on May 28, 2005 (gmt 0)

10+ Year Member



Blueninja, that worked great!. Thank you very much.

When i was about to give up your solution came up and the sun shines again for me now!
Thank you to you and all the other guys that put their time an efforts on this.

This is a great forum.

coopster

1:04 am on May 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Glad you got it sorted, marovios.

And welcome to WebmasterWorld, blueninja.