PHP IF to display HTML based on session isset - php

I have the below code, which gives no errors, but it only ever displays the Logout button, I don't really have much else to say.
<?php
if(isset($_SESSION)):?>
<div class= "buttons feature">Logout</div>
<?php
else:?>
<div class= "buttons feature">LOGIN</div>
<?php endif;
?>
The Logout button runs:
<?php
session_start();
session_destroy();
?>
And have I have code in my main page which displays values stored in the session based on if the session isset, so I know it is destroying the session, and when I'm logged in it is set.
I just don't understand why it always only displays the html from the if where the session doesn't exist.
I've probably made a silly mistake, so sorry if I have.
All help appreciated -Tom

try this
if(isset($_SESSION) AND $_SESSION):?>
<div class= "buttons feature">Logout</div>
<?php
else:?>
<div class= "buttons feature">LOGIN</div>
<?php endif; ?>

Setting $_SESSION
session_start();
$_SESSION['logged_in'] = true;
checking $_SESSION:
session_start();
if(isset($_SESSION['logged_in'])){
echo "logged in";
}

The $_SESSION variable is always set - regardless if you have initialized the session or not. You must actually assing something in $_SESSION array to get results. For example after succesful login do this:
$_SESSION['is_logged_in'] = true;
And then you can check
if(isset($_SESSION['is_logged_in']))
anywhere you want (instead of your if(isset($_SESSION))).

Related

How to Hide/Show Div when session is started with PHP

I have a problem with PHP. When user is logged in, or session is started,
I want to hide div class, which is button "Login".
How can I make this with PHP?
Instead of trying to hide it, never output the div if the session has login information.
I.e. in your template / code that's outputting the HTML, check if the session is present (you'll have to swap user with whatever key you're storing the valid login under):
<?php
if (empty($_SESSION['user'])):
?>
<div class="login"> ... </div>
<?php
endif;
?>
.. and if you need it in a function:
<?php
function show_login_if_unknown() {
if (empty($_SESSION['user'])) {
echo '<div class="login"> ... </div>'; // or use ?> ... <?php
}
}

php log in with session display

Hi I am trying to make a website prototype that will show log in username , i have successfully displayed the user session, but when there is no session it will only display error.
<h1>Dear Precious Customer</h1>
<div>
<h4> Welcome <?php echo $_SESSION['username']; ?> </h4>
</div>
<div>
Logout
</div>
Here is my code for the display, as you can see it only show when session is active , if there is no session it will only show error . Instead of that error, how do I make it display something like "welcome log in or register" thanks.
Change this
<div><h4>Welcome <?php echo $_SESSION['username']; ?></h4></div>
To
<?php
$msg = 'Welcome! Login | Register';
if ($_SESSION && isset($_SESSION['username'])) {
$msg = 'Welcome! ' .$_SESSION['username'];
}
?>
<div><h4><?php echo $msg;?></h4></div>
You would need to test for the session variable like,
<div><h4>Welcome <?php echo (isset($_SESSION['username']) ? $_SESSION['username'] : 'Register'); ?></h4></div>
That way if it's not set, you simply output the other desired string.

using session variables on home page to show account links

I need some links (related to user account) to appear on the index page for the user who logged in. i have a session variable'email'.
i did this but it didn't work.
<div id="left">
left div content
</div>
<div id=-"right">
<?php
if(isset($_SESSION['email']))
{
?>
//show user some links to his account.
<?php
}
else
{
?>
//show login and register forms
<?php
}
?>
</div>
<?php
session_start(); // add this line
if(isset($_SESSION['email']))
{
?>
Link to php manual.
your first statement within the
<?php
session_start();
//followed by rest of the code.
?>
should be
session_start();
Then the further code.

How to pass session variables when header redirects? loosing session variables during the process of redirection

session varibales
<?php session_start();
error_reporting(-1);
if (!isset($_SESSION)){
}
$total_amt=$_POST['total_amt'];
$total_seats=$_POST['total_seats'];
$seat=$_POST['seat'];
$boarding_point=$_POST['boarding_point'];
$submit=$_POST['chksbmt'];
$_SESSION['total_amt']=$total_amt;
$_SESSION['total_seats']=$total_seats;
$_SESSION['seat']=$seat;
$_SESSION['boarding_point']=$boarding_point;
$_SESSION['chksbmt']=$submit;
?>
<?php
$serial_no = 1;
$total_seats = $_SESSION['total_seats'];
$seats=explode(',', rtrim($total_seats, ","));
foreach ($seats as $seat) { ?>
<tr>
<td style="text-align:center;"> <?php echo $serial_no++; ?></td>
<td style="text-align:center;"><?php echo $seat; ?> </td>
& so on ..
login div
<ul class="top">
<li>Registration
</li>
<?php if(!isset($_SESSION['login_user'])) { ?>
<li class="hover">Login
</li>
<?php } else {?>
<li class="hover">Logout</li>
<div style="color:#00CC66;">Welcome <?php echo $_SESSION['login_user']; ?></div>
<?php } ?>
</ul>
logout.php
<?php session_start();
session_destroy();
header('Location:'.$_SERVER['HTTP_REFERER']);
exit;
?>
When m logging out from my page, it has to be redirected & in this process i m loosing my session variables. Is that any process to keep the variables constant after the redirection .. After the page redirection m getting undefined index error.. Any help .. ?? Thank you in advance
You're destroying whole session on logout page. While you just need to unset the relevant session key.
Something like,
session_start();
unset($_SESSION['login_user']); //session_destroy();
header('Location:'.$_SERVER['HTTP_REFERER']);
exit;
instead of using session_destroy(); you should use unset($_SESSION['login_user']);It will change the value of the variable in the session to null and you will not lose your variable. session_destroy() destroys the session completely and then you have no way to access that variable.
You can check the difference between them using var_dump eg:- session_destroy(); var_dump($_SESSION['login_user']); and unset($_SESSION['login_user']); var_dump($_SESSION['login_user']);
AND in your code
if (!isset($_SESSION)){
}
you should not keep it blank. I think you forgot to include the following code inside this block

Creating a simple "Logged In As" line on my page

<?php
session_start();
if(isset($_SESSION['login']))
{
include_once('includes/header.php'); ?>
<!DOCTYPE html>
<html>
<body>
<div id="mainframe">
<img src="img/header.png">
<div id="menu">
<?php include_once('includes/navbar.php'); ?>
</div>
<div id="content">
<h3>Shopping Cart</h3>
</div>
</div>
<?php include_once('includes/footer.php'); ?>
</body>
</html>
<?php }
else
{
header('location: login.php');
}
?>
Here is my small PhP code I've got at the moment, my login session is $_SESSION['login'].
And I'd like to display : Logged in As on my page when they are logged in, I've tried several things but it didn't work out.
Does anyone know a simple method / solution for this?
Put this somewhere in your if statement.
It will show Logged in as User at right top corner of page
<div style="position:absolute; right:0px; top:0px;">
<?php echo "Logged In as". $_SESSION['login']; ?>
</div>
U need to pass username using SESSION variable for the same
write a simple sql query to get the username from any variable you are taking from user to make sure that the particular user is the correct user.i am taking password.
$query = "SELECT name FROM users WHERE password='$password'";
$username = mysql_result(mysql_query($query),0);
$_SESSION['username'] = $username;
than proceed as you are doing
<?php
session_start();
if(isset($_SESSION['login']) && isset($_SESSION['username']))
{
echo "logged in as".$_SESSION['username'];
}

Categories