Reputation: 21
The else dosent work in this code and i have tied lot of things not just redirect()
<html>
<?php if($_SESSION['username']) : ?>
<?
echo "Hi, " .$_SESSION['username']. " | ";
?>
<a href='logout.php'>Logout</a>
<?
else
redirect();
?>
<?php endif; ?>
</html>
Upvotes: 0
Views: 120
Reputation: 3470
If you're doing a redirect then I would consider having that as the primary path through the code, that way it's clear at the top rather than buried 3/4 the way down. Plus you can forget about it in the rest of the page.
E.g.
<?php
if( ! $_SESSION['username'] ) {
redirect();
} else { ?>
stuff...
<?php
} >
Upvotes: 0
Reputation: 3698
Use colon for else condition. Also check your php support <?
or not. Try this:
<?php
else:
redirect();
?>
Upvotes: -2
Reputation: 31
the else misses a ':'
<html>
<?php if($_SESSION['username']) : ?>
<?
echo "Hi, " .$_SESSION['username']. " | ";
?>
<a href='logout.php'>Logout</a>
<?
else:
redirect();
?>
<?php endif; ?>
</html>
Upvotes: 1
Reputation: 28941
If the alternative syntax gets you confused, you can always use the more familiar curly braces:
<?php if($_SESSION['username']) { ?>
Hi, <?php echo $_SESSION['username'] ?> | <a href='logout.php'>Logout</a>
<? } else { redirect(); } ?>
Or do it all in PHP:
<?php
if($_SESSION['username']) {
echo "Hi, " . $_SESSION['username'] . " | <a href='logout.php'>Logout</a>";
} else {
redirect();
}
?>
I personally find those a lot cleaner.
Upvotes: 2
Reputation: 1781
Are you using a special framework to construct the page? normally, what you output with PHP can be HTML as well, so no need to start/stop the scripting language and instead try something like this:
<html>
<?php
if($_SESSION['username'])
{
echo "Hi, " .$_SESSION['username']. " | ";
echo "<a href='logout.php'>Logout</a>";
}
else
{
redirect();
}
?>
Upvotes: 1
Reputation: 7586
This should clear things up for you whilst using the alternative syntax: http://www.php.net/manual/en/control-structures.alternative-syntax.php
It should have a colon :
<?
else:
redirect();
?>
Upvotes: 8