php if/else not working as expected - php

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">';
}
?>

Related

Accessing php variables at different places in the same file

Consider the code
<?php
.....
.....
$error="abc";
......
?>
<html>
<head></head>
<body>
.....
<?php echo $error ?>
.....
</body>
</html>
I am new to php. I want to access the same "error" variable at two parts in the same file. Is there any way to do so? Or I have to create another file with the "error" variable and then include it in the file where I need it again?
You should be able to access the variable as many time as you need it if it's part of the same scope.
This will work:
<?php $foo = 'bar' ?>
<hr />
<?php echo $foo; ?>
This will not:
<?php
function set_foo_variable() {
$foo = 'bar';
}
set_foo_variable();
?>
<hr />
<?php echo $foo; ?>
Make sure your variable is always in the same scope AND is set.
Here's more documentation on PHP scope: http://php.net/manual/en/language.variables.scope.php
Have you already tried accessing it?
You shouldn't have an issue doing something like the following:
<?php $error="abc"; ?>
<html>
<head>
</head>
<body>
<?php echo $error; ?>
</body>
<?php echo $error; // access #2 ?>
</html>
<?php echo $error; // access #3 ?>
Note:
For the future, I would really try to improve the code format of your questions, mention what you tried to do already and provide more details about your issue.

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.

How to hide a <Div> using PHP

Hi I have used some code but I want it so when the name is NULL it is hidden, if this is possible please help.
Here is the code
<p id="top"><strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]<br/>Welcome To the homepage of The Journalist's</p>
I want it to be when name is NULL, for
"<strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]"
to be hidden.
Thank you.
update...
This is what i have so far
This is what i have so far.
<?php
if ($SESSION['name']= NULL) {
?>
<div><strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]</div>
<?php
}
?>
I have fixed it , thank you for everyone that has commented. for anyone else with this problem. here is my solution.
<?php
if ($_SESSION['name']!= NULL) {
?>
<strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]<br/>
<?php
}
?>
Update from your posted code:
For one, you are using $SESSION instead of $_SESSION. Second, you are setting $SESSION['name'] using a single equal sign, so the if condition would always fail because it is evaluating if (NULL). You need to use == or ===. Read up on comparison operators.
<?php
if (condition) {
?>
<div></div>
<?php
}
?>

Insert HTML in if statement

<?php
if(get_field('member_only_content')){
echo "do something";
}else{
echo "do something else";
}
?>
How do I insert HTML code where it says "do something"? When removing echo "do something"; and pasting the HTML code the Wordpress site crashes.
Close and open the PHP blocks, like this:
if(get_field('member_only_content')){
?>
<html></html>
<?php
} else{
?>
<html></html>
<?php
}
You can also use PHP's alternative syntax for this:
if(get_field('member_only_content')): ?>
<html></html>
<?php else: ?>
<html></html>
<?php endif;
Like this!
<?php
if(get_field('member_only_content')) : ?>
<div>Html stuff</div>
<?php else : ?>
<div>More Html stuff</div>
<?php endif;
Put HTML in place of the string.
<?php
if(get_field('member_only_content')){
echo "<your>HTML here</your>";
}else{
echo "<other>HTML</other>";
}
?>
You can also break out of the PHP tags
<?php
if(get_field('member_only_content')){
?>
<your>HTML here</your>
<?
}else{
?>
<other>HTML</other>
<?
}
?>
That would be because you placed the HTML code within php tags (<?php ?>). Text inside this tag is interpreted as a PHP instruction, thus it won't render HTML. There are 2 general ways render the HTML in a PHP file:
Echo the HTML
<?php
if (get_field ('member_only_content')) {
echo "<span>Put HTML here</span>";
}
else {
echo "<span>Put HTML here</span>";
}
?>
Place the HTML Outside PHP Tags
<?php if (get_field ('member_only_content')): ?>
<span>Put HTML here</span>
<?php else: ?>
<span>Put HTML here</span>
<?php endif;?>
Or you can use the <<< statement:
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
Straight from http://php.net/manual/en/function.echo.php
<?php
if(get_field('member_only_content')){
echo "<div style='background-color: #888888'>This is a simple string between div's, make sure you've to be careful while inserting too many single and double quotes in this string as well as inline styles</div>";
}else{
echo "do something else";
}
?>
Be careful while inserting quotes in your string.
Sample code
echo("<h1>This is a sample</h1>");

PHP Easy way to combine PHP if statement with HTML

This probably might be a silly question, but what I am trying to do is to make an if statement to do the following:
<?php if ($_SESSION['login'] == true) { ?>
Display this HTML code (without converting it to PHP echos
<?php } else { ?>
Display this instead
<?php } ?>
Or will I need to echo, and in turn escape all the required characters in order to do what I am after.
Thanks
Just try it out. But for the record, this works. And is in fact an idiomatic way of solving this.
<?php if ($_SESSION['login']) { ?>
<p>Display this HTML code</p>
<?php } else { ?>
<p>Display this instead</p>
<?php } ?>
Indented for readability (however, this messes with the HTML structure indentation so maybe it’s not appropriate).
Alternatively, the following style is often used because the lone brace at the end gets lonely:
<?php if ($_SESSION['login']): ?>
<p>Display this HTML code</p>
<?php else: ?>
<p>Display this instead</p>
<?php endif; ?>
(In both cases I’ve removed the == true from the conditional because it’s utterly redundant. Don’t write == true.)
you can also use if and endif
<?php if ( expression ) : ?>
<p>some message here</p>
<?php else : ?>
<p>other message</p>
<?php endif ?>
Look into the HEREDOC or NOWDOC syntax
<?php
if ($_SESSION['login']) {
$html =<<<HTML
Add HTML here
HTML;
echo $html;
} else {
$other_html =<<<'OTHERHTML'
Add HTML here
OTHERHTML;
echo $other_html;
?>
Anything not in PHP tags will be outputted as HTML anyway, so your original code will work fine.
<?php if ($_SESSION['login'] == true) { ?>
Log Out
<?php } else { ?>
Login
<?php } ?>
Yes, it works.
<?php if ($_SESSION['login'] == true) { ?>
<span>hello</span>
<?php } else { ?>
<span>going already?</span>
<?php } ?>

Categories