Hide site link using $_SESSION - php

I have two users one is 'admin' and 'user'. I need to when I login as a 'user' hide the some url.
my add_sales.php file include some code
<div class="side-menu fl">
<h3>Sales Management</h3>
<ul>
<?php if($_SESSION['usertype'] == 'admin' ) { ?>
<li>Add Sales</li>
<li>View Sales</li>
<?php } ?>
<?php else if($_SESSION['usertype'] == 'user' ) { ?>
<li>Add Sales</li>
<?php } ?>
</ul>
</div> <!-- end side-menu -->
when I access as a 'user' I use this code for use user to if($_SESSION[usertype']=='user') I don't know this is right o wrong please can some one help
me..
my cheacklog.php is
if($count==1){
$row = mysql_fetch_row($result);
$_SESSION['id']=$row[0];
$_SESSION['username']=$row[1];
$_SESSION['usertype']=$row[3];
if($row[3]=="admin"){
header("location:dashboard.php");
}
else if($row[3]=="user")
{
header("location:dashboard.php");
}
}
?>

You can write it in line. This link will be inserted into the page only if the user has admin status:
<ul>
<li>All users will see this link</li>
<?php
if ($row[3]=="admin") {
echo "<li><a href='adminStuff.php'>hidden link</a></a>";
}
?>
</ul>
I am pretty sure that is what you meant, or if you want to do conditional redirects, I think you already have that done.

Try this :
<?php if ($_SESSION['userType'] == 'admin'): ?>
<a>Only admin can see this. </a>
<?php else if($_SESSION['userType'] == 'user' ): ?>
<a>Only user can see this</a>
<?php endif ?>

Related

Issue in pulling pages from the database

I am having problems pulling the pages through in PHP and HTML I have used :-
<li>
<!-- Pulling Categories from the database
dynamically -->
<?php
$nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject =
mysqli_fetch_assoc($nav_subjects)) {
?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
Which pulls the categories dynamically from the database and displays them with a drop down arrow just how I wanted but the pages will not show underneath them here's the code I have used for that bit:-
<!-- Categories listed correctly let's pull the pages for each one -->
<?php
if($nav_subject['id'] == $subject_id) {
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) {
?>
<ul>
<li>
<?php echo h($nav_page['menu_name']); ?>
</li>
</ul>
<?php } // while $nav_pages
} // if($nav_subject['id'] == $subject_id)
} // while $nav_subjects ?>
</li>
<?php
mysqli_free_result($nav_subjects);
mysqli_free_result($nav_pages);
?>
I am pulling in the SQL from another page which is loaded correctly as the categories load and display correctly.
I will be grateful for any ideas.
I have also tried to echo back the sql result but nothing is shown.
I have now got it working with the following code:-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject = mysqli_fetch_assoc($nav_subjects)) {?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<li><?php echo h($nav_page['menu_name']); ?></li>
<?php } ?>
<?php } ?>
</ul>
But now it is listing the secondary subjects as a child of the first instead of individual subjects of their own.
*********Resolved**********
please advise if you think the code could be better i've currently used :-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);?>
<?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)){?>
<span class="opener"><?php echo h($nav_subject['menu_name']);?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<a href="<?php echo url_for('index.php?id=' . h(u($nav_page['id']))); ?>">
<?php echo h($nav_page['menu_name']); ?></a>
<?php } ?>
<?php mysqli_free_result($nav_pages); ?>
</ul>
<?php } ?>
<?php mysqli_free_result($nav_subjects); ?>
</li>

PHP ifelse display white screen of death

My code doesnt work properly and it does now shows any error or any clue, what am I missing here ? any hints ? TIA
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php } ?>
<?php
elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
One unnecessary <?php and else if is in wrong way, do like below:-
<?php if(isset($_SESSION['user_session_organizer'])){ ?>
<li>VIEW TRIPS</li>
<?php } elseif(isset($_SESSION['user_session'])){?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
Note:-
Always add
error_reporting(E_ALL);ini_set('display_errors',1);
on top of your each php page to prevent yourself from a situation like:-"White Screen Of Death"
(It will on the error reporting setting for all type of errors and every error will displayed on the page if any occur).
Thanks
Try this, your else if is wrong ?> is a close tag and then the compiler dont understand the elseif, better this:
<?php if(isset($_SESSION['user_session_organizer']))
{ ?>
<li>VIEW TRIPS</li>
<?php }elseif(isset($_SESSION['user_session']))
{
?>
<li>VIEW TRIPS</li>
<?php }else{ ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php } ?>
This is easier to read and to debug:
<?php if (isset($_SESSION['user_session_organizer'])) : ?>
<li>
VIEW TRIPS
</li>
<?php elseif (isset($_SESSION['user_session'])) : ?>
<li>
VIEW TRIPS
</li>
<?php else : ?>
<li>
<a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a>
</li>
<?php endif; ?>
Check about control structures.
try this, its neat and simple code.
<?php if (isset($_SESSION['user_session_organizer'])): ?>
<li>VIEW TRIPS</li>
<?php elseif (isset($_SESSION['user_session'])): ?>
<li>VIEW TRIPS</li>
<?php else: ?>
<li><a data-toggle="modal" href="#organizer" class="smoothScroll">ORGANIZE TRIPS</a></li>
<?php endif; ?>

Conditional Login Link - Wordpress - PHP

Thanks in advanced for any help provided and taking the time to review my question. I basically just have a Wordpress site that once you login, you see your name and avatar in the top right corner and when you hover over your name an "Edit Profile" and "Log out" link appear. All I want is for a "Login" link to be in that same spot when you're not logged in. (This saves me from having to put a login widget in the sidebar).
I was able to track down the PHP file that holds that specific piece of code and find the spot where the user menu is setup. I'm just not sure what piece of code I could add so that there's a login link there that leads to mysite.com/login/. Here is the current code:
<?php }
//user menu
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
Here's an image to show you what I mean as well. Top is logged out, bottom logged in.
http://6chill.com/loli.jpg
Thanks again for any help that you're able to provide and if you require further details just let me know and I'll do my best to provide them.
Thanks for the prompt reply everyone :) As requested, here's more of the code to ensure nothing was left out.
<?php }
//user menu
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
</ul>
</li>
<?php }?>
<?php //submit menu
if(ot_get_option('user_submit',1)) {
$text_bt_submit = ot_get_option('text_bt_submit');
if($text_bt_submit==''){ $text_bt_submit = 'Submit Video';}
if(ot_get_option('only_user_submit',1)){
if(is_user_logged_in()){?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php }
} else{
?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php
}
} ?>
I hope that's what you're looking for. Where you see the comment "//submit menu", that's where the "Submit Video" button comes into play. Thanks so much again guys, you're freaking awesome!
Please try this code, I think this will help you
<?php
if ( is_user_logged_in() )
{ ?>
LOGOUT
<!-- add another action as per your requirement-->
<?php } else
{ ?>
<a class="login" href="<?php echo site_url(); ?>/login/">LOGIN</a>
<! -- give link to your login page -->
<?php }
put this code in header file.
You need to add an else to the if statement that checks if the user is logged in. If they aren't logged then you output a link to the login page. You may need to tweak the CSS or the classes on the menu item for the login link.
<?php }
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
</ul>
</li>
<?php } else { ?>
<li class="menu-item current_us">
Login
</li>
<?php } ?>
<?php //submit menu
if(ot_get_option('user_submit',1)) {
$text_bt_submit = ot_get_option('text_bt_submit');
if($text_bt_submit==''){ $text_bt_submit = 'Submit Video';}
if(ot_get_option('only_user_submit',1)){
if(is_user_logged_in()){?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php }
} else{
?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php
}
} ?>

Unexpected behaviour of If-Else

