这里我给大家提供的只有代码不分,需要的文件需要自行下载,libs文件不提供。。。
首先是数据库:smarty.sql
/*
Navicat MySQL Data Transfer
Source Server : qq
Source Server Version : 50133
Source Host : localhost:3306
Source Database : smarty
Target Server Type : MYSQL
Target Server Version : 50133
File Encoding : 65001
Date: 2011-11-14 16:44:23
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`studentNum` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`password` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('12', '20044021', '123456');
INSERT INTO `student` VALUES ('13', '20044149', '000000');
INSERT INTO `student` VALUES ('14', '20044173', '000000');
INSERT INTO `student` VALUES ('16', '20044152', '000000');
INSERT INTO `student` VALUES ('17', '20044003', '000000');
INSERT INTO `student` VALUES ('18', '20044023', '000000');
接下来是php文件:
index.php
<?php
require("./libs/Smarty.class.php");
$tpl = new Smarty(); //建立smarty实例对象$smarty
$tpl->template_dir = "./templates/";//设置模板目录
$tpl->compile_dir = "./Stemplates_c/"; //设置编译目录
$tpl->config_dir = "./configs/";
$tpl->left_delimiter = "<{";
$tpl->right_delimiter = "}>";
$tpl->display("index.tpl");
?>
check.php
<?php
$studentNum = @$_POST['studentNum'];
$password = @$_POST['password'];
$conn = mysql_connect("localhost","root","");
mysql_select_db("smarty",$conn);
mysql_query("set names 'utf8'");
$result = mysql_query("select password from student where studentNum = '$studentNum' and password = '$password'");
while($row = mysql_fetch_row($result))
{
$array[]=$row;
}
mysql_free_result($result);
if(count(@$array))
{
echo "登录成功!!!";
}
else
{
echo "用户名或密码不正确!";
}
?>
最后是模板文件:
index.tpl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Smarty用户登录</TITLE>
</HEAD>
<BODY>
<form id="form1" name="form1" method='post' action="check.php">
<table align="center" >
<tr>
<td align="right">学号:</td>
<td><input name="studentNum" type="text"></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td align="right"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</BODY>
</HTML>