www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] How can I get header() to work? I can't put it before the output

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    74

    resolved [RESOLVED] How can I get header() to work? I can't put it before the output

    Hi,

    My index.php is constructed in such a way that I can't figure out how to use header() before any of the included php files.
    The thing is: I always stay on index.php, in the MAIN div because I use $_GET variable to define actions (such as action=login, =displaycategories, =viewpost) and I use include files, depending on which action is triggered.

    So when I use the login feature, I can't use header() to redirect the browser to index.php, because output has already been generated in the header.

    How can I solve this problem?
    Below is my code for index.php.
    Thanks a lot

    Code:
    <body>
    <div id="wrap">
    	<div id="header">
    	<center>this is the header</center>
    	</div>
    
            <div id="main">
    	
    // THIS is where all the PHP stuff happens. 
    
    	</div>
    	<div id="sidebar">
    		<?php include ('include/leftmenu.php');?>
    
    	</div>
    	<div id="footer">
    	<center>this is the footer</center>
    	
    	</div>
    </body>

  2. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Maybe something like this might work
    Code:
    <?php
    $somevar = $_GET['somevar'];
    
    if($somevar == "login" && !isset($_GET['notfirsttime'])){
    header = ('Location:http://www.mysite.com/?somevar=login&notfirsttime=1');
    }
    unset($_GET['notfirsttime'];
    ?>
    <html>
    <body>
    .....
    
    </body>
    </html>
    Last edited by Dasher; 02-09-2012 at 10:49 AM.

  3. #3
    Join Date
    Jan 2012
    Posts
    74
    Hi Dasher,
    Thanks for your response. I didn't understand what $somevar and $notfirsttime represent. What is your thought process?

    I found a way to solve the problem, here it is:

    PHP Code:
    if ((isset($_POST['login'])))        
        
    header("refresh:0;url=index.php");

    <
    html>
    <
    body>
    ... 
    Regards,
    Alex
    Last edited by alex12345; 02-09-2012 at 01:31 PM.

  4. #4
    Join Date
    Feb 2012
    Posts
    46
    You can always use header() before output, you should simply 'gather' all the output first. For example:

    PHP Code:
    ob_start();
    echo 
    'test';
    header('my header');
    ob_end_flush(); 
    What this will do is that it will gather everything that is output into a 'buffer' and ob_end_flush() will be the line that actually 'echoes' the output, so header() always comes before that.

    Note that you can do a lot more with output buffer, read more here.

  5. #5
    Join Date
    Jan 2012
    Posts
    74
    Hi Kristovaher,
    Thanks a lot for your response. I integrated your tip and it did solve my problem.

    Regards,
    Alex

  6. #6
    Join Date
    Feb 2012
    Posts
    46
    Quote Originally Posted by alex12345 View Post
    Hi Kristovaher,
    Thanks a lot for your response. I integrated your tip and it did solve my problem.

    Regards,
    Alex
    Glad to hear

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles