需要帮助在php中记住我

时间:2014-06-06 00:37:53

标签: php remember-me

我试图创造一个"记住我"登录功能,以便保存用户cookie,并且他们不必在关闭浏览器时继续登录。这是我的开始会话语法..

     <?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

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

if (isset($_POST['UserName'])) {
  $loginUsername=$_POST['UserName'];
  $password=$_POST['Password'];
  $MM_fldUserAuthorization = "Userlevel";
  $MM_redirectLoginSuccess = "profile.php";
  $MM_redirectLoginFailed = "Login.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_localhost, $localhost);

  $LoginRS__query=sprintf("SELECT Username, Password, Userlevel FROM users WHERE Username=%s AND Password=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

  $LoginRS = mysql_query($LoginRS__query, $localhost) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {

    $loginStrGroup  = mysql_result($LoginRS,0,'Userlevel');

    if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;       

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

然后我的登录表单语法和记住我:

<form id="LoginForm" name="LoginForm" method="POST" action="<?php echo $loginFormAction; ?>">
<?php if (isset($_SESSION['errors'])): ?>
    <div class="form-errors">
        <?php foreach($_SESSION['errors'] as $error): ?>
            <p><?php echo $error ?></p>
        <?php endforeach; ?>
    </div>
<?php endif; ?> 
<table width="600" border="0">
    <tr>
      <td><span id="sprytextfield1">
        <label for="UserName2"></label>
Username: <br />
        <input type="text" name="UserName" id="UserName2" />
      <span class="textfieldRequiredMsg">A value is required.</span></span></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><span id="sprytextfield2">
        <label for="Password"></label>
Password: <br />
        <input type="password" name="Password" id="Password" />
      <span class="textfieldRequiredMsg">A value is required.</span></span></td>
    </tr>
    <tr>
      <td><span id="sprycheckbox1">
        <input type="checkbox" name="ch1" id="ch1" />
        <label for="ch1"></label>
      <span class="checkboxRequiredMsg">Please make a selection.</span>remember username and password</span> &nbsp;
        <?php

0 个答案:

没有答案