萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php入門級用戶登錄代碼

php入門級用戶登錄代碼

這是一款很簡單的php入門級用戶登錄代碼,如果用戶登錄成功了就會自動跳到指定的頁面,否則就會返回登錄頁面,登錄原是是判斷用戶輸入的用戶名與密碼在數據庫中是否在存了,如果是就保存信息存在session否提示登錄出錯。

#
# 表的結構 `admin`
#

 代碼如下 復制代碼 create table `admin` (
  `id` mediumint(4) not null auto_increment,
  `us` varchar(50) not null default '',
  `pas` varchar(250) not null default '',
  unique key `id` (`id`)
) type=myisam comment='登錄' auto_increment=2 ;

#
# 導出表中的數據 `admin`
#

insert into `admin` (`id`, `us`, `pas`) values (1, 'admin', 'admin');
*/?>

登錄html頁面代碼

 代碼如下 復制代碼

<html>
<head>
<title>登錄</title>
<meta http-equiv="content" content="text/html; charset=gb2312">
<link href="css教程.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
if ($http_cookie_vars["admin"]) {
 header("location:admin.php");
}
?>
<form method="post" action="admin_logins.php">
<table width="40%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolorlight="#cccccc" bordercolordark="#f7f7f7" class="css">
<tr>
<td bgcolor="#f7f7f7" colspan="2" align="center"><b>管理員登錄</b></td>
<tr>
<tr>
<td>用戶名</td>
<td><input type="text" name="us" size="30" maxlength="30" class="inputt"></td>
<tr>
<tr>
<td>密碼</td>
<td><input type="password" name="pas" size="34" class="inputt" maxlength="40"></td>
</tr>
<tr>
<td bgcolor="#f7f7f7" colspan="2"><div align="center">
  <input type="submit" name="submit" class="inputt" value="進入">
</div></td>
</tr>
</table>
</form>
</body>
</html>

php代碼

 代碼如下 復制代碼

<?php
$db=@mysql_connect("localhost","root","micronsky.net") or die("數據庫連接也錯");
mysql_select_db("vote",$db);
$submit=$_post["submit"];
$us=$_post["us"];
$pas=$_post["pas"];
if ($submit) {
 $result=mysql_query("select us,pas from admin where us='$us'",$db);
 if ($myrow=mysql_fetch_array($result)) {
  if ($pas==$myrow["pas"]) {
   setcookie("admin",$us,time()+3600);
   header("location:admin.php");
  }
  else {
   echo "<script>alert('密碼不正確');history.back();</script>";
  }
 }
  else {
   echo "<script>alert('用戶名不存在');history.back();</script>";
  }

 }
?>

copyright © 萬盛學電腦網 all rights reserved