dongweihuan8610 2015-02-05 01:48
浏览 37

使用PHP登录自定义CMS后,重定向到其他页面无效

I'm making a custom Content Management System for a website I built, and so far everything is looking good up until this road block. I got my code to work to where from login.php page you can enter in the username and password and it successfully redirects me to the index.php page but then I decided that I need to redirect anyone who isn't logged in to login.php before looking at other pages on my CMS.

So in comes this code right here...

<?php require_once("lib/sessions.php");
      require_once("lib/functions.php");
      require_once("meta.php");
      login_confirm(); 
?>

That is at the top of each page in my CMS and that login_confirm function looks like this...

function login_confirm() {
    if(!logged_in()) {
        redirect_to("login.php");
    }
}

function logged_in() {
    return isset($_SESSION['admin_id']);
}

The $_SESSION['admin_id'] gets set on my login page like this...

$found_admin = attempt_login($username, $password); 
    if($found_admin) {
        //Success, mark user as logged in
        $_SESSION["admin_id"] = $found_admin["id"];
        $_SESSION["username"] = $found_admin["username"];
        redirect_to("index.php"); 
    } else {
        //failed
        $_SESSION["message"] = "Log in failed"; 
    }

And like I said in the beginning, the login page will successfully redirect you to index.php no problem, but that was before I put login_confirm() at the top of my pages. Now no matter what I always get redirected to login.php even though I typed in the right credentials. I assume the Session isn't saving or something...

Edits: session.php looks like this. So the session is being started at the top of the page.

<?php 
session_start(); 
ob_start();
//function for message and errors
?>
  • 写回答

1条回答 默认 最新

  • dua55014 2015-02-05 01:58
    关注

    I don't see that You are using session_start(); Before using $_SESSION You must initialize it.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真