header not working with $_SERVER

Discussion in 'PHP' started by tableman, Sep 18, 2006.

  1. #1
    A button is chosen and clicked on one page, and a string to identify a page corresponding to that particular button is appended to the URL of the second page, where the visitor completes a form and upon submission the string is appended to the third (confirmation) page, which times out and a fourth page opens which corresponds to the original button.

    Everything worked when I used javascript and php on the second page:
    
    <script language="javascript" type="text/javascript"> 
    function redirectPage()
    { 
    window.location.href="<?php echo 'absolute_url.com/third_page.php?'.$_SERVER['QUERY_STRING']; ?>"; 
    } 
    </script> 
    
    Code (markup):
    However, I want to do this in a separate php file. So I try this in the php file (using an absolute url):

    
    header ('Location: absolute_url.com/third_page.php?'.$_SERVER['QUERY_STRING']);
    
    PHP:
    The third_page.php opens, but now it is without the string in the URL, so now we don't get to the fourth page.

    Any suggestions as to how to again get recognition for $_SERVER['QUERY_STRING']?
     
    tableman, Sep 18, 2006 IP
  2. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    just curios .. in the javascript ... did you make sure that $_SERVER['QUERY_STRING'] has a value??? coz if it does not then the second redirection does not have it as well
     
    VONRAT, Sep 18, 2006 IP
  3. tableman

    tableman Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The value is generated by the first page where the visitor selects and clicks a button. The value (string) is appended to the URL of the second page so that the visitor will end up with the correct page.

    That final correct page corresponding to the button clicked on the first page is identified by the string which is appended to the URL of the second page which passes it to the URL of the third page which then opens the correct fourth page.

    The problem is that it is now not being passed from the second to the third page even though the value is there in the query string of the second page.
     
    tableman, Sep 18, 2006 IP
  4. kjewat

    kjewat Active Member

    Messages:
    149
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    This code works fine for me...
    
    <?php
    $location="http://yourwebsite.com/page.php?".$_SERVER['QUERY_STRING'];
    header("Location: $location");
    ?>
    
    PHP:
    The query string is appended to the new location as expected.
     
    kjewat, Sep 18, 2006 IP
  5. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    used the following code to test it

    
    <?
    	if ( isset($_SERVER['QUERY_STRING']) ) {
    		header ('Location:third_page.php?'.$_SERVER['QUERY_STRING']); 
    	} else {
    ?>
    
    <script language="javascript" type="text/javascript"> 
    function redirectPage() { 
    	window.location="<?
    		if ( isset($_SERVER['QUERY_STRING']) ) {
    		 	echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
    		} else {
    			echo $_SERVER['PHP_SELF']."?a=1";
    		}
    		 ?>"; 
    } 
    </script>
    
    <input name="go" type="button" value="Go" onClick="redirectPage()">
    
    <?
    	}
    ?>
    
    PHP:
     
    VONRAT, Sep 18, 2006 IP
  6. tableman

    tableman Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Good suggestion kjewat.

    It's late where I am. I will try it in the morning.
     
    tableman, Sep 19, 2006 IP
  7. tableman

    tableman Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Here is what the URL string looks like in the second page:

    
    http://absolute_url.com/second_page.php?quoteId=2&carrierId=26&state=MT&agent_id=2687&lead_id=3244962&premium=$131.15&deductible=$5,100&coinsurance=0%&copay=N/A&effective_date=10/01/06&plan_type=PPO&planId=16
    
    Code (markup):
    Everything after the "?" was generated by the first page and passed to the second page.

    I have tried the suggestions but that string after the "?" still does not appear in the URL of the third page when it opens.

    As stated in the original post, it all works when I use the javascript that I posted.

    However, what I am trying to do, is use a separate php file without any javascript. That attempt is what is not working.

    I appreciate your input, VONRAT, but you included javascript.

    I tried kjewat's idea and some variations on it, but the string still does not show up in the URL of the third page.

    If this makes any difference, this is what the form statement in the second page is like:

    
    <form method="post" action="http://absolute_url.com/separate_file.php" name="second"  />
    
    Code (markup):
    separate_file.php contains the php code.

    An email of entries would be sent using mail() and the code in question is included to open the third page.

    However, in trying to solve this problem, everything is commented out in separate_file.php except the code in question.

    The string data is still being ignored.

    There must be some way to get it recognized.
     
    tableman, Sep 19, 2006 IP
  8. edD

    edD Peon

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    There should be nothing in $_SERVER['QUERY_STRING'] in separate_file.php since there _is no_ query string in the url. You'll probably need to append your query string to the url that is in the action attribute of your form tag.

    Example :
    <form method="post" action="http://absolute_url.com/separate_file.php<?php echo 'key='.$var1.'&key2='.$var2; ?>" name="second" />
     
    edD, Sep 19, 2006 IP
  9. tableman

    tableman Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    edD, you are absolutely correct. That was the problem, exactly.

    Now it all works.

    I used a variation on your suggestion:

    
    <form method="post" action="<?php echo http://absolute_url.com/separate_file.php?'
    .$_SERVER['QUERY_STRING']; name="second" ?>" 
    
    Code (markup):
    Since the string changes with each visitor's choices, $_SERVER['QUERY_STRING'] is needed.

    Thank you for your insight!

    Resolved.
     
    tableman, Sep 20, 2006 IP