Reputation: 73
I wrote a simple login system and that is working. I've set up a few pages that are only viewable when logged in. I want to restrict a page to specific session IDs. How would I go about doing that? This is what I'm using to restrict pages right now:
<?php
session_start();
if (!isset($_SESSION['u_id'])) {
header("Location: ../index.php?index=mustlogin");
exit();
}
?>
How would I restrict this from any u_id to a specific u_id?
Upvotes: 1
Views: 1347
Reputation: 1382
You need to match your $_SESSION['uid'] with your specific id. For that you need some kind of data for specific user id. There are multiple approach to do this but I would do this with array. What you need is an array of your specific ids
//Should've come from database of your users
$specific= array(
"id" => 1
);
And then just search in array through in_array()
if (!in_array($_SESSION['u_id'], $specific)) {
header("Location: ../index.php?index=mustlogin");
exit();
}
Upvotes: 1
Reputation: 3302
You can create an array of specific ids and then use in_array to validate user.
Example
<?php
session_start();
$sessionIds = array('1','2'); //for example i have inserted 1 and 2 as ids
if (!isset($_SESSION['u_id']) || in_array($_SESSION['u_id'], $sessionIds)) {
header("Location: ../index.php?index=mustlogin");
exit();
}
Explanation
Here i created an array $sessionIds of specific ids that will not allow to access page. then cheking with in_array that current session user id exist in $sessionIds array then redirect to user.
Upvotes: 1