Reputation: 156
I have done some researches but i cant find a proper answer
I want that button can only be clicked by admin, if that button clicked my a normal user an alert box will appeared
Or perhaps that button will only appeared if logged in as admin?
<a href="view.php" class="btn btn-info" role="button">Admin button</a>
This is my admin button ↑
My admin information from my database is:
userid: 3
username: admin
password: admin
role: 0
Should I include ↓
<?php
session_start()
include "connection.php"
if(empty($_SESSION['role'] == '0')){
echo "xxx";
}
else
{
echo "xxx";
}
?>
If so, what should i write within the "xxx"?
The connection.php will be:
<?php
$conn = mysqli_connect("localhost","root","","gallery");
if(mysqli_connect_errno())
{
die("<script>alert('Error in connection !');window.history.go(-1);</script>");
}
echo "<script>alert('Successfully connected !');</script>";
?>
Upvotes: 2
Views: 630
Reputation: 5477
You can use session variable "role" and javascript function to do that.
Javascript:
Add this script in your head or footer
<script type="text/javascript">
function checkUser(type) {
if (type == '0') { // User is Admin move to view
window.location("view.php");
} else {
alert("Hey! You are not admin");
}
}
</script>
HTML
In your page change your link to button as:
<?php
session_start();
include "connection.php" ?>
<button class='btn btn-info' role='button' onclick="checkUser(<?php if($_SESSION['role'] == '0'){
echo '0';
} else {
echo '1';
} ?>
);" >Admin button</button>
Upvotes: 0
Reputation: 3593
<?php
session_start()
include "connection.php"
if(empty($_SESSION['role'])
&& $_SESSION['role'] == '0'){
echo "<p><a href='view.php' class='btn btn-info' role='button'>Admin button</a></p>";
}
?>
It should be like above condition.
In your code, empty function and equality condition is not correct.
Upvotes: 1
Reputation: 100
If your only intentions are to hide the button for non-admin users then its very simple, and you don't need an else
logic operator for this. Since if the condition isn't met. Don't do anything
<?php
session_start()
include "connection.php"
if(empty($_SESSION['role'] == '0')){
echo "<p><a href='view.php' class='btn btn-info' role='button'>Admin button</a></p>";
}
?>
Upvotes: 1