PHP自动登录功能登录脚本 [英] PHP Autologin function to login script

查看:149
本文介绍了PHP自动登录功能登录脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何隐藏此脚本的自动登录功能?

How would I implent an autologin feature to this script?

session_start();
$result = mysql_query("SELECT id FROM users 
                       WHERE username = '{$_POST['username']}' 
                       AND password = '{$_POST['password']}'");

     if (isset($_POST['savelogin'])) {
     setcookie("SaveLogin", $_POST['username'], time()+3600);
     setcookie("SaveLogin", $_POST['password'], time()+3600);
     }


if (mysql_num_rows($result) == 0) {
    exit('wrong username/password');

    } else {
    $_SESSION['id'] = mysql_result($result, 0, 'id');
    header("Location: ./");
    }



<form method="post">


Username: <input type="text" name="username" size="22" /><br>
Password: <input type="password" name="password" size="22" /><br>

<br>
Autologin? <input type="checkbox" name="savelogin" />

<input type="submit" value="Login" />

</form>

这是我有多远。保存用户名和密码。
但是我应该怎么做它自动登录?

Thats how far i have gotten. Saving the username and password. But how should I do so it autologins?

推荐答案

首先,你不想保存用户名和密码到cookie。这是一个坏主意。

First, you don't want to save the username and password to the cookie. That's a bad idea.

一个简单的思考方法是:

A simple method of thinking of this would be:

1)创建一个新的字段,用于存储MD5散列。你可以调用它session_key。
2)当您提交页面时,脚本应该执行以下操作。

1) Create a new field in the users table that stores an MD5 hash. You can call it session_key. 2) When you submit the page, the script should do the following.


  • 验证用户名和密码

  • 如果是良好的用户名和密码对,请检查saveLogin变量

  • 如果设置了saveLogin变量,请生成md5并将其存储在数据库中。还将该md5存储在cookie中。

  • 建立您需要的工作阶段资料。

  • 重新导向至./

  • Validate the username and password
  • If it is a good username and password pair, check for the saveLogin variable
  • If the saveLogin variable is set, generate an md5 and store that in the database. Also store that md5 in a cookie. Be sure the database table has a cookie-expires field as well.
  • Build the session data that you need.
  • Redirect to ./

3)在./页面上,执行以下操作:

3) On your ./ page, do the following:


  • 检查会话是否仍然存在。

  • 如果会话不存在,请检查该cookie。

  • 如果Cookie存在,请查找会话ID在数据库中,并确保它没有过期。

这将使您的应用程序更安全一些。它可能不是最好的编码方式,但概念应该给你一个如何做一个相当安全的登录页面的想法。

This should make your app a bit more secure. It may not be the best way of coding, but the concepts should give you an idea of how to make a fairly secure login page.

这篇关于PHP自动登录功能登录脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