dszpyf4859 2016-02-02 17:02
浏览 48
已采纳

如果isset userid等于1?

I have small issue! I current have a UserID number coming from a login page, Can I not do something like this?

This works:

session_start();
if (isset($_SESSION["UserID"])){
}
    include('../includes/navAdmin.inc.php');
}
else {
    header('Location: Login.php');
}

But I want to do something more like this to restrict links to certain users etc:

session_start();
if (isset($_SESSION["UserID"])){
}else if (isset($_SESSION["UserID"] === 1){ <---this one to give the "admin" the admin page etc
    include('../includes/navAdmin.inc.php');
}
else {
    header('Location: Login.php');
}

Seems like I can't or the syntax is wrong perhaps? Can someone point me in the right direction please?

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dounieliang4712 2016-02-02 17:06
    关注

    You will need to modify your code to check if it is set and equal to 1.

    if (isset($_SESSION["UserID"]) && $_SESSION["UserID"] === 1)
    

    rather than the else if.

    Just set an else condition after that if it is not set or equal to 1.

    On another note, add exit; after header. If you have more code below that, it way want to continue to execute.

    As per the manual:

    <?php
    header("Location: http://www.example.com/"); /* Redirect browser */
    
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    ?>
    

    Sidenote: As spotted/stated in comments by thanksd. There is an extra brace here, if that is your actual code.

    if (isset($_SESSION["UserID"])){
    }
        include('../includes/navAdmin.inc.php');
    } // Right there
    else {
        header('Location: Login.php');
    }
    

    and that would have thrown you an unexpected end of file notice having error reporting set to catch/display.

    You may have meant to do:

    if (isset($_SESSION["UserID"])){
        include('../includes/navAdmin.inc.php');
    }
    
    else {
        header('Location: Login.php');
    }
    

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // Then the rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?