简体   繁体   English

codeigniter 空会话导航模板

[英]codeigniter empty session navigation template

I have this code on a navigation template.我在导航模板上有这个代码。

 <a href="<?php  
                    if(!empty($session)){
                    echo site_url('/pages/view/profile');
                    }else{
                    echo site_url('/pages/view/login'); }?>">

                    <?php if(!empty($session['username'])){ echo 'Welcome '.$session['username'];}else{ echo 'Account'; }?></a>

I already declared $session = $this->session->userdata();我已经声明$session = $this->session->userdata(); inside the navigation container.导航容器内。

It's really simple.这真的很简单。 I just wanted the Account Link to change to "Welcome " if a user is logged hence check if $session['username'] is empty or not.如果用户已登录,我只是希望帐户链接更改为“欢迎”,因此检查$session['username']是否为空。 This part works.这部分有效。 The first part though, it's quite troublesome.不过第一部分还是挺麻烦的。

I wanted it to link to 2 different views, I have a view function on my pages controller that handles views.我希望它链接到 2 个不同的视图,我的页面控制器上有一个处理视图的视图函数。

IF a user is logged in hence !empty($session) it will redirect to the profile view, if not, it will redirect to log in view.如果用户已登录,则!empty($session)它将重定向到个人资料视图,如果没有,它将重定向到登录视图。

The problem is that, it always redirects to profile view even when i deleted the cookies of my browser(i still havent gotten around to doing a logout function so i opted to just delete the cookies as a means of log out to check my code).问题是,即使我删除了浏览器的 cookie,它也总是重定向到配置文件视图(我还没有开始执行注销功能,所以我选择只删除 cookie 作为注销的一种方式来检查我的代码) . It should redirect me to my log in page but will still redirect me to my profile page.它应该将我重定向到我的登录页面,但仍会将我重定向到我的个人资料页面。

What am i doing wrong?我究竟做错了什么?

I think i got it.我想我明白了。 Even when i deleted cookies, i printed the $session and it displayed Last_regenerate and some value.即使我删除了 cookie,我也会打印 $session 并显示 Last_regenerate 和一些值。 So i guess it meant that when i inputted !empty($session) it always returned false.所以我想这意味着当我输入!empty($session)它总是返回 false。 Thus the $session array wasn't empty the whole time.因此 $session 数组一直不是空的。

So instead of !empty($session) i just used !empty($session['username']; .所以,而不是!empty($session)我只是使用!empty($session['username'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM