How do I make something disappear for everyone besides the admin, I have this code but it only works when you are not logged in when I log in with another user it keeps showing me.
<?php
if( isset($_SESSION['username']) == "admin"){
}else{
echo "<style> .add{display: none} </style>";
}
?>
There are two steps, First you check if username isset, then you compare it. In your code, you are comparing a boolean returned by isset to a string that is "admin", try this:
if (isset($_SESSION['username']) {
// username is set
if ($_SESSION['username'] == "admin") {
// user is admin
} else {
echo("<style> .add {display:none} </style>");
}
}
Also if it is something you want to not have in the page, using display: none is very shallow. Someone can just use inspect element to style it and get it to show.
Related
Before we start, I'm a beginner. I'm sure it's a simple solution but just not for me.
I'm using a session so no one can't access an admin page unless they're an admin.
The code is:
if ( $_SESSION['admin'] != 1 ) {
$_SESSION['message'] = "You're not admin!";
header("location: ../error.php");
}
Which works perfectly. regular users can't access it but admins can. BUT.
In my main cpanel I want to have a button linking to the admin panel that will only appear if the user is admin. I did the same piece of code:
if ( $_SESSION['admin'] != 1 ) {
echo '<button class="butto button-bloc" name="admin"/>Admin Panel</button>';
}
Problem is, it seems like the button shows up whether the user is an admin or not and now I think that my code is just trying to make me blow my lid as I spent a full hour trying to find what the f-- is on the go.
Any help would be greatly appreciated! I'm sure someone who's good with PHP will have it solved in under a minute lol
The condition you have written is same for user who is admin and not an admin
Please check the condition
For having a button linking to the admin panel that will only appear if the user is admin.
if ( $_SESSION['admin'] == 1 ) { // you have written $_SESSION['admin'] != 1
echo '<button class="butto button-bloc" name="admin"/>Admin Panel</button>';
}
You can use
if (isset($_SESSION['admin']) && ($_SESSION['admin'] == 1 )) {
echo '<button class="butto button-bloc" name="admin"/>Admin Panel</button>';
}
This way the button will show when you set the $_SESSION['admin'] and if it is TRUE (i.e. 1).
Try This:
if (isset($_SESSION['admin']) {
if ( ($_SESSION['admin'] == 1) ) {
echo '<button class="butto button-bloc" name="admin"/>Admin Panel</button>';
}
}
I want to hide button in search menu to specific page for normal users so they can't access to page witch is for administrators only. i'm new at writing this code so i'm asking you guys who have much more knowledge than me for help.
I want to hide File/page name upload.php for normal users and show for administrators only
Is anyone know how could i do this with php?
i'm really appreciate for every help. Thank you!
You can capture user type of user's in session. And, according to user type show / hide button.
<?
if(S_SESSION['userType'] == 'Admin') {
//Show
}
if(S_SESSION['userType'] == 'User') {
//Hide
}
?>
It's hard to give an advice without your context, but an simple example for your scenario:
// Normal Page
if($_SESSION['user_level'] > x){
//show button
}
// Admin-Only-Page
if($_SESSION['user_level'] < x){
die("Access denied");
}
Depends of how you define the administrator, if he is the user whose the Id is 1 for example, use the following script:
<?php
$current_user_id = // get it from the session if user is logged in
If ($current_user_id == 1):
?>
<input ...>
<?php endif; ?>
For that you need to set one flag in Database as user is admin or normal user.
after that in your code check with condition that logged in user is admin or not;
for example admin role is 1 then:
if($user->role == 1){
// your button code
}
simple, Enjoy :)
for example, you can use following Conditional statement :
if ($user -> role == 1){
// `enter code here`
}
I am using the minimal version of php-login from: http://www.php-login.net/ on Windows 7. When the user logs in, the login is verified and the response from the verification is True or False. The response is returned to index.php which has this code
if ($login->isUserLoggedIn() == true) {
include("views/logged_in.php");
I would like to give the user an opportunity to choose which page they go to after logging in but that page can only be accessible when the user is logged in. Any ideas? Let me know if you need more information or if what I am trying to do isn't clear. Thanks
======= UPDATED BELOW =========================
I modified the code below so that if the user login validation returns true, the user is directed to a page that has these two links Green Blue when the user clicks a link the user gets an Access Forbidden error message.
if ($login->isUserLoggedIn() == true) {
include("views/choose-pages.htm");
} else {
include("views/not_logged_in.php");
}
You can modify not_logged_in.php and have 2 submit buttons like Google, normal "Google search" and "I'm feeling luckey".
<input type="submit" name="login" value="Log in green" />
<input type="submit" name="login" value="Log in blue" />
then modify index.php
if ($login->isUserLoggedIn() == true) {
if ($_POST["login"] == "Log in green") {
include("green.php");
} else {
include("blue.php");
}
}
Well, try to create a main page (main.php), in this page write the code of choosing a page, its easy !!
if ($login->isUserLoggedIn() == true) {
include("views/main.php");
}
You could store your pages names/links in mysql database first and then get the pages links/names from the mysql database in an array and put it right after your code which would excute after the users are logged in. like so:
if ($login->isUserLoggedIn() == true) {
///Your array of pages here ///
EDIT:
Based on your comment, you can do this:
if ($login->isUserLoggedIn() == false) {
header(location someotherpage.php)
You could put that code in the blue.php, green.php etc etc
Second edit:
I hope i understand it correctly and if so, then you can use something like this:
if ($login->isUserLoggedIn() == false) {
echo "access denied";
you could even populate your page names and links in a drop down menu fashion so the users can select one and go to that page.
I have the following code which I use in conjunction with a members script which displays a members username the page or asks guests to login or register.
PHP code:
if ($_SESSION['username'])
{
echo "".$_SESSION['username'].", you are logged in.<br><small>Click here to logout</small>";
}
else
echo "Welcome Guest!<br><small>Login or Register</small>";
It works perfectly well, though now I want to modify it so if a user with admin privileges logs in it identifies the username and offers a link to the admin page.
So here's my modified code:
<? php
$validateadmin = $_SESSION['username'];
if ($validateadmin == "admin1" or $validateadmin == "admin2")
{
echo "Hello $validateadmin, you have admin privileges.<br><small>Click here to logout</small>";
}
else if ($_SESSION['username'])
{
echo "".$_SESSION['username'].", you are logged in.<br><small>Click here to logout</small>";
}
else
{
echo "Welcome Guest!<br><small>Login or Register</small>";
}
?>
Any idea's what I'm doing wrong? It either leaves me with a blank page or errors.
I know it's probably a newbie error but for the life of me I don't know what's wrong.
Generally you should use elseif in php not "else if" because the php parser will interpret else if as else { if { .... }} and you can have some weird errors.
Also, it is a great practice to ALWAYS use braces with control statements to avoid dangling clauses.
Also to avoid notices about array indexes don't do checks like if($array[$index]) if the index may not exist. Use any of array_key_exists, isset, empty, etc (they all are slightly different) to check if an array contains a key you are looking for.
try the following
<?php #removed space
session_start(); #you will need this on all pages otherwise remove it if already called
$validateadmin = $_SESSION['username'];
if($validateadmin == "admin1" || $validateadmin == "admin2"){
echo "Hello $validateadmin, you have admin privileges.<br><small>Click here to logout</small>";
}elseif(isset($_SESSION['username'])){ #you should use isset to make sure some variable is set
echo $_SESSION['username'].", you are logged in.<br><small>Click here to logout</small>";
}else{
echo "Welcome Guest!<br><small>Login or Register</small>";
}
?>
Please tell me a way on how to make that if the user has not logged-in the site, he cannot see specific text, instead he will see something like "This content is only for registered users. To register click here." Or something around that way...
Thanks!
set SESSION['user_id'] value for the logged user
and u apply the logic on that specific div
if(!isset(SESSION['user_id']) || empty(SESSION['user_id']))
{
echo "Please <a href='register.php'>login/register</a> to view this content";
}
else
{
<div>
// your content and whatever
</div>
}
Pass your text from some conditions and check before echoing it. For an example
Check if the user is logged in though session variable directly or you can create method/function for this
if (is_user_logged_in() === TRUE)
{
echo $content;
}
else
{
echo "This content is only for registered users. To register click here."
}
This is just a simple example.