I need to add html to this page without echoing it. I only want to display it if there is a session id.
<?php
session_start();
//home.php
if($_SESSION['id'])
{
echo "Welcome ,".$_SESSION['username'] ;
echo "<br /><br /><a href='/login/logout.php'>Logout</a>" ;开发者_Go百科
echo "<br /><br /><a href='edit.php'>Edit Profile</a>" ;
echo "<br /><br /><a href='/profiles/".$_SESSION['username']."'>View Profile</a>" ;
}
else
{
echo "You don't belong here!";
}
?>
You mean like this?
<?php
session_start();
//home.php
if($_SESSION['id'])
{
?>
Welcome , <?php echo $_SESSION['username'] ?>
<br /><br /><a href='/login/logout.php'>Logout</a>
<br /><br /><a href='edit.php'>Edit Profile</a>
<br /><br /><a href='/profiles/<?php $_SESSION['username'] ?>'>View Profile</a>
<?php
}
else
{
echo "You don't belong here!";
}
?>
alternatively...
<?php
session_start();
//home.php
if($_SESSION['id'])
{
echo <<< END
Welcome , $_SESSION[username]
<br /><br /><a href='/login/logout.php'>Logout</a>
<br /><br /><a href='edit.php'>Edit Profile</a>
<br /><br /><a href='/profiles/$_SESSION[username]'>View Profile</a>
END;
}
else
{
echo "You don't belong here!";
}
?>
You may have to validate syntax, etc. I don't have an immediate way of testing these, so they were free-hand.
Assuming that you're setting $_SESSION["id"] somewhere (e.g. when the user logs in) this should work
if (isset($_SESSION["id"]))
{
// output HTML here
}
Why not just redirect if there is no ID?
session_start();
if(!$_SESSION['id'])
{
header( 'Location: /noentry.html' ) ;
}
else
{
.....
精彩评论