Reputation: 141
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?
Upvotes: 2
Views: 2036
Reputation: 146460
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.
Upvotes: 1
Reputation: 6252
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.
Upvotes: 1
Reputation: 1429
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.
Upvotes: 5