...

View Full Version : nesting if statements in html for session



thelung
09-16-2011, 05:49 AM
I'm trying to put together a website for which I've already gotten the design figured out and coded up.
I'm using a lot of php includes to call files with content and forms on the various pages. But, I want to add php sessions to some of the pages to restrict privileges to only registered users.
I know this is very basic, but I'm hung up on something. I'm sure that half of my battle is my terrible syntax. But, even with that being said, I don't know how this is done.
Do I need to 'echo' the html tags (numerous tags) individually into the if statement or can i break the if/else statement like


<? php if ($_SESSION['username'])
{ ?>
<div id = "blocks_of_phpIncludes">
<? include '../form/muliple.php'; ?>
<? include '../tables/foo.php'; ?>
</div>
}
<? else{
<div id = "blocks_of_phpIncludes">
<? include '../forms/signIn.php'; ?>
</div>
}
?>


Is there some way of doing something like this? Or, do I just need to start with <?php and echo everything into their respective places throughout each page that will require the session be set?

ASTP001
09-16-2011, 07:45 AM
you can echo out the entire html within php like this



<? php if ($_SESSION['username'])
{
echo '<div id = "blocks_of_phpIncludes">';
include('../form/muliple.php');
include('../tables/foo.php);
echo '</div>';
}
else{
echo '<div id = "blocks_of_phpIncludes">';
include('../forms/signIn.php');
echo '</div>';
}

?>


Edit: Make sure you begin the session on top of the webpage that you're using it in. The function for that is
session_start();

thelung
09-18-2011, 03:33 PM
Okay, I see what your saying. Thanks.
Now, if I wanted to have the sign-in form nested between various php includes (like some photos, dynamic text from admins, etc); I would then likely have to create like a single "signIn.php" that contains the sign-in form and includes the php includes into that single php file, calling it in the else statement used in the page that is demanding session validation?

BluePanther
09-18-2011, 03:39 PM
It should be noted that there was nothing really wrong with your original code snippet, providing you format it properly:


<?php if ($_SESSION['username']) {
?>
<div id = "blocks_of_phpIncludes">
<?php include '../form/muliple.php';
include '../tables/foo.php'; ?>
</div>
<?php
}
else {
?>
<div id = "blocks_of_phpIncludes">
<?php include '../forms/signIn.php'; ?>
</div>
<?php
}
?>

Notice the use of <?php, not just <?. There's entirely nothing wrong with doing that.

As for your sign in page, if you mean that you include it on the result from a condition that your client isn't signed in, then you're correct.

thelung
09-19-2011, 01:45 AM
It should be noted that there was nothing really wrong with your original code snippet, providing you format it properly:

Awesome! Thanks! That means I can keep the layout concept that I had worked so hard for and implement the session at the same time. Now it's just a matter of cleaning up a ton of messy code. Thanks a mil, guys! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum