喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
     
标题: 如何做个安全的登陆验证和session的问题  
 
jiyangtx (jiyangtx)
新手上路
Rank: 1
初级会员



UID 67836
精华 0
积分 0
帖子 5
金钱 0 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-1-7
状态 离线
如何做个安全的登陆验证和session的问题

请问PHP+MYSQL如何做个安全的登陆验证?
和使用SESSION应注意的问题,我写时老出错:crying: 大家帮忙发表点建议
2006-1-7 01:26 PM#1
查看资料  发短消息  顶部
 
meteor_shower
高级会员
Rank: 4
揮刀戲江湖譜寫情緣傳奇


UID 66798
精华 2
积分 977
帖子 913
金钱 947 喜悦币
威望 30
人脉 0
阅读权限 50
注册 2005-9-21
来自 深圳
状态 离线
[推荐阅读] 乐吧注册有礼了阿


<?php
include_once('config/config.php'); 
include_once(
'include/db_config.php'); 
session_start();

$action = ($_GET[action]) ? $_GET[action] : $_POST[action];
switch(
$action)
{
    case 
'logout'logout(); break;
    case 
'login' :
    default      : 
login();  break;
}
?>

<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title><?php echo $title?></title>
<link rel="stylesheet" type="text/css" href="style/login.css">
<link rel="stylesheet" type="text/css" href="style/link.css">
</head>

<body>
<?php include_once("config/logo.php"); ?><br><br>
<form action="login.php" method=post>
<table width="380" border="0" cellspacing="0" cellpadding="0" align="center" class="newtable">
<tr><td class="newtd" colspan="2"><div align="center">登錄文件管理系統</div></td> </tr>
<tr>
    <td class="newtd"><div align="right">帳號﹕</div></td>
    <td class="newtd" width="280"><div align="left"><input name=accounts type=text id="accounts" size="20">
    </div></td>
</tr>
<tr>
    <td class="newtd"><div align="right">密碼﹕</div></td>
    <td class="newtd"><div align="left"><input name=password type=password size="20"></div></td>
</tr>
<tr>
    <td class="newtd"><div align="right">驗証碼﹕</div></td>
    <td class="newtd"><input name=verifycode type=text value="" size="20">
    請輸入 <img style="background: url('images/bg_logincode.gif')" src="include/getcode.php" align=absmiddle width="32" height="16"></td>
</tr>
<tr>
    <td class="newtd"><div align="right">&nbsp; </div></td>
    <td class="newtd"><div align="left"><input class=button type=submit name=submit value=" 登 錄 "></div></td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td colspan="2"><div align="center"><a href="../index.php">返回首頁</a></div></td>
  </tr>
</table>
</form>
</body>
</html>
<?php
function login()
{
    global 
$db_link;
    if(
$_POST[submit])
    {
        
$query="SELECT * FROM  login_admin WHERE ACCOUNTS='$_POST[accounts]' ";
        
$result=mysql_query($query,$db_link);
        if(
mysql_num_rows($result)!=1)
        {
            echo 
"<meta http-equiv='Content-Type' content='text/html; charset=big5'>";
            print (
'<SCRIPT>window.alert("帳號不存在,無法進入系統﹗")</SCRIPT>');
        }
        else
        {
            
$arrUser=mysql_fetch_array($result);
            if(
$arrUser["PASSWORD"]!= md5($_POST[password]))
            {
                echo 
"<meta http-equiv='Content-Type' content='text/html; charset=big5'>";
                print (
'<SCRIPT>window.alert("密碼不正確,無法進入系統﹗")</SCRIPT>');
            }
            else
            {
                if( 
$_SESSION[verifycode] == $_POST[verifycode] )
                {
                    
$_SESSION[isaccounts]  = 1;
                    
$_SESSION[accounts] = $_POST[accounts];
                    
header("Location: index.php");
                }
                else
                {
                    echo 
"<meta http-equiv='Content-Type' content='text/html; charset=big5'>";
                    print (
'<SCRIPT>window.alert("驗証碼沒有填寫或者填寫錯誤﹐無法進入系統﹗")</SCRIPT>');
                }
            }
        }        
    }
    else
    {
        
session_unset($_SESSION[isaccounts]);
        
session_unset($_SESSION[accounts]);
    }
}


function 
logout()
{
    echo 
"<meta http-equiv='Content-Type' content='text/html; charset=big5'>";
    unset(
$_SESSION[isaccounts]);
    unset(
$_SESSION[accounts]);
    print (
'<SCRIPT>window.alert("你已經安全退出管理系統﹐請關閉瀏覽器﹗")</SCRIPT>');
}
?>
在其它網頁中加下下列語句

<?php 
$action 
= ($_GET[action]) ? $_GET[action] : $_POST[action];

session_start();
if(
$_SESSION[isaccounts]!=1)
{
    unset(
$_SESSION[isaccounts]);
    unset(
$_SESSION[accounts]);
    
header("Location: login.php");
}
?>

記信密碼部分使用了 MD5  加密

驗証碼自己生成﹐很簡單﹗

我的驗証碼不能公布﹐否則我的算法就不安全了
2006-1-7 04:41 PM#2
查看资料  Blog  发短消息  QQ . .   顶部
 
nroe (nroe)
中级会员
Rank: 3Rank: 3
中级会员



UID 67843
精华 0
积分 227
帖子 220
金钱 227 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2006-1-7
状态 离线
[推荐阅读] preg_match_al问题麻烦高手指点下!谢谢!
文章收录呵呵!
2006-1-7 04:47 PM#3
查看资料  Blog  发短消息  顶部
 
phphp
版主
Rank: 7Rank: 7Rank: 7
加班一族


UID 65188
精华 1
积分 184
帖子 3007
金钱 173 喜悦币
威望 10
人脉 1
阅读权限 100
注册 2005-7-20
来自 北京市海淀区
状态 离线
[推荐阅读] 群组投票系统
跟验证码算法还有关系?
那这个有什么用




书到用时方恨少, 事非经过不知难
2006-1-7 06:51 PM#4
查看资料  访问主页  Blog  发短消息  QQ . .   顶部
 
jiyangtx (jiyangtx)
新手上路
Rank: 1
初级会员



UID 67836
精华 0
积分 0
帖子 5
金钱 0 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-1-7
状态 离线
[推荐阅读] 如何用正则表达式截取html中的部分文字?
谢谢

谢谢你的程序!
2006-1-7 07:26 PM#5
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.027923 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP