Web Analytics Made Easy -
StatCounter Redirect not working properly - CodingForum

Announcement

Collapse
No announcement yet.

Redirect not working properly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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()?

    Comment


    • #3
      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

      Comment


      • #4
        ok, if you say so

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

        Comment


        • #5
          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

          Comment


          • #6
            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

            Comment


            • #7
              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.
              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

              Comment


              • #8
                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
                "Tango says double quotes with a single ( ' ) quote in the middle"
                '$Name says single quotes with a double ( " ) quote in the middle'
                "Tango says double quotes ( \" ) must escape a double quote"
                '$Name single quotes ( \' ) must escape a single quote'

                Comment


                • #9
                  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

                  Comment

                  Working...
                  X
                  😀
                  🥰
                  🤢
                  😎
                  😡
                  👍
                  👎