Multiple PHP If Statements inside HTML - Unexpected endif - php

Im trying to implement a second if statement inside a first statement, all of that inside HTML.
Should work like this:
User logs in
Login Script checks if information is missing (New User)
If so, user needs to add it
If not, just display "welcome back" (Known User)
<?php if ($_SESSION['ID']): $log = 'Logout'; ?> <!-- If we have a session, login button becomes logout button-->
<h1>You are logged in <?php echo $_SESSION['Name']; ?>!</h1>
<?php if($_SESSION['locationcheck'] === False)?> <!-- If login script says location is missing, form comes up to enter location-->
<h2>Please complete by entering your location:</h2>
<!-- FORM TO ENTER LOCATION COMES UP-->
<?php endif; ?> <!-- This endif is unexpected-->
<?php else: $log = 'Login'; ?> <!-- We dont have a session, display login button-->
<h1>You are logged out!</h1>
<div><?php echo '<a href='.$helper->getLoginUrl().'>Login with fb</a>';?></div>
<?php endif ?>
The error I get is that the first endif is unexpected.

Related

Sign In or Sign Out menu options not working in PHP script

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?

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
}
}

How to restrict the view for particular user when they are login in using cookie name using php

This is my php code
<?php
require_once('../includes/config.php');
// include file to check, If current user loggedin or not
include('log-security.php');
require_once('../cs/includes/header.php');
if(in_array(cms_username, $allowedUsers))
{
$allowedUsers=['john', 'akhil'];
<div id="view8"></div>
}
?>
This is my div id code
<div id="view8" class="tabcontents">
<h3>Creation of files</h3>
</div>
Im new to PHP, Please help me to correct the code if its wrong .
while login only it should restrict the user.when allowed user is entered then the viewid should displayed for others it should not.
Try something like that :
<?php
require_once('../includes/config.php');
require_once('../cs/includes/header.php');
include('log-security.php');
$allowedUsers = array('john', 'akhil');
?>
<?php if (in_array('cms_username', $allowedUsers)): ?>
<div id="view8" class="tabcontents">
<h3>Creation of files</h3>
</div>
<?php endif; ?>

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.

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