Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,096
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Redirect not working properly

    I am working on a module that allows Members to add comments to my articles. To do so, you must log-in or register.

    I am testing the Article123 ---> Log In ---> Redirect to Article123 path...

    My code was working last night except I was getting this annoying "Headers Already Sent" error message unless I added Output Buffering.

    Late last night I moved most of my PHP before my HTML to eliminate this issue, but now my re-direct is broken?!

    In my article.php I am setting the return page here...
    PHP Code:
    // Set Article Title.
    $articleTitle $_GET['title'];
    $_SESSION['articleTitle'] = $_GET['title'];

    // Set current Script Name + Query String.
    $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '/' $articleTitle
    When a User chooses to "Log In", the button that they click takes them to log_in2.php and this code is supposed to re-direct them back to the original article...

    log_in2.php
    PHP Code:
    // ****************************
    // Check for Member Record.        *
    // ****************************
    if (mysqli_stmt_num_rows($stmt)==1){
        
    // Member was Found.

        // Bind result variables.
        
    mysqli_stmt_bind_result($stmt$memberEmail$memberFirstName);

        
    // Fetch record.
        
    mysqli_stmt_fetch($stmt);

        
    $_SESSION['loggedIn'] = TRUE;
        
    $_SESSION['memberEmail'] = $memberEmail;
        
    $_SESSION['memberFirstName'] = $memberFirstName;

        
    // Redirect User.
        
    if (isset($_SESSION['returnToPage'])){
            
    header("Location: " $_SESSION['returnToPage']);
        }else{
            
    // Take user to Home Page.
            
    header("Location: " WEB_ROOT "index.php");
        }

        
    // End script.
        
    exit();
    }else{
        
    // Member not Found.
        
    $_SESSION['loggedIn'] = FALSE;

        
    $errors['pass'] = 'The E-mail and Password do not match those on file.';
    }
    // End of CHECK FOR MEMBER RECORD. 

    I am also using the following mod_rewrite...
    Code:
    RewriteEngine on
    
    #PRETTY:		articles/postage-meters-can-save-you-money
    #UGLY:		article.php?title=postage-meters-can-save-you-money
    
    RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?title=$1
    
    # Build Date: 2011-08-22 7:08pm

    As far as I can tell, when flow is re-directed back to "article.php", the GET variable is not getting set and this code is failing...

    article.php
    PHP Code:
    // Check for Title in URL.
    if (isset($_GET['title'])){
        
    // Title found in URL.

        // Set Article Title.
        
    $articleTitle $_GET['title'];
        
    $_SESSION['articleTitle'] = $_GET['title'];

        
    // Set current Script Name + Query String.
        
    $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '/' $articleTitle;

        
    // Build query.
        
    $q 'SELECT html_title, meta_description, meta_keywords, page_title, page_subtitle,
            written_on, author, body, reference_listing, endnote_listing
            FROM article
            WHERE article_title=?'
    ;

        
    // Prepare statement.
        
    $stmt mysqli_prepare($dbc$q);

        
    // Bind variable.
        
    mysqli_stmt_bind_param($stmt's'$articleTitle);

        
    // Execute query.
        
    mysqli_stmt_execute($stmt);

        
    // Transfer result set from prepared statement.
        // (Required for all queries that return results.)
        
    mysqli_stmt_store_result($stmt);

        
    // Check for Article Record.
        
    if (mysqli_stmt_num_rows($stmt)==1){
            
    // Article in Database.
            
    $articleExists TRUE;

            
    // Bind result variables.
            
    mysqli_stmt_bind_result($stmt$htmlTitle$metaDescription$metaKeywords$pageTitle$pageSubtitle,
            
    $writtenOn$author$body$referenceListing$endnoteListing);

            
    // Fetch record.
            
    mysqli_stmt_fetch($stmt);
        }else{
            
    // Article not in Database.
            // Take user to Home Page.
            
    header("Location: " WEB_ROOT "index.php");
        }
    }else{
        
    // Title not found in URL.
        // Take user to Home Page.
        
    header("Location: " WEB_ROOT "index.php");
    }
    // End of CHECK FOR ARTICLE IN URL. 
    And this path gets run...

    Code:
    }else{
    	// Title not found in URL.
    	// Take user to Home Page.
    	header("Location: " . WEB_ROOT . "index.php");
    }// End of CHECK FOR ARTICLE IN URL.

    Any ideas what my code re-write did to break things?!

    Thanks,



    Debbie

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    header() will not work if any output has been created before the call to header() in your code.

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
    Have you got any output generated before you call header()?

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,096
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by webdev1958 View Post
    header() will not work if any output has been created before the call to header() in your code.

    Have you got any output generated before you call header()?
    You missed *everything* I just said...


    Debbie

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    ok, if you say so

    I'm sure you'll find where you have gone wrong eventually then

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Quote Originally Posted by doubledee View Post
    As far as I can tell, when flow is re-directed back to "article.php", the GET variable is not getting set and this code is failing...
    That is correct, there is no value to GET on your redirect. The GET value comes from this (?title=postage-meters-can-save-you-money) in the URL but what you are using after someone logs in is simply /postage-meters-can-save-you-money so there is no GET value to get.

    Try changing your returnToPage session value to:

    PHP Code:
    $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '?title=' $articleTitle
    and see what happens.
    Dave .... HostMonster for all of your hosting needs

  • #6
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,096
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djm0219 View Post
    That is correct, there is no value to GET on your redirect. The GET value comes from this (?title=postage-meters-can-save-you-money) in the URL but what you are using after someone logs in is simply /postage-meters-can-save-you-money so there is no GET value to get.

    Try changing your returnToPage session value to:

    PHP Code:
    $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '?title=' $articleTitle
    and see what happens.
    I don't whether to cry for joy because your one line of code fixed things or because it was so simple and I wasted so much time in vain last night?!

    Thank You!!!!


    Two follow-up questions...

    1.) Why did this work when my PHP was in the middle of my HTML? (Vague, I know, but still...)


    2.) Is my code acceptable in how I am handling things (including your fix)?

    Apparently it is better that all of my logic/PHP is before the HTML display, and things do work okay so far, but did the code I posted look acceptable?!

    Thanks,



    Debbie

  • #7
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Quote Originally Posted by doubledee View Post
    1.) Why did this work when my PHP was in the middle of my HTML? (Vague, I know, but still...)
    Because the HTML had already been sent to the browser so your additional header command to redirect failed since headers had already been sent to the browser to output the HTML.
    Quote Originally Posted by doubledee View Post
    2.) Is my code acceptable in how I am handling things (including your fix)?
    Your logic looks reasonable from here.
    Dave .... HostMonster for all of your hosting needs

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,329
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    Quote Originally Posted by doubledee View Post
    Apparently it is better that all of my logic/PHP is before the HTML display, and things do work okay so far, but did the code I posted look acceptable?!
    Apparently? - I badgered you several times about this and you've only NOW realised just WHY I was telling you this

    Run logic first, generate any ouput required and then merge into HTML. Makes like a LOT easier for debugging Deb
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,096
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Apparently? - I badgered you several times about this and you've only NOW realised just WHY I was telling you this
    What would I do without all of your badgering (I mean "wisdom"), Tango?!


    Run logic first, generate any ouput required and then merge into HTML. Makes like a LOT easier for debugging Deb
    Yeah yeah yeah...


    Debbie


  •  

    Posting Permissions

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