我在项目中工作并使用Codeigniter PHP Framework对其进行编码,我想要做的是我想制作一个动态导航菜单,一个普通菜单供客人Home - Register - Login ...
和另一个菜单到注册用户options menu and so on ...
以下是我的标题模板的代码:
<?php
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
?>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Hey, <?php echo $data['username']; ?></a>
<ul class="dropdown-menu">
<a href="<?php echo base_url(); ?>/login/logout">Déconnexion</a>
</ul>
</li>
</ul>
<?php }else{ ?>
<ul class="nav navbar-nav navbar-right">
<li><a href="<?php echo base_url(); ?>User">Inscrivez-vous</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Connectez-vous <b class="caret"></b></a>
<ul class="dropdown-menu ins">
<?php echo form_open("verifylogin"); ?>
<input type="text" style="margin: 5px 0;" id="username" name="username" class="form-control" placeholder="Nom d'utilisateur"/>
<input type="password" style="margin: 5px 0;" id="password" name="password" class="form-control" placeholder="Mot de passe"/>
<input type="submit" class="btn btn-primary" value="Connexion" />
<?php echo form_close(); ?>
<p><a href="#">Mot de passe oublier?</a></p>
</ul>
</li>
</ul>
<?php } ?>
它的工作方式如下,但另一个问题已经显示,内容和页脚部分在页面中显示两次,这里是问题的图片:< / p>
https://docs.google.com/file/d/0B2keUK5KhVo8bDk1OU1yQ2xfU0k/edit?usp=drivesdk
这是我的主页控制器
session_start(); //we need to call PHP's session object to access it through CI
class Pages extends CI_Controller {
public function view($page = 'home')
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('templates/header');
$this->load->view('pages/home');
$this->load->view('templates/footer');
}/*
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}*/
$this->load->view('templates/header');
$this->load->view('pages/home');
$this->load->view('templates/footer');
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('/', 'refresh');
}
}
答案 0 :(得分:0)
我认为你错误地控制了你的逻辑。 如果用户已登录,您的视图将被加载两次。 用这个覆盖你的视图功能:
public function view($page = 'home')
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('templates/header');
$this->load->view('pages/home');
$this->load->view('templates/footer');
}