1.项目框架

|--mvc

  |--data  数据

    |--cache 缓存

    |--template_c 模板生成目录

  |--framework  

    |--function

      |--function.php 功能类

    |--libs

      |--core

        |--DB.class.php  数据库工具类  

        |--VIEW.class.php  视图工具类

      |--db

        |--mysql.class.php  mysql工具类

      |--view

        |--Smarty  smarty核心文件

    |--include.list.php  统一导入php数组

    |--pc.php  统一初始化

  |--html

  |--img

  |--libs

    |--controller

      |--adminController.class.php  用户控制

    |--model

      |--adminModel.class.php  用户模型

  |--admin.php  php入口

  |--config.php  php配置

2.function.php 功能类

    function daddslashes($str){
return (!get_magic_quotes_gpc())?addslashes($str):$str;
}

3.DB.class.php  数据库工具类

class DB {

    public static $db;

    public static function init($dbtype, $config) {
self::$db = new $dbtype;
self::$db->connect($config);
} public static function query($sql){
return self::$db->query($sql);
} public static function findAll($sql){
$query = self::$db->query($sql);
return self::$db->findAll($query);
} public static function findOne($sql){
$query = self::$db->query($sql);
return self::$db->findOne($query);
} public static function findResult($sql, $row = 0, $filed = 0){
$query = self::$db->query($sql);
return self::$db->findResult($query, $row, $filed);
} public static function insert($table,$arr){
return self::$db->insert($table,$arr);
} public static function update($table, $arr, $where){
return self::$db->update($table, $arr, $where);
} public static function del($table,$where){
return self::$db->del($table,$where);
} }

4.VIEW.class.php  视图工具类

class VIEW {

    public static $view;

    public static function init($viewtype,$config){
self::$view = new $viewtype;
/*$smarty = new Smarty();//实例化smarty
$smarty->left_delimiter=$config["left_delimiter"];//左定界符
$smarty->right_delimiter=$config["right_delimiter"];//右定界符
$smarty->template_dir=$config["template_dir"];//html模板的地址
$smarty->compile_dir=$config["compile_dir"];//模板编译生成的文件
$smarty->cache_dir=$config["cache_dir"];//缓存*/
foreach($config as $key=>$value){
self::$view -> $key = $value;
} } public static function assign($data){
foreach($data as $key=>$value){
self::$view->assign($key, $value);
}
} public static function display($template){
self::$view->display($template);
}
}

