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.
Related
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'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.
I have a reporting website that I created and I'm slowly adding functionality to it. I've just added a part where it is supposed to force a user to log in. It's really just to capture the users CorpID, I don't keep or record the password and it's not required.
Right now I have the login portion working. Then I'm trying to run a check to make sure that a user is logged in and if not to force them to log in. I am only doing this right now on the Admin page, which only I have access to. Here is how I've got it right now:
AdminPage.php:
<body>
<?php
require 'CheckLogin.php';
include 'Menu.php';
?>
Other code for the page
CheckLogin.php:
<?php
$Expiration = time() - (60*60*24*7);
echo "You made it to here!";
if(!isset($_COOKIE['UserName']))
{
if(isset($_POST['UserName']))
{
setcookie("UserName",$_POST['UserName'],$Expiration);
}
else
{
echo "<script>location.href='LoginForm.php'</script>";
}
}
else
{
if(isset($_POST['UserName']))
{
setcookie("UserName",$_POST['UserName'],$Expiration);
}
else
{
setcookie("UserName",$_COOKIE['UserName'],$Expiration);
}
}
?>
UPDATE
Here's the Menu.php
<?php
$AdminUsers = include 'AdminUsernames.php';
if(isset($_COOKIE['UserName']) && in_array($_COOKIE['UserName'],$AdminUsers,TRUE))
{
$user = 'Admin';
}
else
{
$user = 'User';
}
//echo "<BR>"; print_r($_COOKIE['UserName']);
//echo "<BR>"; print_r($AdminUsers);
?>
<div class="menu-wrap">
<nav class="menu">
<ul class="clearfix" id="menu">
<li>Home</li>
<li>Report Builder</li>
<li>OPCEN Reports
<ul class="sub-menu">
<li>New COEI OPR Report</li>
<li>New OSP OPR Report</li>
<li>EOJ Report</li>
<li>Material Tracking</li>
<li>Vendor Material Tracking</li>
<li>CAF2 Tracker</li>
<li>JIM Report</li>
</ul>
</li>
<li>CAFII Reports
<ul class="sub-menu">
<li class="minHeight">Material Received Job Not Started</li>
<li class="minHeight">CAF2 Tracker New Test</li>
<?php
include 'DBConn.php';
$data = $conn->prepare('SELECT Id, QName, SSRSName from pmdb.QDefs where QSrc = 2 AND IsActive = 1 order by QName');
$data->execute();
$result = $data->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $q)
{
echo '<li class="minHeight">' . $q['QName'] . '</li>';
}
?>
</ul>
</li>
<li>Invoicing/Closing
<ul class="sub-menu">
<li>Non-Varasset Invoices</li>
</ul>
</li>
<li>ENG Reports
<ul class="sub-menu">
<li>Approved Projects</li>
<li>Approved Projects Previous Day</li>
<li>M6Action</li>
</ul>
</li>
<?php
if($user == 'Admin')
{
include 'AdminMenu.php';
}
?>
</ul>
</nav>
</div>
It's really just a standard menu and has been working fine till I added the require for the CheckLogin.php page.
All I get is a blank page when I have this require in the AdminPage.php. I don't get the echo I don't get the menu or anything.
What am I doing wrong? This isn't the first time that I've used the require, but it is the first time that it results in a blank page.
I do know that I have the expiration set to last week, I'm trying to force a re-login.
Put the following in your script before any includes or requires.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Using Doug's suggestion from above I found the error. In using the require I did not use the full path for the file and so it could not be opened. What that line should look like is:
require 'Helper/LoginCheck.php';
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();
I have a LAMP(Ubuntu) Server running on a physical machine. My goal is to host a website on the server. Right now, I have all my files in my /var/www/html directory and inside certain HTML files, I run PHP scripts to check if the user is logged in or not such as below:
<div class="collapse navbar-collapse" id = "navCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active link-nav">Home</li>
<li class="link-nav">Categories</li>
<li class="link-nav">About</li>
<!-- This part -->
<?php
if($_SESSION['loggedin'] != 1){
echo '<li class="link-nav">
Log In
</li>
<li class="link-nav">
Register
</li>';
}else{
echo "<li class = 'link-nav'> Log Out ";
}
?>
<!-- End of PHP -->
</ul>
</div>
When I open up to my server in the browser, all I get is this:
The $_SESSION['loggedin'] variable has already been set but both the Login and Logout buttons show. Also, the text in between the two sets of strings is showing on the page.
When I display this on my local computer using XAMPP, this doesn't happen.
Any ideas on how to fix this?
Thanks in advance!
This line is invalid. With proper error reporting on, you would see the error. When testing, it will help a lot to have error reporting turned on so you can see the helpful errors :)
echo "<li class = 'link-nav'> Log Out ";
You can not use the double quotes inside themselves without escaping them!
Instead write it like this:
echo '<li class="link-nav">Log Out';
See if that works.
Try this, It's much easier to work with code that has islands rather than everything being a PHP string(Also, take a look at http://www.Laravel.com and http://www.Laracasts.com):
<?php if($_SESSION['loggedin'] != 1){ ?>
<li class="link-nav">
Log In
</li>
<li class="link-nav">
Register
</li>
<?php }else{ ?>
<li class="link-nav"> Log Out
<?php } ?>