I'm struggling to get this PHP conditional statement to work, I have ensured that the session variable ID is not set by using session_destroy(), however no matter whether the ID variable is set or not the dropdown menu is displayed rather than the login button:
<nav>
<ul>
<li>Home</li>
<li>Catalogue</li>
<?php if(isset($_SESSION['ID']) && $_SESSION['ID'] != ""): ?>
<div class="dropdown">
<button class="dropbtn">Profile</button>
<div class="dropdown-content">
Overview
Edit Details
Logout
</div>
</div>
<?php else: ?>
<li>Login</li>
<?php endif; ?>
<li>Contact Us</li>
</ul>
</nav>
My desired outcome is that when the session variable ID is set, the dropdown menu is visible, otherwise the login button is visible.
Your code should work.
session_destroy() doesn't destroy the session cookie and the globals related to the current session.
Try again using session_unset() to clear all the variables related.
Related
I want to add extra navigation button for admin and customer differently.If admin is logged in an extra naviagtion button should be added and same for customer. I don't get any errors but the if else block (user_type) doesn't execute and I don't get any extra navigation button. What is wrong in my code.
index.php
<div class="navbar-collapse collapse">
<div class="menu">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation"><a href="index.php" class="active" >Home</a></li>
<li role="presentation"><a href="subscribe.php" >Subscribe</a></li>
<li role="presentation">Sign up</li>
<?php
if(isset($_SESSION['login_user'])) //check if user is logged in
{
if(!empty($_POST['user_type']))
{
$user_type=$_POST['user_type'];
if($user_type==1) //type 1 is admin
{
echo'<li role="presentation">Customers</li>';
echo'<li role="presentation">Logout</li>';
}
elseif($user_type==2) { //type 2 is customer
echo'<li role="presentation">My account</li>';
echo'<li role="presentation">Logout</li>';
}
}
}
else{
echo'<li role="presentation">Login</li>';
}
?>
<li role="presentation">Contact</li>
</ul>
</div>
</div>
signup.php
<form action="psignup.php" method="POST" class="register-form" id="register-form">
<select required type="text" name="user_type" class="dropdown">
<option value="NULL">Select User Type</option>
<option value=1>Admin</option>
<option value=2>Customer</option>
</select>
</form>
I suppose that after log in, you validate user's credentials and you create the $_SESSION['login_user'].
Use the same logic to validate the user and according to his'/her's user type, create the $_SESSION['user_type'] right after your $_SESSION['login_user'] setup, within a conditional statement.
That way, you can check for the if($_SESSION['user_type'] == 1) in every page of your site.
Remember to clear or destroy your $_SESSION variables during logout and filtering your posted values before assigning them into your session (i.e. htmlspecialchars(strip_tags($_POST['user_type'], ENT_QUOTES)) see here.
$_POST['user_type'] will only exist once, after the first time the user completes your signup form. That's just how POST variables work - they are populated with data the user has submitted in the current request; they don't persist during subsequent requests.
So if you want to display the menu for every page after the user logs in, then you can't use it. You would need to store the user type value in the Session, and fetch it from there (just like you are doing with the login_user value already.)
Please, I'm new to PHP and I'm building a subscription-based ecommerce site. I've been able to customize the pages such that they look different based on who's browsing what, but I'd like to take the Renew Account off the admin page since they don't have to subscribe.
I've been struggling all day. Please, could someone show me how it could be achieved?
Below is the snippet:
<?php // Show the user info or the login form:
if (isset($_SESSION['user_id'])) {
// Show basic user options:
echo '<div class="title">
<h4>Manage Your Account</h4>
</div>
<ul>
<li>Renew Account</li>
<li>Change Password</li>
<li>Favorites</li>
<li>History</li>
<li>Recommendations</li>
<li>Logout</li>
</ul>
';
// Show admin options, if appropriate:
if (isset($_SESSION['user_admin'])) {
echo '<div class="title">
<h4>Administration</h4>
</div>
<ul>
<li>Add Page</li>
<li>Add PDF</li>
<li>Blah</li>
</ul>
';
}
} else { // Show the login form:
require ('login_form.inc.php');
}
?>
What you could do is to first check if the user admin session is set, to which I added the same conditional value for in the first conditional statement.
If it is set, then assign an empty value for what I named as $renew, with an else{} with the value that I removed from your existing <li></li>.
The first two session arrays here are only representational values of course.
I concatenated the '.$renew.' variable in the menu.
Note: Make sure that the session was started using session_start() inside all pages using sessions; that is not known.
$_SESSION['user_id'] = 123;
$_SESSION['user_admin'] = "john";
if (isset($_SESSION['user_admin'])) {
$renew = '';
} else {
$renew = '<li>Renew Account</li>';
}
if (isset($_SESSION['user_id'])) {
// Show basic user options:
echo '<div class="title">
<h4>Manage Your Account</h4>
</div>
<ul>
'.$renew.'
<li>Change Password</li>
<li>Favorites</li>
<li>History</li>
<li>Recommendations</li>
<li>Logout</li>
</ul>
';
// Show admin options, if appropriate:
if (isset($_SESSION['user_admin'])) {
echo '<div class="title">
<h4>Administration</h4>
</div>
<ul>
<li>Add Page</li>
<li>Add PDF</li>
<li>Blah</li>
</ul>
';
}
}
Try..
If(!isset($_SESSION['user_admin'])) {echo '<li>Renew Account</li>'; }
This way if it the session variable for admin is set the echo won't occur.
I am trying to build authentication syatem in my website and i am able to successfully register and login but after login my menu bar is still showing me option for login.
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li>HOME</li>
<li>REGISTRATION</li>
<li>DOWNLOAD</li>
<li>CONTACT</li>
<?php
if(isset($_SESSION['user_id']) === true){
?><li>LOGOUT</li><?php
}else{
?><li>LOGIN</li><?php
}
?>
</ul>
</div>
</div>
what should i do?
this piece will fail every time
if(isset($_SESSION['user_id']) === true){
?><li>LOGOUT</li><?php
}else{
?><li>LOGIN</li><?php
?>
normally we check for session like this , check if it is set and check if it not empty and also modified your echo way
if(isset($_SESSION['user_id']) ) && (!empty($_SESSION['user_id']) ){
echo '<li>LOGOUT</li>';
}else{
echo '<li>LOGIN</li>';
}
?>
also very important at the top of each page use this code at line 1 and 2 otherwise no session functions will work
<?php
session_start();
My issue is: I have a login system and I wanna show the current username in the page.
calendar.php:
// SOME CODE
<ul class="nav navbar-nav">
<li>Welcome <? echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
// SOME CODE
This shows the "Welcome" but don't show username...
How can I do this? Thank you all in advance.
Make sure $_SESSION['username'] is already set, i.e:
index.php
<?php
session_start();
$_SESSION['username'] = "someusername";
calendar.php
<?php
session_start();
?>
<ul class="nav navbar-nav">
<li>Welcome <?php echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
Note:
Ensure session_start(); is the first statement on the script, i.e.:
<?php
session_start();
Otherwise you may get the error:
"Headers already sent..."
Learn how to use php sessions across multiple files here
Unless you are using a rather old system, you need
<li>Welcome <?php echo $_SESSION['username'] ?></li>
If it still doesn't show, ensure that the page is being parsed for PHP. Then, make sure that $_SESSION['username'] has a value.
You forgot to write php in your code:--
<ul class="nav navbar-nav">
<li>Welcome <?php echo $_SESSION['username'] ?></li>
<li>Logout</li> <!-- class="active" -->
<li>Store location <span class="label label-danger">65</span></li>
</ul>
Note:-
1.This code file name will be .php not .html and if your Session have username index and have some value then only it will show.
2.As you asked for the error you need to put this code on very upside of your php page
<?php
session_start();
$_SESSION['username'] = 'any user name';
After that my above code and then everything works fine.
3.If you want this session value to any other page on that page write first session_start(); on top and then you will get the value.
I'm using cakephp and I want the login view for the controller 'users' to be displayed on the default.ctp for the layout controller. I can I do this?
For example:
<div id="leftNav">
<div id="login-block" class="block">
<?php
//render users/login here
?>
<ul>
<li>Login</li>
<li>Register</li>
<li>Logout</li>
</ul>
</div>
</div>
I would make it an element - they are designed for re-usable code chunks that can be used anywhere.
To summarize the functionality, in your view you'd use:
<?php echo $this->element('login'); ?>
and put your "login-block" div login stuff in
/app/views/elements/login.ctp
If you don't use elements for that and you already have the login view you can use it like this
<?php echo $this->requestAction('/users/login'); ?>