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.
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 would like to detect a certain category with ID=4 and then apply if/else to add div to display contents.
<?php
if (JRequest::getVar('view')=='categories' && JRequest::getVar('id')==4) { ?>
<?php } ?>
How do I achieve this?
I want to show this if the article belongs to category 4 else show another div.
Imagining this two be the different div
<div class="category4">text here</div>
<div class="categoryxxx">text here</div>
Do note that <?php echo $this->item->catid;?> shows the correct Category ID. I would like to apply if and else statement using catid==9 or something. Just not good at PHP.
You can also put directly the html code, avoiding echo. It may useful especially when html code is sizeable.
<?php if ($this->item->catid == 4): ?>
<div class="category4">text here</div>
<!--- any other html code --->
<?php else: ?>
<div class="categoryxxx">text here</div>
<!--- any other html code --->
<?php endif; ?>
Hey I got it working :)
<?php
if ($this->item->catid == 9) {
echo "yes, it exists";
}
else {
echo "no, it don't exist";
}
?>
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 am new to PHP and cannot figure out why this is not working properly. What I want to happen is if a session is found execute the web page otherwise redirect to login.php. What is happening is the webpage is being executed and the user sees sql errors then the redirect happens. It's as if both the if and the else are being executed. Please help.
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>
a nicer way:
<?php
session_start();
if (!isset($_SESSION['userID'])){
header("Location: /login.php");
}
$mainUID=intval($_SESSION['userID']);
?>
<html>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
check your braces and do session_start before using the session
If you are using if else statements within a webpage you may find this format easier:
<?php if(true): ?>
HTML Here
<?php else: ?>
HTML Here
<?php endif; ?>
But the reason why your code isn't working is because you have got your curly braces mixed up.
Examine this example and compare it with your code:
if(true) {
// Do stuff
}
else {
// Do stuff
}
Try this code your first curtly bracket was closing instead of opening
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>