SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    header function....

    I had the following code at the top of a page thinking that it prevented the rest of the page from executing if the condition was met.

    PHP Code:
    if(!$um->isLoggedinUser())
        
    header('Location: index.php'); 
    Obviously the simple solution would be to add an else clause such as..

    PHP Code:
    if(!$um->isLoggedinUser()){
        
    header('Location: index.php');
    }
    else{
    //Execute the rest of the page

    Or another option would be,

    PHP Code:
    if(!$um->isLoggedinUser()){
        
    header('Location: index.php');
            die();

    But I was surprised that the header function waited until the end of the page to work. Is that a configuration thing, or just the norm?

  2. #2
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Best would be:
    PHP Code:
    <?php
    ob_start
    ();
    require_once(
    'authentication.class.php');
    ..
    if(!
    $um->isLoggedinUser()){
        
    header('Location: index.php'); 
        exit();
    }
    //rest code goes here....
    ?>

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The header function does nothing more than send an http header. Sending an http header doesn't, and shouldn't, make the script stop executing.

    php will send the headers as soon as you send some output(text, html etc..), unless output buffering is enabled. That doesn't mean the browser will immediately read it though. A location header is strictly an advisory to the remote client(browser). The browser doesn't need to obey if it doesn't want to.


Bookmarks

Posting Permissions

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