I tried to show a login/logout link in my header based on the value in session. i tried some thing like this
<ul class="nav navbar-nav navbar-right text-uppercase">
<li>Contact</li>
<li>FAQ</li>
<?php
$session = Yii::$app->session;
$user_id = $session->get('userid');//print_r($user_id);die();
if($user_id != null)
{?>
<li>Logout</li>
<?php}
else
{?>
<li>Login</li>
<?php } ?>
</ul>
then both links didn't appeare in the header(login/logout). then after a lot of trying i came up with this code
<ul class="nav navbar-nav navbar-right text-uppercase">
<li>Contact</li>
<li>FAQ</li>
<?php
$session = Yii::$app->session;
$user_id = $session->get('userid');//print_r($user_id);die();
if($user_id != null)
{
?>
<li>Logout</li>
<?php
}
else
{
?>
<li>Login</li>
<?php
}
?>
</ul>
the code is actually same but i have added some spaces between the curly brackets'{'. And it works as i intended. Is space an issue when we use html and yii2 code combined?
This has nothing to do with Yii, it is simply a php syntax problem (you should always have a space after <?php)...
If you want to mix condition and html output, and have a better readability, you should use this :
<?php if ($user_id != null) : ?>
Output 1
<?php else : ?>
Output 2
<?php endif; ?>
Read more : http://php.net/manual/en/control-structures.alternative-syntax.php
There HAS to be a space after the opening <?php tag, which means <?php} is not valid:
without space:
$ cat z.php
<?php if(true) {?>
true
<?php} else {?> // note, no space after <?php
false
<?php }?>
$ php z.php
true
<?php} else {?>
false
with space:
$ cat y.php
<?php if(true) {?>
true
<?php } else {?>
false
<?php }?>
$ php y.php
true
Note the difference in output. This has nothign to do with Yii, and everything to do with your core PHP coding.

How to add a new custom link using PHP "if" and "else", in OpenCart 1.5.5.1?

I'm editing a theme named AquaCart, installed in OpenCart 1.5.5.1. I also integrated Twitter Bootstrap's fixed top navbar. I already modified the navbar and put the Login / Logout menu there, and it's doing fine. This project is for my own online store.
I don't have any PHP skills yet, only HTML+CSS. I have managed to put the login/logout button on my new navbar menu by copying codes from my current theme's header.tpl file and editing the header.php file (from the catalog\english\common\header.php).
Now, I am polishing the menu and wanted to add some custom menu/link named Sign Up!.
I want this Sign Up! menu link to show as Logout link when a user is already logged in.
My current edit shows the logged-in user's name in a <li>, and a logout menu in a second <li>. This is not what I really want. I want to show the Logout link in place of the Sign Up! link when a user is logged in.
Here is my current header.tpl edit:
<ul>
<ul>
<li>
My Account<!--Shall be shown only when user is logged in-->
<ul>
<?php if (!$logged) { ?>
<?php echo $text_welcome; ?>
<?php } else { ?>
<?php echo $text_logged; ?>
<?php } ?>
<li class="divider"></li>
<li>Store Front</li>
<li>Blog Page</li>
</ul>
</li>
<li>Sign Up!</li>
</ul>
</ul>
My header.php from the catalog\language folder of opencart. text_logged and text_welcome was already edited.
<?php
// header.php from catalog\language\english\common\header.php
$_['text_home'] = 'Online Shop';
$_['text_wishlist'] = 'Wish List (%s)';
$_['text_shopping_cart'] = 'Shopping Cart';
$_['text_search'] = 'Search';
$_['text_welcome'] = '<li>Login</li>
<li>Sign Up!</li>';
$_['text_logged'] = '<li>%s</li>
<li>Logout</li>';
$_['text_account'] = 'My Account';
$_['text_checkout'] = 'Checkout';
?>
The above code is rendered like so...
..and I wanted to make the sign Up! link to become Logout when a user is logged in.
I don't know PHP yet, but I'm struggling on studying the PHP files of OpenCart installation, and I've found this string from my header.php file found in catalog\controller\common:
$this->data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->url->link('account/logout', '', 'SSL'));
I am thinking of duplicating that, but don't know what to duplicate and modify. And what are other files involved?
UPDATE:
I've now added a new Text name "Sign Up" and "Login", so I may not be confused, and I can easily substitute it to my Text Link.
So far.. I've added: In my catalog\english\common\header.php:
$_['text_login'] = 'Login';
$_['text_signup'] = 'Sign Up!';
And in my catalog\controller\header.php.
$this->data['text_login'] = $this->language->get('text_login');
$this->data['text_signup'] = $this->language->get('text_signup');
UPDATE 2:
I've re-marked up my Menu, based from shadyxx opinion, I've changed the menu a bit. So for "not Logged-in Users"... This menu should be echoed...
Marked up like this:
<div class="nav-collapse collapse">
<ul>
<li>Login</li>
<li>Register</li>
</ul>
</div>
And for LOGGED-IN USERS, this menu should be echoed...
Marked up like this:
<div>
<ul>
<ul>
<li>
Account></b>
<ul>
<li>%s</a></li>
<li class="divider"></li>
<li>Shopping Cart</a></li>
<li>Checkout</a></li>
</ul>
</li>
<li>Logout</a></li>
</ul>
</ul>
</div>
In my catalog\controller\common\header.php:
$this->data['signup'] = sprintf($this->language->get('text_signup'), $this->url->link('account/register', '', 'SSL'));
$this->data['login_register'] = sprintf($this->language->get('text_login_register'), $this->url->link('account/login', '', 'SSL'), $this->url->link('account/register', '', 'SSL'));
$this->data['logged_in'] = sprintf($this->language->get('text_logged_in'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->data['text_shopping_cart'] = $this->language->get('text_shopping_cart'), $this->data['shopping_cart'] = $this->url->link('checkout/cart'), $this->url->link('account/logout', '', 'SSL'));
In my catalog\language\english\common\header.php
$_['text_login_register']
= '<div>
<ul>
<li>Login</a></li>
<li>Register</a></li>
</ul>
</div>';
$_['text_logged_in']
= '<div>
<ul>
<ul>
<li>
Account<b class="caret"></b>
<ul>
<li>%s</li>
<li class="divider"></li>
<li>Shopping Cart</li>
<li>Checkout</li>
</ul>
</li>
<li>Logout</li>
</ul>
</ul>
</div>';
In my header.tpl
<?php if (!$logged) { ?>
<?php echo $login_register; ?>
<?php } else { ?>
<?php echo $logged_in; ?>
<?php } ?>
So, what's not working, using the above codes is the checkout menu. The checkout menu is redirecting a user to the Shopping Cart Page.
I've just copy pasted the above codes. I've tried to understand the use of the existing code and by trial and error.
IMHO, you do not want to have My Account link when there is no user logged in...as it would make no sense to be shown for the visitors (not-logged-in users).
I would show only the Sign Up! link, and after logging in, I would show only the My Account menu that would have the desired Logout link (so as it is right now).
So keep your current template as is, and using this code block (you have given us):
<?php if (!$logged) { ?>
<?php echo $text_welcome; ?>
<?php } else { ?>
<?php echo $text_logged; ?>
<?php } ?>
We can modify your top navbar slightly... The condition <?php if (!$logged) { ?> applies if there is no user logged in, so inside it we will show the Sign Up! link, then the <?php } else { ?> gets applied if there is a user logged in. Thus we will move the My Account menu there. This code is just an example (as you didn't provide us with the navbar source code...):
<ul id="top-navbar>
<?php if (!$logged) { /* only visitor */?>
<li><?php echo $text_signup;?></li>
<?php } else { /* logged in user */?>
<li><span>My Account</span>
<ul class="sub-menu">
<li><span><?php echo $text_logged; ?> <?php echo $logged_username; ?></span></li>
<li><a href="<?php echo $logout; ?>><?php echo $text_logout; ?></a></li>
</ul>
</li>
<?php } ?>
</ul>
In your controller you would have to push the language strings, URLs and username to the template and in language file you would have to add the text strings needed for your menu (if you hadn't done so far...).
EDIT:
Your thinking is good, but the approach not. Do not put HTML into language files, nor controllers, unless really necessary or no other way is possible (which may not occure and if yes, something is wrong).
So, put all the HTML into your template file (header.tpl) and use PHP variables:
<?php if (!$logged) { ?>
<div>
<ul>
<li><?php echo $text_login; ?></li>
<li><?php echo $text_signup; ?></li>
</ul>
</div>
<?php } else { ?>
<div>
<ul>
<li>
<?php echo $text_account; ?><b class="caret"></b>
<ul>
<li><?php echo $username;?></li>
<li class="divider"></li>
<li><?php echo $text_shopping_cart; ?></li>
<li><?php echo $text_checkout; ?></li>
</ul>
</li>
<li><?php echo $text_logout; ?></li>
</ul>
</div>
<?php } ?>
Now many of the URLs and texts are already defined thus we need to add only few of them.
Open your language file (catalog\language\english\common\header.php) and add/modify these:
$_['text_login'] = 'Log In';
$_['text_signup'] = 'Register';
$_['text_logout'] = 'Log Out';
You can remove the previous text_login_register and text_logged_in variables with its values.
Now open the controller (catalog\controller\common\header.php) and find the part that ends with this line:
$this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL');
And after it, add these lines:
$this->data['text_login'] = $this->language->get('text_login');
$this->data['text_signup'] = $this->language->get('text_signup');
$this->data['text_logout'] = $this->language->get('text_logout');
$this->data['login'] = $this->url->link('account/login', '', 'SSL');
$this->data['signup'] = $this->url->link('account/register', '', 'SSL');
$this->data['logout'] = $this->url->link('account/logout', '', 'SSL');
$this->data['username'] = '';
if($this->customer->isLogged()) {
$this->data['username'] = $this->customer->getFirstName() . ' ' . $this->customer->getLastName();
}
Now you should be done.

Categories