我做了一个后台,想要设置成如果没有登录就不能进入管理界面,在网上查到一段代码,几乎实现了功能,但是在提交登录按钮时用到了一个全局变量,不知道是在什么地方定义的,给它付了个什么样的值,代码部分有具体的注释,求知道的帮忙解释一下,代码如下:
登录界面代码:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/font.css" rel="stylesheet">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<p> </p>
<p>
</p>
<p> </p>
<p> </p>
<p> </p>
<script language="javascript" type="text/javascript">
function chkinput(form){
if(form.name.value==""){
alert("请输入用户名!");
form.name.select();
return(false);
}
if(form.pwd.value==""){
alert("请输入用户密码!");
form.pwd.select();
return(false);
}
return(true);
}
</script>
<form name="form1" method="post" action="chkadmin1.php" onSubmit="return chkinput(this)">
<table width="545" border="0" align="center" cellpadding="0" cellspacing="0" id="__01">
<tr>
<td height="167" background="images/admin_user.jpg"> </td>
</tr>
<tr>
<td height="201" align="right" background="images/default_02.gif">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="66" height="32" align="center">用户名:</td>
<td width="112" align="center"><input type="text" name="name" size="14" maxlength="20" class="inputcss"></td>
<td width="60" align="center"> 密 码:</td>
<td width="112" align="center"><label>
<input name="pwd" type="password" id="pwd" size="14" maxlength="20">
</label></td>
<td width="150">
<div align="center">
<input name="imageField" type="image" src="images/default_07.gif" align="middle" width="74" height="24" border="0">
</div></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
提交部分的代码:
<?php
session_start();class chkinput{
var $name;
var $pwd;
function chkinput($x,$y){
$this->name=$x;
$this->pwd=$y;
}
function check_input(){ // check_input()的作用是什么,不需要定义吗,好像与checkinput()是一样的,随便用一个都能实现功能 include_once("conn/conn.php");
$sql=mysql_query("select name from tb_admin_user where name='".$this->name."'",$conn);
$info=mysql_fetch_array($sql);
if($info==false){
echo "<script>alert('对不起,不存在该用户!');history.back();</script>";
exit;
}else{
$sql=mysql_query("select name from tb_admin_user where name='".$this->name."' and pwd='".$this->pwd."'",$conn);
$info=mysql_fetch_array($sql);
if($info==false){
echo "<script>alert('对不起,密码输入错误!');history.back();</script>";
exit;
}else{
if($_SESSION["admin_user"]!=""){ [color=#0000FF]//不知道这是什么意思以及它的作用是什么?admin_user是怎么来的。 session_unregister("admin_user");
}
session_register("admin_user");
$_SESSION["admin_user"]=$this->name;
echo "<script>alert('登录成功!');window.location.href='index.php';</script>";[/color] }
}
mysql_close($conn);
}
}
$chk=new chkinput($_POST[name],md5($_POST[pwd]));
$chk->check_input();
?>
登录界面代码:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/font.css" rel="stylesheet">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<p> </p>
<p>
</p>
<p> </p>
<p> </p>
<p> </p>
<script language="javascript" type="text/javascript">
function chkinput(form){
if(form.name.value==""){
alert("请输入用户名!");
form.name.select();
return(false);
}
if(form.pwd.value==""){
alert("请输入用户密码!");
form.pwd.select();
return(false);
}
return(true);
}
</script>
<form name="form1" method="post" action="chkadmin1.php" onSubmit="return chkinput(this)">
<table width="545" border="0" align="center" cellpadding="0" cellspacing="0" id="__01">
<tr>
<td height="167" background="images/admin_user.jpg"> </td>
</tr>
<tr>
<td height="201" align="right" background="images/default_02.gif">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="66" height="32" align="center">用户名:</td>
<td width="112" align="center"><input type="text" name="name" size="14" maxlength="20" class="inputcss"></td>
<td width="60" align="center"> 密 码:</td>
<td width="112" align="center"><label>
<input name="pwd" type="password" id="pwd" size="14" maxlength="20">
</label></td>
<td width="150">
<div align="center">
<input name="imageField" type="image" src="images/default_07.gif" align="middle" width="74" height="24" border="0">
</div></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
提交部分的代码:
<?php
session_start();class chkinput{
var $name;
var $pwd;
function chkinput($x,$y){
$this->name=$x;
$this->pwd=$y;
}
function check_input(){ // check_input()的作用是什么,不需要定义吗,好像与checkinput()是一样的,随便用一个都能实现功能 include_once("conn/conn.php");
$sql=mysql_query("select name from tb_admin_user where name='".$this->name."'",$conn);
$info=mysql_fetch_array($sql);
if($info==false){
echo "<script>alert('对不起,不存在该用户!');history.back();</script>";
exit;
}else{
$sql=mysql_query("select name from tb_admin_user where name='".$this->name."' and pwd='".$this->pwd."'",$conn);
$info=mysql_fetch_array($sql);
if($info==false){
echo "<script>alert('对不起,密码输入错误!');history.back();</script>";
exit;
}else{
if($_SESSION["admin_user"]!=""){ [color=#0000FF]//不知道这是什么意思以及它的作用是什么?admin_user是怎么来的。 session_unregister("admin_user");
}
session_register("admin_user");
$_SESSION["admin_user"]=$this->name;
echo "<script>alert('登录成功!');window.location.href='index.php';</script>";[/color] }
}
mysql_close($conn);
}
}
$chk=new chkinput($_POST[name],md5($_POST[pwd]));
$chk->check_input();
?>
echo "<script>alert('登录成功!');window.location.href='index.php';</script>";
}
session_register("admin_user");判断admin_user这个session是否为空。如果不为空,就重新注册,为它赋值为$this->name