Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:42:15+00:00 2026-05-27T02:42:15+00:00

In PHP, I’m using an if statement to identify whether a user is logged

  • 0

In PHP, I’m using an if statement to identify whether a user is logged in or not, and depending on the result, displaying the main menu (if logged in) or a “you need to login” message if not. I am doing this like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <title>Home</title>
</head>
<body>
    <div id="header">
       <a href="index.php"><img src="wtcdblogo.png" alt="WTC DB logo" /></a>
    </div>
    <?php 
       if($_SESSION['loggedIn'] == 1) {
          echo "<div id='main'>MAIN MENU stuff goes here</div>";
       } else {
          echo "<div id='main'>Please login...</div>";
       } 
    ?>
</body>
</html>

As you can see, the code to display either the main menu or the “please login” message is produced by an echo. Is this bad practice, perhaps there’s a better way?

By the way, I’ve cut out most of the HTML from the echos in my snippet above. The main menu is made up of a list, but I didn’t bother including that as it’s irrelevant to the question, I guess.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:42:15+00:00Added an answer on May 27, 2026 at 2:42 am

    I consider it to be bad practice. Not sure about what anyone else thinks. For one thing, it looks terrible in text editors with syntax highlighting, then you have to worry about escaped strings, etc.

    This is how I do it:

       <div>
          <? if ($_SESSION['loggedIn'] === 1): ?>
             <div id="main">Main Menu stuff goes here</div>
          <? else: ?>
             <div id="main">Please log in...</div>
          <? endif ?>
       </div>
    

    You can hop out of the PHP tags and use straight up HTML. There are pros and cons to doing it this way. I like it way better than echoing stuff out. Other options would be to render new views into those areas based on the results of the if statements. Lots of possibilities, but the above is just one way to make that a little cleaner and (I think) better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP, as we all know is very loosely typed. The language does not require
----- PHP and mySQL ----- I have two quick questions need some advice. On
PHP having function to get values of a form using '$_POST' for 'method=post' '$_GET'
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Php is a service program, it can not do a multi threading work. It
PHP seems to use a rather annoying method for setting the evnironment timezone, using
I'm trying to create an if statement in PHP that prevents a single post
php isn't really my thing but I would need to get the votes and
‘php.exe’ is not recognized as an internal or external command, operable program or batch
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.