1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Making one page not viewable to guests

Discussion in 'Customizations' started by Wes, Dec 9, 2014.

  1. Wes New Member

    In user groups I see that guests have the option to see or not see certain things - but not certain pages.

    I would like to make one of my pages available only to logged in members.
    Any ideas how to go about this?

    Thanks!
  2. Mike-N-Tosh Owner

    Depending on what you kind of "page" that you are talking about, you could just do it in the actual template for the page.

    Something like this:
    Code:
    {* Put this above what you want to block guests from *}
    {if $smarty.session.username<>'' || $smarty.cookies.username<>""}
    ... your page content ...
    {else}
    <div>
      You must be a registered user and logged in to view this page.
    </div>
    {endif}
  3. Wes New Member

    It is a Contest page that appears in the Nav bar. I added the page on in the "Page Editor"

    Does that make a difference?

    Thank you.
  4. Mike-N-Tosh Owner

    Yes, that makes a big difference. Pages made in the page editor all use the same template file and the contents of the page are stored in the database. These pages can't contain smarty code in them.

    However you can still do this with smarty like this. The content pages all use the ./templates/YOUR_TEMPLATE_FOLDER/content.tpl file.

    If you don't have that file in your template folder, copy it from the "default" folder to your template folder. Then using the same principle as in my earlier post, you just need to add an additional check for the page that you want to block like this:
    Code:
    {if $smarty.request.pg == "name-of-your-page"}
      {if $smarty.session.username<>'' || $smarty.cookies.username<>""}
        {$pPageContent}  <!-- This is already in the 'content.tpl' file -->
      {else}
          <div>
              You must be a registered user and logged in to view this page.
          </div>
      {endif}
    {else}
       {$pPageContent}
    {endif}
    
    To follow this code:
    1. The first "if" statement checks to see if it is the page you want to "block"
    2. If it matches that page it continues, if not it goes to last "else" which just shows the page normally.
    3. If it continues, it runs the second "if" statement which checks if you're logged in
    4. If you're logged in it shows the page normally
    5. If you're NOT logged in, it goes to the "else" statement which shows the notice (in the <div>.
    I haven't run or tested this, but I don't see why this wouldn't work.

Share This Page