Page 1 of 1

Remember my username and password in my machine

Posted: Sat Jun 03, 2006 12:00 am
by madhu
hi all ...............

can anybody tell how to do "Remember my username and password in my machine" ???

if any body is trying to login to my website and if he clicks on checkbox ,i.e remember my username and

password,then that username and password should remember on that machine.

Like gmail ,if we select remember my username and password, then it is remembering na.

please give suggestions how to implement this feature.....

Waiting for your valuable suggestions...........

Thanks and regards
madhu

Posted: Sat Jun 03, 2006 2:20 am
by NiGHTFiRE
cookies

USe Cookies

Posted: Sat Jun 03, 2006 2:47 am
by phpmash
twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php
session_start();        // Shows we are using sessions

include("dbconnect.inc");
$username = $_POST['username'];    // Gets the inputted username from the form
$password = $_POST['password'];    // Gets the inputted password from the form
$time = time();            // Gets the current server time
$check = $_POST['setcookie'];        // Checks if the remember me button was ticked

$query = "SELECT username, passwd FROM table WHERE username = '$username' AND passwd = '$password'";
//echo $query;
$result = mysql_query($query, $dbh);
if(mysql_num_rows($result)) {    // If the username and password are correct do the following;
   $_SESSION['loggedin'] = 1;        // Sets the session 'loggedin' to 1

    if($check) {
    // Check to see if the 'setcookie' box was ticked to remember the user
    setcookie("hb[username]", $username, $time + 3600);        // Sets the cookie username
    setcookie("hb[password]", $password, $time + 3600);    // Sets the cookie password
    }

   header('Location: admin.html'); // Forwards the user to this URL
   exit();
}
else    // If login is unsuccessful forwards the user back to the index page with an error
{
   header('Location: login.php?error=1');
   exit();
}
?>
twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Jun 03, 2006 4:11 am
by madhu
hi am using php5, but it doesnot support cookies.........

please suggest any other ways to develope this feature.........

waiting for your valuable features..........

Thanks and regards
madhu

Posted: Sat Jun 03, 2006 5:14 am
by s.dot
PHP5 does support cookies. And I believe that would be about the only way of going about this feature.

Set Cookie

Code: Select all

setcookie($cookieName,$contents,$persist);

Posted: Tue Jun 06, 2006 1:06 am
by madhu
twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi all, finally after struggling i implemented like this..................

**************************************************************************************
[u][b]CODE:[/b][/u]

Code: Select all

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<title>Login Cookies</title>
<link rel="stylesheet" type="text/css" href="../home.css">
</head>

<body onload="getme();">
<script language="JavaScript">
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
// -->
</script>

<script language="JavaScript">
var usr;
var pw;
var sv;
function getme()
{
usr = document.loginform.username;
pw = document.loginform.password;
sv = document.loginform.save;

	if (GetCookie('username') != null)
	{
		usr.value = GetCookie('username')
		pw.value = GetCookie('password')
		if (GetCookie('save') == 'true')
		{
			sv[0].checked = true;
		}
	}
}
function saveme()
{
	if (usr.value.length != 0 && pw.value.length != 0)
	{
		if (sv[0].checked)
		{
			expdate = new Date();
			expdate.setTime(expdate.getTime()+(365 * 24 * 60 * 60 * 1000));
			SetCookie('username', usr.value, expdate);
			SetCookie('password', pw.value, expdate);
			SetCookie('save', 'true', expdate);
		}
		if (sv[1].checked)
		{
			DeleteCookie('username');
			DeleteCookie('password');
			DeleteCookie('save');
		}
	}
		else
	{
		alert('You must enter a username/password.');
		return false;
	}
}
</script>

<table border="0" cellpadding="5" cellspacing="0" width="100%">
  <tr>
    <td valign="top" width="100%">

    </td>
  </tr>
  <tr>
    <td valign="top" width="100%">

      <form name="loginform" onSubmit="return saveme();">
        <blockquote>
          <table border="1" cellpadding="5" cellspacing="0">
            <tr>
              <td>
                <table border="0" cellpadding="0">
                  <tr>
                    <td align="right"><font size="1" face="Verdana">Username:&nbsp;</font></td>
                    <td><font size="1" face="Verdana"><input type="text" name="username" size="20"></font></td>
                  </tr>
                  <tr>
                    <td align="right"><font size="1" face="Verdana">Password:&nbsp;</font></td>
                    <td><font size="1" face="Verdana"><input type="password" name="password" size="20"></font></td>
                  </tr>
                  <tr>
                    <td valign="top">
                      <p align="center"><font size="1" face="Verdana"><input type="submit" value="Login"></font></p>
                    </td>
                    <td valign="top">
                      <p><font size="1" face="Verdana">Remember my
                      username/password?<br>
                      <input type="radio" value="ON" name="save">Yes <input type="radio" name="save" value="OFF" checked>No</font></td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </blockquote>
      </form>
    </td>
  </tr>
  <tr>
    <td valign="top" width="100%">

      </td>
  </tr>
  <tr>
    <td valign="top" width="100%">

      <ol>


      </ol>
  </tr>
</table>

</body>

</html>
****************************************************************************************

Problem:

but here the problem is , once if we delete cookies in our system,then that username and password what we saved willnot show again.

please give suggestions how to proceed for this..............

Waiting for your valuable replies........................

Thanks and regards
madhu


twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jun 06, 2006 3:02 am
by twigletmac
madhu wrote:but here the problem is , once if we delete cookies in our system,then that username and password what we saved willnot show again.
That's just the way it works (for example GMail can only remember you if you keep the cookie they set).
madhu wrote:please give suggestions how to proceed for this..............
Set the cookie to have a long shelf life and that's about it really. Beyond that point it's up to your users to remember their usernames and passwords and you to have a system in place to recover/reset if they forget them.

Mac