关于cookie问题,紧急求助

qjhaaaaa 2003-09-12 07:07:31
各位师兄:
在用户登录的时候,用户输入用户名和密码,当用户登录后,用户名和密码能自动保存,第二次登录的时候,不用输入用户名和密码,直接点击登录按钮,就可以进去,大家可以看看http://www.epubcn.com这个网站
1、点击e类出版物论坛 | EPUBCN.NET{Forum}按钮
2、进入http://www.epubcn.com/forum/index.php页面
3、点击登录按钮,看到登录页面,输入用户名和密码aaa
4、进入论坛
5、关闭IE浏览器
6、重新打开http://www.epubcn.com
7、点击点击e类出版物论坛 | EPUBCN.NET{Forum}按钮
直接进入到论坛看到用户名为aaa
大家注意千万不要退出,一退出,就清除了cookie,
麻烦各位师兄帮我讲解一下,这个问题具体怎么实现。
...全文
106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gboy2003 2003-09-14
  • 打赏
  • 举报
回复
up
joyrush 2003-09-14
  • 打赏
  • 举报
回复
请看 jCookie.sf.net
qjhaaaaa 2003-09-13
  • 打赏
  • 举报
回复
望各位兄台帮忙,答者有分
qjhaaaaa 2003-09-12
  • 打赏
  • 举报
回复
上面这位兄弟,有msn或者mail没有
明天我来问你
我的msn : qjhaaaaa@hotmail.com

邮件:qjh@sinodg.com
umljsp 2003-09-12
  • 打赏
  • 举报
回复
就是cookie的读写问题吧,应该是很好实现的.
登陆时写入,退出时清除
我给你一个cookie的读写bean,它是cnjbb论坛的一部分,要是对具体的操作不清楚,你可以下载一份过来看看
网址是www.cnjsp.org/cnjbb
package com.cnjsp.forum.util;
/**
* Title: GCookie
* Description:
* Copyright: Copyright (c) 2002
* Company: Cnjsp.com
* @author: Sager
* @version 1.0
*/
import javax.servlet.http.*;

public class GCookie {

public static String getCookieValue(HttpServletRequest request,String cookieName,String defaultValue) {
Cookie cookieList[]=request.getCookies();
if(cookieList==null||cookieName==null)
return "";
for(int i = 0; i < cookieList.length; i ++) {
try {
if(cookieList[i].getName().equals(cookieName))
//return java.net.URLDecoder.decode(cookieList[i].getValue(),"GB2312");
return java.net.URLDecoder.decode(cookieList[i].getValue(), "GBK");
} catch(Exception e){
e.printStackTrace();
}
}
return "";
}
public static void setCookie(HttpServletResponse response,
String cookieName,
String cookieValue) {
try {
Cookie theCookie = new Cookie(
java.net.URLEncoder.encode(cookieName, "GBK"),
java.net.URLEncoder.encode(cookieValue, "GBK"));
//Sager20021221添加,设置Cookie存放路径,以便网站和论坛共享Cookie
theCookie.setPath("/");
response.addCookie(theCookie);
}
catch(java.io.UnsupportedEncodingException uee) {
}
}
public static void setCookie(HttpServletResponse response,
String cookieName,
String cookieValue,
int cookieMaxage) {
try {
Cookie theCookie = new Cookie(
java.net.URLEncoder.encode(cookieName, "GBK"),
java.net.URLEncoder.encode(cookieValue, "GBK"));
theCookie.setMaxAge(cookieMaxage);
//Sager20021221添加,设置Cookie存放路径,以便网站和论坛共享Cookie
theCookie.setPath("/");
response.addCookie(theCookie);
}
catch(java.io.UnsupportedEncodingException uee) {
}
}
}
ghostxinghe 2003-09-12
  • 打赏
  • 举报
回复
下面这三个函数就是我在写一个登陆系统时曾经写过的,在你第一次登陆的时候login()会自动生成一个cookie和有效时间,然后在你的登陆调用的php程序checklogin()检查是否登陆过,logout()就会删除cookie,这样就要重新登陆.

<?php

// ------------------------------ function CHECKLOGIN ------------------------------ //

function islogin($username,$password){
global $dbhost,$dbuser,$dbpasswd,$dbname,$user_table;


// 如果cookie[用户名/密码]为空, 跳转到登录页面
if ($username=="" || $password==""):
header ("Location:index.php");
exit;
endif;

// 获得用户名下的密码
$sql = "SELECT * FROM $user_table WHERE username='$username'";
$result = mysql_db_query($dbname,$sql);
$obj = mysql_fetch_object($result);
$user_password = $obj->password;
// 如果cookie[密码]不正确, 跳转到登录页面
if ($password!=$user_password):
header ("Location:index.php");
exit;
endif;


} // end checklogin


// ------------------------------ function LOGIN ------------------------------ //

function login($username,$password){
global $dbhost,$dbuser,$dbpasswd,$dbname,$user_table,$rizhi_table,$cookie_name,$REMOTE_ADDR;

$sql = "SELECT * FROM $user_table WHERE username='$username'";
$result = mysql_db_query($dbname, $sql);
$objresult = mysql_fetch_object($result);
$user_password = $objresult->password;
$user_zhuangtai= $objresult->zaixianzhuangtai;

if ($username=="" || $password==""):
echo "用户名或密码不能为空";
echo "<br><a href=javascript:history.back(1)>点击这里返回</a>";
exit;
endif;

if ($user_password == ""):
echo "用户名错误";
echo "<br><a href=javascript:history.back(1)>点击这里返回</a>";
exit;
endif;

if ($password != $user_password):
echo "密码错误";
echo "<br><a href=javascript:history.back(1)>点击这里返回</a>";
exit;
endif;

if ($user_zhuangtai == '1'):
echo "管理员已经在线";
echo "<br><a href=javascript:history.back(1)>点击这里返回</a>";
exit;
endif;

setcookie ($cookie_name."[1]",$username,time()+(1*24*3600));
setcookie ($cookie_name."[2]",$password,time()+(1*24*3600));

$user_zhuangtai="update $user_table set zaixianzhuangtai='1' where id='1'";
$result1 = MySQL_query($user_zhuangtai);

$denglushijian=date("l d F Y h:i:s");

$denglu1="insert into guanlirizhi set denglushijian='$denglushijian',flag='1',ip='$REMOTE_ADDR'";
$result2 = MySQL_query($denglu1);
echo "登录成功!";

echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=gaoshu.php'>";

exit;

} // end login


// ------------------------------ function LOGOUT ------------------------------ //

function logout(){
global $dbhost,$dbuser,$dbpasswd,$dbname,$user_table,$rizhi_table,$cookie_name;

setcookie ($cookie_name."[1]","");
setcookie ($cookie_name."[2]","");
$user_zhuangtai="update $user_table set zaixianzhuangtai='0' where id='1'";
$result = MySQL_query($user_zhuangtai);

$tuichushijian=date("l d F Y h:i:s");
$denglu2="update guanlirizhi set tuichushijian='$tuichushijian' where flag='1'";
$result1 = MySQL_query($denglu2);
$flag="update guanlirizhi set flag='0' where flag='1'";
$result2 = MySQL_query($flag);
echo "<a href=index.php>注销成功</a>";
echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=../math/main.php?select=1'>";
exit;

} // end logout

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