1

我的脚本中有 3 个页面 1. header.php 2. auth.php 3. home.php

头文件有登录代码,函数和数据库连接在 auth.php 中。

现在登录正在工作,唯一的就是cookie,我的意思是我想知道如何在脚本中添加一个记住功能,请帮忙。这是我的代码 1. header.php

include 'auth.php';
if(empty($_POST['submitlogin']) === false)
{
$username = $_POST['username'];
$password = $_POST['password'];  

if(empty($username) === true)
  {
      $errors[] = '<font color="#FFFFFF">You need to enter an username!</font>';

  } 

 if(empty($password) === true)
  {

      $errors[] = '<font color="#FFFFFF">You need to enter password!</font>';
  } 

else if(user_exists($username) === false )
  {

      if(empty($username) === true){}
      else
        $errors[] = '<font color="#FFFFFF">No such username has been found.</font>';
  } 

else if(user_active($username) === false)
  {

      $errors[] = '<font color="#FFFFFF">You haven\'t activated your account.</font>';
  }
else
  {

    $login = login($username, $password);
    if($login === false)
    {
        $errors[] = '<font color="#FFFFFF">The Username/Password Combination is incorrect</font>';
    } 
    else 
    {
        $_SESSION['user_id'] = $login;
        header('Location: home.php');
        exit();
    }
  }
}

2. auth.php 代码

session_start();
error_reporting(0);

require 'connect.php';


if(logged_in() === true){

  $session_user_id = $_SESSION['user_id'];
  $user_data = user_data($session_user_id, 'user_id', 'username', 'password',  'email', 'active', 'type');


  if(user_active($user_data['username']) === false)
    {
      session_destroy(); 
      header('Location: home.php');
      exit();
    }

        }else{

       }

    $errors = array();


  function user_data($user_id)
   {
  $data = array();
  $user_id = (int)$user_id;

  $func_num_args = func_num_args();
  $func_get_args = func_get_args();

  if($func_num_args > 1)
  {
     unset($func_get_args[0]); 

     $fields = '`' . implode('`, `', $func_get_args) . '`';
     $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `edge_users` WHERE `user_id` = $user_id"));
     return $data;
  }
   }


   function user_active($username) 
   {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `edge_users` WHERE  `username` = '$username' AND `active` = 1"), 0)==1)?true: false;  }



    function login($username, $password)
     {
  $user_id = user_id_from_username($username);
  $username = sanitize($username);
  $password = md5($password);
  return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `edge_users` WHERE  `username` = '$username' AND `password` = '$password'"), 0)  == 1) ? $user_id : false;
      }

HTML 表单

  <form  method="post">
  Username
   <input type="text" name="username" style="padding: 5px; width:228px; height:15px;"  autocomplete="off" value="<?php echo $_POST['username'];?>"/>>         
   Password
   <input type="password" name="password" style="padding: 5px;  width:228px;height:15px;" />                                                                    
   <input type="checkbox" name="rememberme" /> Remember Me  

   <input type="submit" name="submitlogin"  style="background: #00c800; border:1px  solid  #00c800; color:#FFFFFF; padding:5px 10px 5px 10px; font-size:14px; float: right;"  value="Login" />
    </form>
4

1 回答 1

0

因为您使用的是 PHP 会话,所以您需要扩展会话。

$_SESSION['user_id'] = $login;

添加以下行:

if (isset($_POST['rememberme'])) {
    $params = session_get_cookie_params();
    setcookie(session_name(), $_COOKIE[session_name()], time() + 60*60*24*30, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
}

如果按下记住我按钮,这会将 PHP 会话 cookie 延长 30 天。

在注销时,您可能想要这样的东西:

$_SESSION = array();
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
session_destroy();

这将破坏您的 PHP 会话,因此如果有人检查了“记住我”并注销然后再次登录而没有按“记住我”,您将不记得他们。

于 2013-12-14T01:55:21.557 回答