Codeigniter 入门(3)登录

<p> &nbsp; <span style="font-family:楷体, 楷体_gb2312, simkai;">用户登录操作,先上登录页面图</span> </p> <p> <img src="/Uploads/article/20130426/1366965957803.png" title="1.png" /> </p> <p> &nbsp; <span style="font-family:楷体, 楷体_gb2312, simkai;">登录成功后</span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"><img src="/Uploads/article/20130426/1366965984580.png" title="2.png" /><br /> </span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"><br /> </span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"> &nbsp; 好,操作步骤如下:</span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;font-size:24px;">1、配置数据库文件</span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"> &nbsp; &nbsp;将application/config/database.php文件修改如下<br /> </span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"></span> </p> <pre class="brush:as3;toolbar:false;">$db['default'] = array( 'dsn' =&gt; '', 'hostname' =&gt; 'localhost', #数据库地址 'username' =&gt; 'root', #数据库用户名 'password' =&gt; 'xxxxxx', #密码 'database' =&gt; 'example', #数据库名 'dbdriver' =&gt; 'mysqli', #数据库类型 'dbprefix' =&gt; '', 'pconnect' =&gt; TRUE, 'db_debug' =&gt; TRUE, 'cache_on' =&gt; FALSE, #是否开启数据库缓存 'cachedir' =&gt; '', 'char_set' =&gt; 'utf8', 'dbcollat' =&gt; 'utf8_general_ci', 'swap_pre' =&gt; '', 'autoinit' =&gt; TRUE, 'encrypt' =&gt; FALSE, 'compress' =&gt; FALSE, 'stricton' =&gt; FALSE, 'failover' =&gt; array() );</pre> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;">里面的字段名手册里面有说明..<br /> </span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;font-size:24px;">2、登录页面</span> </p> <p> <br /> </p> <pre class="prettyprint lang-js"> <pre class="prettyprint lang-html"> Welcome to CodeIgniter!用户名: 密码 : 验证码 :[removed]alert("");[removed] #其中form_open会提交到application/controllers/welcome类的login方法 </pre> </pre> <p> <br /> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;font-size:24px;">3、控制器</span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"> &nbsp; &nbsp;</span> </p> <pre class="brush:as3;toolbar:false;"><!--?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { public function index() { $out_datas=$this--->_captcha(); $this-&gt;load-&gt;view('welcome_message',$out_datas); } private function _captcha(){ $this-&gt;load-&gt;helper("my_captcha");//加载验证码 $vals = array( 'img_path' =&gt; './captcha/', //验证码图片存放的地址 'img_url' =&gt; base_url()."/captcha/", //图片访问的路径 'img_width' =&gt; '60', //图片的宽度 'img_height' =&gt; 20, //高度 'expiration' =&gt; 1, //存放时间,1分钟 'word_length'=&gt; 4 //显示几位验证数字 ); $cap = create_captcha($vals); $out_datas["v_img"]=$cap["image"]; //生成的图片文件 $out_datas["v_word"]=$cap["word"]; //生成的验证码,也可放入session中管理 return $out_datas; } public function login(){ $randCode=$this-&gt;input-&gt;post("rand");//生成的验证码,我这里直接写在页面中,然后传进来 $inputCode=$this-&gt;input-&gt;post("word");//手动输入的验证码 $data=$this-&gt;_captcha(); //当出错时,返回登录页面所需要的验证码 if($inputCode==$randCode){ $where["username"]=$this-&gt;input-&gt;post("uname");//用户名 $where["password"]=md5($this-&gt;input-&gt;post("pass")); //用户密码 $userObj = $this-&gt;mcom_model-&gt;query_one_object(mymsg::XT_PERSONNEL,$where);//判断输入是否正确 if(empty($userObj)){ //用户名或密码错误 $data['msg']=mymsg::USER_PASS_ERROR; $this-&gt;load-&gt;view("welcome_message", $data); return; }else if($userObj-&gt;islock==mymsg::INFO_SUO_DING){//用户被锁定 $data['msg']=mymsg::USER_LOCK; $this-&gt;load-&gt;view("welcome_message", $data); return; } $this-&gt;session-&gt;set_userdata($userObj); //将用户数据存放session中 $this-&gt;load-&gt;view("success"); //跳转到指定的页面 }else{ $data["msg"]=mymsg::CAPTCHA_ERROR; //验证码不一致 $this-&gt;load-&gt;view("welcome_message", $data); } } }</pre> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;">在login方法中使用到了二个文件</span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"> &nbsp; &nbsp;1、mcom_model.php &nbsp; &nbsp; &nbsp;:封装了Codeigniter的CRUD操作,点击下载-</span><img src="http://www.xiuxiandou.com/resource/blog/ueditor/dialogs/attachment/fileTypeImages/icon_txt.gif" /><a href="/Uploads/article/20130426/13669664268637.txt">mcom_model.txt</a>,<span style="font-family:楷体, 楷体_gb2312, simkai;">mcom_model.php存放在application/models/目录下</span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"> &nbsp; &nbsp;2、mymsg.php &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :存放着一些常量,存放在application/libraries/目录下</span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"><br /> </span> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"> &nbsp; &nbsp;mymsg.php内容如下</span> </p> <pre class="brush:as3;toolbar:false;"> <pre class="prettyprint lang-php">session-&gt;userdata("username")?&gt; 登录 !</pre> <br /> <p> <br /> </p> </pre> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"><br /> </span> </p> <p> <br /> </p> <p> <span style="font-family:楷体, 楷体_gb2312, simkai;"><br /> </span> </p>