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!
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.
Write your condition as below:-
session_start();
if (isset($_SESSION["UserID"])){
if($_SESSION["UserID"] === 1){
include('../includes/navAdmin.inc.php'); die;
}else{
header('Location: Login.php'); die;
}
}else{
header('Location: Login.php'); die;
}