简体   繁体   English

通过登录防止直接URL访问页面

[英]Prevent direct url access of pages by login

How to prevent direct access of pages without login CODEIGNITER? 如何在不登录CODEIGNITER的情况下阻止直接访问页面?

I make this on [views > Nav] 我在[views> Nav]上做这个

It's working for preventing user when they wrong input their username. 它可以防止用户输入用户名时出错。 But when I clicked home, it's success without password *sigh 但是当我点击回家时,没有密码*叹息就成功了

<?php
if($_SESSION['username']==FALSE){
        redirect(base_url("usr/login"));
 }
?>

However I tried 但是我试过了

<?php
if($_SESSION['username']||$_SESSION['password']==FALSE){
        redirect(base_url("usr/login"));
  }
?>

But the result 但结果

$route['default_controller'] = 'main/home';

This pages sometimes comes to an error "has to many redirect loop" neither slow loading pages. 这个页面有时会出现错误“必须有很多重定向循环”,既没有慢速加载页面。 And now it's preventing me from accessing homepage even with both username and password exist 现在即使存在用户名和密码,它也阻止我访问主页

First Create a Model and add a construct 首先创建一个模型并添加一个构造

function __construct()
{
    parent::__construct();
    if($_SESSION['username']==FALSE)
    {
        redirect(base_url("usr/login"));
    }
}

then 然后

Call this model to your controller every time you want to check if the user is login 每次要检查用户是否登录时,都要将此模型调用到控制器

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

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