5.mysql.class.php  mysql工具类

    /**
* 报错函数
*
* @param string $error
*/
function err($error){
die("对不起,您的操作有误,错误原因为:".$error);//die有两种作用 输出 和 终止 相当于 echo 和 exit 的组合
} /**
* 连接数据库
*
* @param string $dbhost 主机名
* @param string $dbuser 用户名
* @param string $dbpsw 密码
* @param string $dbname 数据库名
* @param string $dbcharset 字符集/编码
* @return bool 连接成功或不成功
**/
function connect($config){
extract($config);
if(!($con = mysql_connect($dbhost,$dbuser,$dbpsw))){//mysql_connect连接数据库函数
$this->err(mysql_error());
}
if(!mysql_select_db($dbname,$con)){//mysql_select_db选择库的函数
$this->err(mysql_error());
}
mysql_query("set names ".$dbcharset);//使用mysql_query 设置编码 格式:mysql_query("set names utf8")
}
/**
* 执行sql语句
*
* @param string $sql
* @return bool 返回执行成功、资源或执行失败
*/
function query($sql){
if(!($query = mysql_query($sql))){//使用mysql_query函数执行sql语句
$this->err($sql."<br />".mysql_error());//mysql_error 报错
}else{
return $query;
}
} /**
*列表
*
*@param source $query sql语句通过mysql_query 执行出来的资源
*@return array 返回列表数组
**/
function findAll($query){
while($rs=mysql_fetch_array($query, MYSQL_ASSOC)){//mysql_fetch_array函数把资源转换为数组,一次转换出一行出来
$list[]=$rs;
}
return isset($list)?$list:"";
} /**
*单条
*
*@param source $query sql语句通过mysql_query执行出的来的资源
*return array 返回单条信息数组
**/
function findOne($query){
$rs = mysql_fetch_array($query, MYSQL_ASSOC);
return $rs;
} /**
*指定行的指定字段的值
*
*@param source $query sql语句通过mysql_query执行出的来的资源
*return array 返回指定行的指定字段的值
**/
function findResult($query, $row = 0, $filed = 0){
$rs = mysql_result($query, $row, $filed);
return $rs;
} /**
* 添加函数
*
* @param string $table 表名
* @param array $arr 添加数组(包含字段和值的一维数组)
*
*/
function insert($table,$arr){
//$sql = "insert into 表名(多个字段) values(多个值)";
foreach($arr as $key=>$value){//foreach循环数组
$value = mysql_real_escape_string($value);
$keyArr[] = "`".$key."`";//把$arr数组当中的键名保存到$keyArr数组当中
$valueArr[] = "'".$value."'";//把$arr数组当中的键值保存到$valueArr当中,因为值多为字符串,而sql语句里面insert当中如果值是字符串的话要加单引号,所以这个地方要加上单引号
}
$keys = implode(",",$keyArr);//implode函数是把数组组合成字符串 implode(分隔符,数组)
$values = implode(",",$valueArr);
$sql = "insert into ".$table."(".$keys.") values(".$values.")";//sql的插入语句 格式:insert into 表(多个字段)values(多个值)
$this->query($sql);//调用类自身的query(执行)方法执行这条sql语句 注:$this指代自身
return mysql_insert_id();
} /**
*修改函数
*
*@param string $table 表名
*@param array $arr 修改数组(包含字段和值的一维数组)
*@param string $where 条件
**/
function update($table,$arr,$where){
//update 表名 set 字段=字段值 where ……
foreach($arr as $key=>$value){
$value = mysql_real_escape_string($value);
$keyAndvalueArr[] = "`".$key."`='".$value."'";
}
$keyAndvalues = implode(",",$keyAndvalueArr);
$sql = "update ".$table." set ".$keyAndvalues." where ".$where;//修改操作 格式 update 表名 set 字段=值 where 条件
$this->query($sql);
} /**
*删除函数
*
*@param string $table 表名
*@param string $where 条件
**/
function del($table,$where){
$sql = "delete from ".$table." where ".$where;//删除sql语句 格式:delete from 表名 where 条件
$this->query($sql);
} }

6.include.list.php  统一导入php数组

    $paths = array(
'function/function.php',
'libs/core/DB.class.php',
'libs/core/VIEW.class.php',
'libs/db/mysql.class.php',
'libs/view/Smarty/Smarty.class.php'
);

7.pc.php  统一初始化

$currentdir = dirname(__FILE__);
include_once($currentdir.'/include.list.php');
foreach($paths as $path){
include_once($currentdir.'/'.$path);
}
class PC{
public static $controller;
public static $method;
private static $config;
private static function init_db(){
DB::init('mysql', self::$config['dbconfig']);
}
private static function init_view(){
VIEW::init('Smarty', self::$config['viewconfig']);
}
private static function init_controllor(){
self::$controller = isset($_GET['controller'])?daddslashes($_GET['controller']):'index';
}
private static function init_method(){
self::$method = isset($_GET['method'])?daddslashes($_GET['method']):'index';
}
public static function run($config){
self::$config = $config;
self::init_db();
self::init_view();
self::init_controllor();
self::init_method();
C(self::$controller, self::$method);
}
}

8.adminController.class.php  用户控制

require_once("libs/model/adminModel.class.php");

class adminController{
public function login(){
if($_POST){
//登录处理
$this->checklogin();
}else{
VIEW::display("login.html");
} } private function checklogin(){
$authobj = new adminModel;
if($authobj->loginsubmit()){
$this->showmessage("登录成功");
}else{
$this->showmessage("登录失败");
}
} private function showmessage($info){
echo "<script>alert('$info')</script>";
exit;
}
}

9.adminModel.class.php  用户模型

class adminModel{

    //定义表名
public $_table = "admin";
private $auth=""; function findByUsername($username){
$sql = 'select * from '.$this->_table.' where username="'.$username.'"';
return DB::findOne($sql);
} public function loginsubmit(){
if(empty($_POST["username"])||empty($_POST["password"])){
return false;
}
$username = addslashes($_POST["username"]);
$password = addslashes($_POST["password"]);
//用户验证操作
if($this->auth = $this->checkuser($username,$password)){
$_SESSION["auth"]=$this->auth;
return true;
}else{
return false;
}
} private function checkuser($username,$password){
//$adminobj = M("admin");
//$auth = $adminobj->findByUsername($username);
$auth = $this->findByUsername($username);
if((!empty($auth))&&$auth["password"]==$password){
return $auth;
}else{
return false;
}
}
}

10.admin.php  php入口

    header("Content-type: text/html; charset=utf-8");
session_start();
require_once('config.php');
require_once('framework/pc.php');
PC::run($config);

11.config.php  php配置

$config=array(
"dbconfig"=>array(
"dbhost" => "localhost",
"dbuser" => "root",
"dbpsw" => "root",
"dbname" => "demo",
"dbcharset" => "utf8"
),
"viewconfig" => array(
'left_delimiter' => '{',
'right_delimiter' => '}',
'template_dir' => 'html',
'compile_dir' => 'data/template_c',
"cache_dir" => "data/cache"
)
)

PHP框架_Smarty_实现登录功能的更多相关文章

  1. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(五)——实现注册功能

    使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...

  2. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(四)——对 run.py 的调整

    使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...

  3. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用

    使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...

  4. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化

    使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...

  5. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(三)——使用Flask-Login库实现登录功能

    使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...

  6. 构建NetCore应用框架之实战篇(四):BitAdminCore框架1.0登录功能细化及技术选型

    本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.BitAdminCore框架1.0版本 1.1.0版本是指最小版本 ...

  7. smm框架学习------smm框架整合实现登录功能(一)

    一.准备所需的jar包 1.1所需jar包 1.Spring框架jar包 2.Mybatis框架jar包 3.Spring的AOP事务jar包 4.Mybatis整合Spring中间件jar包 5.a ...

  8. smm框架整合实现登录功能

    一.准备所需的jar包 1.1所需jar包 1.Spring框架jar包 2.Mybatis框架jar包 3.Spring的AOP事务jar包 4.Mybatis整合Spring中间件jar包 5.a ...

  9. SpringMVC详解(四)------SSM三大框架整合之登录功能实现

    为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring.SpringMVC.MyBatis)的整合.前面讲解 MyBatis 时,写了一篇 MyBatis 和 Spri ...

随机推荐

  1. HDOJ/HDU 2560 Buildings(嗯~水题)

    Problem Description We divide the HZNU Campus into N*M grids. As you can see from the picture below, ...

  2. STL之set、multiset、functor&pair使用方法

    set是一个集合容器,其中包含的元素是唯一的,集合中的元素是按照一定的顺序排列的.元素插入过程是按照排序规则插入,所以不能使用指定位置插入. set采用红黑树变体的数据结构实现,红黑树属于平衡二叉树. ...

  3. How to install Python 2.7 and Python 3.3 on CentOS 6

    原文地址:http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

  4. system2之:4-LVM逻辑卷管理

    LVM有扩容功能,无容错功能 物理卷: [root@localhost ~]# pvscan   PV /dev/sda2   VG VolGroup   lvm2 [19.51 GiB / 0    ...

  5. 面试题 php随机获取概率结果

    题目:随机输出“苹果”,“橘子”,“香蕉”要求输出“苹果”的概率为50%,“橘子”的概率为30%,“香蕉”的概率为20% 分析 方案一: 最常用rand(1,10)来处理 如果是5以下的输出苹果 6到 ...

  6. 【Android - 进阶】之代码打包签名与混淆

    代码打包签名 Android Studio为大家集成了代码打包混淆的功能,具体操作流程如下组图所示: 1.点击Android Studio上方工具栏的  Build -> Generate Si ...

  7. UIView与CALayer的区别,很详细

    研 究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不多,看得人云山雾罩,感觉,写那篇东西的人, ...

  8. [Unity3D]Unity3D游戏开发之自己主动寻路与Mecanim动画系统的结合

    大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei. 这段时间博主将大部分的精力都放在了研究官方演示样例项目上,主要是希望能够从中挖掘出有价值的东 ...

  9. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. 用 Qt 中的 QDomDocument类 处理 XML 文件(下)

      QDomDocument doc; 1).创建根节点:QDomElement root = doc.documentElement("rootName " ); 2).创建元素 ...