How to print an echo statement before header

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bannu
    New Member
    • Sep 2012
    • 2

    How to print an echo statement before header

    Hi All,

    I prepared a project where session starts while log in and will be log out by checking through the same session. When I log out from the page I kept header("locatio n:index.php"); so it redirects to the log in Home page as per my code. Well, now I would like to print a statement SUCCESSFULLY logged out along with the index page. Tried many ways and used ob_end_flush(); but unable to meet my requirement.

    Otherwise it would be fine that the Content executes for few seconds and then it should redirect to the index page.

    Can anyone help me out on this please..
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    You cant achive your requirements with your current method. there is no way you print something before sending header. If you were actually allowed to do so your browser would failed to read your data and create unknown response error. what you can do is create timer first echo that user has been logged out then after certain period call function in javascript to redirect page to other url just call the below code:
    window.location =url;

    Comment

    • jdstankosky
      New Member
      • Sep 2012
      • 30

      #3
      Can you set a session variable when the log out command is sent, and display it in a portion of your index.php, or do you kill the session first?

      Code:
      if (isset($_SESSION['logged_out']) && $_SESSION['logged_out'] == true) {
          echo $message;
      }
      Otherwise, could you include something similar with a $_POST variable?

      Comment

      • Bannu
        New Member
        • Sep 2012
        • 2

        #4
        Thanks for the replies @Johny,jdstanko sky

        Code:
        if(!isset($_SESSION['aid']))
        	{
        		header("location:index.php");
        		exit;
        	}
        	session_destroy();
        	echo "Successfully logged out from Admin Page";
        	
        	?>

        Finally I wrote the code as above.

        After log out it dispelling the echo statement. When I refresh the page it is redirecting to the index page..

        Is this right way?
        Last edited by Dormilich; Sep 26 '12, 05:43 AM. Reason: Please use [CODE] [/CODE] tags when posting code.

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          this is not wrong, but you are looking for redirecting to your index page. thats why I was talking about timer

          Comment

          • jdstankosky
            New Member
            • Sep 2012
            • 30

            #6
            Probably the simplest answer is Johny's with a javascript timer. Try looking up javascript function setTimeout() to accomplish what you had originally asked.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              a simple HTML redirect via <meta http-equiv="refresh" ...> works even with JavaScript turned off.

              Comment

              • johny10151981
                Top Contributor
                • Jan 2010
                • 1059

                #8
                Its a good one, I never knew

                Comment

                • jdstankosky
                  New Member
                  • Sep 2012
                  • 30

                  #9
                  My brain has forgotten about meta... :\

                  Comment

                  Working...