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>
Related
All the page scripts (i.e. .php files) in my application use the "include" statement to include a menu (menu.php) on the page as follows:
<div id="menublock" class="menu">
<ul class="clearfix">
<li>Home</li>
<li>About Us</l1>
<li>Membership</l1>
<li>Gallery</li>
<?php
if (isset($_SESSION['valid_user'])) {
echo '<li>Sign Out</li>';
} else {
echo '<li>Sign In</li>';
}
?>
</ul>
</div>
<!-- end of menublock -->
The session variable "valid_user" indicates whether the user is logged in or not, and the final option on the menu should reflect this by showing either "Sign In" or "Sign Out".
The login.php and logout.php scripts, after performing their functions, will call themselves and display a message indicating that the user is now
logged in or out. However, the menu shows the wrong option. For example, when you sign out the menu will still show the "Sign Out" option. If you then click on one of the other menu options the relevant page is shown with the correct "sign In" option.
Here is logout.php in response to Levi's request. Inserting it has made me realize what the problem is. The php code in menu.php is executed before the logout request is processed, so the valid_user session variable still indicates that the user is logged in. A similar thing no doubt happens in login.php. Silly mistake, I know!
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include( 'assnid.html' );
?>
</head>
<body>
<?php
include( 'menu.php' );
include( 'functions.php');
$db = dbconnect();
$temp = logAction( $db, 'Sign Out' );
$db->close();
?>
<div id="bodytext">
<!-- Main content -->
<?php
// store to test if they *were* logged in
$old_user = $_SESSION['valid_user'];
unset($_SESSION['valid_user']);
unset($_SESSION['gen_pw']);
session_destroy();
?>
<html>
<body>
<!-- <h2>Sign out</h2> -->
<?php
if (!empty($old_user))
{
echo '<h2>You have been signed out.</h2><br />';
}
else
{
// if they weren't signed in but came to this page somehow
echo '<h2>You were not signed in, and so have not been signed out.</h2><br />';
}
?>
<?php
include 'footer.html';
?>
</div>
</body>
</html>
This is sending me crazy in trying to resolve it, as many websites have similar logic, and I don't understand why this should not work.
Is this something to do with compile opcodes being cached and then retrieved, or is it something else?
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
}
}
I am using osclass for a classifieds website. The theme has some widgets of which one is a search form. It is called into a header which exists on all pages. However, I would like to hide the search form only on this one page.
The form looks like this:
<form class="nocsrf navbar-left form-vertical" action="<?php echo osc_base_url(true); ?>" >
<div class="dropdown navbar-form js-menu-search-container">
…
…
</form>
How can I condition the form to show on all pages except on this one only: item-post.php?
I would greatly appreciate some help here.
You could have a style tag just on that page that hides the search form.
Add the following to the page you want to hide the form on:
<style>
.dropdown.navbar-form.js-menu-search-container {
display: none !important;
}
<style>
<style>
.form-hide{
display:none !important;
}
</style>
<?php
function need_hide_condition(){
$pos = strpos( $_SERVER["REQUEST_URI"], "YourUrl"); //likes /test.php
if( $pos!=false ) return true;
else return false;
}
?>
<form class="nocsrf navbar-left form-vertical <?php if( need_hide_condition() ) echo 'form-hide';?>">
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.
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; ?>