$data[‘mytitle‘] = "My site"; $data[‘base‘] = $this->config->item(‘base_url‘); $data[‘css‘] = $this->config->item(‘css‘); $data[‘mytitle‘] = "A website to monitor other websites"; $data[‘text‘] = "Please log in here!";
$this->load->view(‘entrypage‘,$data); }
它调用视图:entrypage。 视图中包含一个表格,这个表格让用不着户输入用户名和密码。 HTML 表格需要指定一个处理$_POST的函数,我们已经把它放在start控制器中,就是 assessme() 。用HTML代码表示,在我们的视图上的表格应该是:
让我们看看代码结构以及各部分如何交互。 (注意为了要使例子简洁,我们不校验用户的输入。 当然,这不会造成问题, CI 的表格类自动为输入数据“消毒”。
/*receives the username and password fromthe POST array*/ function assessme() { $username = $_POST[‘username‘]; $password = $_POST[‘password‘];
/*calls the checkme function to see if the inputs are OK*/ if ($this->checkme($username,$password)==‘yes‘) { /*if the inputs are OK, calls the mainpage function */ $this->mainpage;() } /*if they are not OK, goes back to the index function, which re-presents the log-in screen */ else { $this->index(); } } /*called with a u/n and p/w, this checks them against some list. For the moment, there‘s just one option. Returns ‘yes‘ or ‘no‘ */
function checkme($username=‘‘, $password=‘‘) { if ($username == ‘fred‘ && $password ==‘12345‘) { return ‘yes‘; } else { return(‘no‘; }
CI 有一个会话类来处理会话相关工作。 事实上,它大大减少了编码量。我们在上一章学习到 CI 有各种各样的library,大大简化了PHP的编程。 这些就是框架的核心: 集成大量代码,为你提供各种强大的功能。你要做的只是使用它们,而不必关心他们是如何运作的。作为结果,你的应用使用了最专门的代码,而你却不需要自己去编写它们!
检查你的system/application/config/config.php 文件,你会找到像这样的一个部分: -------------------------------------------------------------------------- | Session Variables |-------------------------------------------------------------------------- | | ‘session_cookie_name‘ = the name you want for the cookie | ‘encrypt_sess_cookie‘ = TRUE/FALSE (boolean). Whether to encrypt the cookie | ‘session_expiration‘ = the number of SECONDS you want the session to last. | by default sessions last 7200 seconds (two hours). Set to zero for no expiration. | */ $config[‘sess_cookie_name‘] = ‘ci_session‘; $config[‘sess_expiration‘] = 7200; $config[‘sess_encrypt_cookie‘] = FALSE; $config[‘sess_use_database‘] = FALSE; $config[‘sess_table_name‘] = ‘ci_sessions‘; $config[‘sess_match_ip‘] = FALSE; $config[‘sess_match_useragent‘] = FALSE;
$fred = $this->agent->is_robot(); if ($fred == TRUE) {$agent = $this->agent->agent_string(); /*add code here to store or analyse the name the user agent is returning*/ }
在 CI框架中 ,会话机制保存那发出请求的 IP 地址,因此你可以使用这个功能维护一个网站的黑名单。可以用如下代码取回来自会话的 IP:
/*remember to load the library!*/ $this->load->library(‘session‘); /*look for an ‘ip_address‘ variable in the contents of the session cookie*/ $ip = $this->session->userdata(‘ip_address‘);