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 6089783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:08:21+00:00 2026-05-23T12:08:21+00:00

Please review this Stackoverflow post. I have the same PHP problem as bob_cobb. Here’s

  • 0

Please review this Stackoverflow post.

I have the same PHP problem as bob_cobb. Here’s Brad Chrisite’s answer:

Order of operations.

Place your session creation and
test-for-validity check at the very
top of the page so the rest of the
page can make judgment calls off the
existence of $_SESSION[‘username’]

(Chances are you’re trying to validate
them inside the content area so your
“yay” or “ney” message appears in the
desired section of the document.
Pretty, yes, but the whole top-half of
the page can’t see that it’s
[potentially] a valid session.)

He is basically saying that session_start() and the conditionals that check for session variables should be at the top, so that the rest of the page could act based upon that.

However, my session-check is at the top of the page.

<?php
session_start();

if ($_SESSION['username']) 
//User is already logged in, echo the log out button.
...    

else 
//User is not logged in, echo the log in form & button.
...    

//Login form validation if user is not logged in and submitted form.
//At the end, create session variable ($_SESSION['username'])

//Destroy session if user pressed log out button.
session_destroy();
?>

Everything works fine, but, as with the poster of the other question, I have to refresh my page, to get the top script executed (the script that checks for $_SESSION[‘username’]).

Why is that?

  • 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-23T12:08:21+00:00Added an answer on May 23, 2026 at 12:08 pm

    Do not echo anything before your entire control flow is finished. What I mean by this is that you should work to separate logic from display (even better: use a pattern like Model-View-Controller). In your case, maybe you can do something like this:

    <?php
    /* Place all your control logic at the beginning.
       Do not echo anything in this block. */
    
    session_start();
    
    if ($_SESSION['username']) {
      $loggedin = true;
    } else {
      $loggedin = false;
      ...    
    
      //Login form validation if user is not logged in and submitted form.
      //If login succeeded, set $loggedin to true.
      //At the end, create session variable.
    }
    
    //Destroy session if user pressed log out button.
      session_destroy();
    
    /* Only display logic below, we do not change any state here */
    
    if($loggedin) {
      echo logout button
    } else {
      echo login form
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

StackOverflow - I'm still trying to deploy this site, but with every problem I
StackOverflow - I'm still trying to deploy this site, but with every problem I
Would someone please review this code and tell me why the for loops are
Please review my code and give me your advise on this: XML file: content.xml
This is kind of a follow to my other post about Unity (http://stackoverflow.com/questions/3998559/irepository-iservice-unity-in-an-asp-net-mvc-application-reference-question). Basically
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
StackOverflow, you're my only hope! I am unfortunate enough to have inherited an ASP.NET
Please review my code. For every my Entity i've created a service class, where
Please review my .htaccess code below. The code below was suggested by I User
In my application I am getting this error: You can only have one <head

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.