Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    Thanked 0 Times in 0 Posts

    nesting if statements in html for session

    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
    Code:
    <? 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?
    Last edited by thelung; 09-16-2011 at 06:54 AM.

  • #2
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    you can echo out the entire html within php like this

    PHP Code:
    <? 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
    PHP Code:
    session_start(); 

  • Users who have thanked ASTP001 for this post:

    thelung (09-18-2011)

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    It should be noted that there was nothing really wrong with your original code snippet, providing you format it properly:
    PHP Code:
    <?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.

  • Users who have thanked BluePanther for this post:

    thelung (09-19-2011)

  • #5
    New Coder
    Join Date
    Sep 2010
    Posts
    34
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •