Felix Faal Felix Faal - 5 months ago 8
PHP Question

PHP - IF statement either regular user OR admin have access

To view news section in my website you must be logged in (either as a regular account = login_user or admin = admin ) and you can see the uploaded news in chronological order. If you are not logged in as either conditions, direct to somewhere else. HOWEVER the code is thinking none of the conditions are met even when logged in with regular account or admin account and simply directing me elsewhere...

<?php
error_reporting(0);
session_start();
if(!isset($_SESSION['admin']) || !isset($_SESSION['login_user']))
{
?>
<div class = "api" style="position: absolute;
top: 140px;
right: 400px;
width: 730px;
height: 400px;word-wrap: break-word;">

<h2 align = "center"> <font color ="red"> You Do Not Have Permission To View this Page. </font> <br><br> Your Options Are:
<br><br><br>
<a href="index.php"> Create and Register for A New Account </a> <br> <br> <br> OR <br> <br> <br> <a href="indexmember.php">Log In With An Existing Account. </h2>
</div>


<?php
}
elseif(isset($_SESSION['admin']) || isset($_SESSION['login_user']))
{
include 'connect.php';
?>
<div class = "api" style="position: absolute;
top: 10px;
right: 220px;
width: 1200px;
height: 3200px;word-wrap: break-word;" id="easyPaginate">


<?php
$query = mysqli_query($con,"select * from news order by date DESC");
while($r = mysqli_fetch_array($query))
{
?>
<section class="propertypage">
<br> <br>
<h4><?php echo "<b><u>Title</u>: </b>".$r['Title']; ?></h4>
<h4><?php echo "<b><u>Detail</u>: </b>".$r['story']; ?></h4>
<h4><?php echo "<b><u>Published Date</u>: </b>".$r['DATE']; ?></h4>
<img src="<?php echo $r['image']; ?>" height="600px" width="1200px"/>
<br> <br> <br>
</section>
<?php
}
?>
</div>

<?php
}
?>

Answer

You want:

if(!isset($_SESSION['admin']) && !isset($_SESSION['login_user']))
    // ....

This is saying if the user isn't an admin and the user isn't a login user, then they must not have the privilege.