How to Hide/Show Div when session is started with PHP - 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
}
}

Related

Conditionally showing HTML without wrapping it in a PHP echo statement

I have a block of code to generate a user's profile that looks like this:
<?php
if($user_isbanned) {
echo 'User is banned.';
exit(1);
}
?>
<!-- Content to show if the user is not banned -->
This works for what I want it to do (stop the profile from appearing if the user's account is banned), but if I were to add in a footer at the bottom of the page, it would not appear on a banned user's page since the exit() statement aborted generating the rest of the page. I could reformat my code to look like this:
<?php
if($user_isbanned) {
echo 'User is banned.';
} else {
// Content to show if the user is not banned
}
?>
This would make my footer appear regardless of whether or not the user is banned, but I don't want to wrap the entire page in one large PHP echo statement. Is there anyway to prevent a certain block of HTML from appearing if a PHP condition is or is not satisfied, without putting the entire page in an echo statement?
What you could do is something like that:
<html>
<head>
</head>
<body>
<div id="header"></div>
<?php if($user_isbanned){ ?>
<div id="content-banned"></div>
<?php } else { ?>
<div id="content-not-banned"></div>
<?php } ?>
<div id="footer"></div>
</body>
Just render different kind of content, when user is banned.
In your first example, a footer will never be displayed. In fact, not even a closing </html> tag, that doesn't seem very desirable.
You don't have to literally echo the HTML, you could just:
<?php
if($user_isbanned) {
echo 'User is banned.';
} else {
?>
<!-- html goes here -->
<?php
}
?>
Alternatively, you could just redirect a banned user to a separate page entirely.
My preferred solution would be an HTTP redirection to an informative page:
<?php
if($user_isbanned) {
header('Location: http://example.com/banned');
exit(1);
}
<!DOCTYPE html>
If you prefer to keep your current design:
<?php
if($user_isbanned) {
echo 'User is banned.';
} else {
?>
<p>Content to show if the user is not banned</p>
<?php
}
N.B. Closing ?> tag is optional.

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.

PHP IF to display HTML based on session isset

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))).

how to change my web site's elements style dynamicly

In index.php file I first check if $_SESSION['user'] is set it would not show the login form and if not, the login form should be shown.
Here is my login form style
<style>
#login_form{visibility:hidden;}
</style>
And my PHP script is:
<?php
if(isset($_SESSION['user']))
dont show the login form;
else
show the login form;
?>
and the question is: how can I change the #login_form style to visible?
Why not adding an inline style (perhaps a class would be even better) when the user is logged in?
<form style="<?php if(isset($_SESSION['user'])) { echo 'visibility: hidden;';}?>">
<!-- Rest of the form -->
</form>
Or even better, print the form only when is logged in (so visitors can't force the browser to display the form):
<?php if(isset($_SESSION['user'])) { ?>
<form>
<!-- Rest of the form -->
</form>
<?php } ?>
Create a CSS class:
.invisible {
visibility: hidden;
}
Then apply it when your user is already logged in:
<form class="<?=isset($_SESSION['user']) ? 'invisible' : ''?>">
You can have it directly in the div that holds the form, like this:
<div style="<?if(isset($_SESSION['user'])){echo 'display:block'}else{echo 'display:none'}?>">
...Form code
</div>

Hide and show a div depending on session

I have an admin link with a div id "admin". Sessions are started when a user is logged in to show if it is a normal user or an admin. Normal users can't access the files for admin, but can still see the admin link.
Is there a way to make it so normal users can't see the link, using only php or html, without jquery or jscript or any of those.
Using interleaved PHP & HTML with standard PHP syntax:
<?php
if ($user_is_an_admin) {
?>
<div id='admin'>
Only admins can see this...
</div>
<?php
}
?>
Alternate templating syntax:
<?php if ($user_is_an_admin): ?>
<div id='admin'>
Only admins can see this...
</div>
<?php endif; ?>
Not interleaving, PHP only:
if ($user_is_an_admin) {
echo "<div id='admin'>
Only admins can see this...
</div>
";
}
You'll need to use conditionals inside of your views:
<?php if($_SESSION['adminid'] == 1234): ?>
<!-- Admin div goes here -->
<?php else: ?>
<!-- Admin link goes here -->
<?php endif; ?>

Categories