下面是所谓的"菜单"小练习,有四个文件,其中的mainpage.php不变,增加了一个menu库文件,放在Application/libraries目录下。entrypage.php增加了一点东西,变化也不是很大。
entrypage.php:
<html>
<head>
<title><?=$mytitle?></title>
</head>
<body>
<?=$menu?><br><br>
<?=$text ?>
<?=form_open('start/assessme');?>
<input type="text" name="username" value="username"/>
<input type="text" name="password" value="hi"/>
<input type="submit" value="submit" />
<input type="reset" value="reset"/>
</form>
</body>
</html>
menu.php:
<?php
class Menu
{
function show_menu()
{
$obj=& get_instance();
$obj->load->helper('url');
$menu=anchor("start/hello/fred","Say hello to Fred |");
$menu.=anchor("start/hello/bert","Say hello to Bert|");
$menu.=anchor("start/another_function","Do something else|");
return $menu;
}
}
?>
start:
<?php
class Start extends CI_Controller
{
function __construct()
{
parent::__construct();
// $this->output->enable_profiler(TRUE);
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('unit_test');
$this->load->library('menu');
$this->unit->active(TRUE);
$this->load->database();
}
function index()
{
// $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!";
$mymenu=$this->menu->show_menu();
$data['menu']=$mymenu;
$this->load->view('entrypage',$data);
}
function hello($name)
{
echo $name;
}
function another_function()
{
echo "you are here!";
}
function assessme()
{
$this->load->library('session');
$name=$_POST['username'];
$password=$_POST['password'];
if($name=='fred' && $password=='12345')
{
$this->benchmark->mark('here_start');
$newdata=array('status'=>'ok');
$this->session->set_userdata($newdata);
echo $this->session->userdata('ip_address')."<br>";
echo $this->session->userdata('session_id')."<br>";
echo $this->session->userdata('user_agent')."<br>";
echo $this->session->userdata('last_activity')."<br><br>";
$currentsession=array('ip_address'=>$this->session->userdata('ip_address'),
'session_id'=>$this->session->userdata('session_id'),
'user_agent'=>$this->session->userdata('user_agent'),
'last_activity'=>$this->session->userdata('last_activity'));
$this->db->insert('ci_sessions',$currentsession);
$this->benchmark->mark('here_end');
echo $this->benchmark->elapsed_time('here','there')."<br>";
// $this->output->enable_profiler(TRUE);
$this->mainpage();
}
else
{
$this->index();
;}
}
function mainpage()
{
$test=floor(1.56);
$expected_result=1;
$test_name='test php floor function';
$this->unit->run($test,$expected_result,$test_name);
$test=floor(2.56);
$expected_result=1;
$test_name='tests php floor function';
$this->unit->run($test,$expected_result,$test_name);
//echo $this->unit->result();
$this->load->view('mainpage');
}
}
?>