header not working with $_SERVER

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tableman
Forum Newbie
Posts: 5
Joined: Mon Sep 18, 2006 3:20 pm

header not working with $_SERVER

Post by tableman »

I have some data being passed from page to page in the URL string.

On the middle page of the process, an email of visitor textbox entries

should be sent upon submit and a new page should open carrying the

data.

Code: Select all

$entries="First Name: $firstname\nLast Name:
$lastname\nButton: $applying $more_information"; 

mail ("email@big_isp.net", "this subject", $entries); 

header ('Location: next_page_url.com/confirmation.php'?.$_SERVER

['QUERY_STRING']);
When ?.$_SERVER['QUERY_STRING'] is appended to the URL, the result is a

blank page.

Without appending ?.$_SERVER['QUERY_STRING'] , the emal is sent, and

the next page opens.

Using javascript, appending $_SERVER['QUERY_STRING'] does work:

Code: Select all

<script language="javascript" type="text/javascript"> 
function redirectPage()
{ 
window.location.href="<?php echo 

'next_page_url.com/confirmation.php?'.$_SERVER['QUERY_STRING']; ?>"; 
} 
</script>
However, I need to do this without javascript.

I also tried

Code: Select all

header ('Location: next_page_url.php'?GETvar'.$_SERVER

['QUERY_STRING']');
and

Code: Select all

header ('Location: next_page_url.php'?POSTvar'.$_SERVER

['QUERY_STRING']');
Neither of those worked.

Any suggestions?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the "?" must be inside the string for it to work.. and use full URL references (http:// and all.)

Notice how the forum's highlighting makes your strings look odd. If you check your logs you should notice that you are getting a warning regarding the "?"
tableman
Forum Newbie
Posts: 5
Joined: Mon Sep 18, 2006 3:20 pm

Post by tableman »

Thank you for that observation, feyd.

You are correct. I had it right in the javascript but somehow transposed the ' and the ? in the php file.

Now the next page opens and an email is sent.

However, with that corrected, the data does now not transfer to the next page. Full URL references are being used.

The data is transferred when the php is in the javascript, but not with the php in a php file.

Any suggestions as to to what could fix it?
Last edited by tableman on Fri Sep 22, 2006 12:04 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The code you've posted thus far only uses relative URLs. A full URL has "http://" followed by a domain and other bits.

I'd like to see your new code, and the code on the following page that is looking for this passed data.
tableman
Forum Newbie
Posts: 5
Joined: Mon Sep 18, 2006 3:20 pm

Post by tableman »

The reason for what looked like relative urls in the post is because some forums don't allow absolute URLs in posts. If "http://" is in the post anywhere, the post is rejected, so it's easier to always leave it out. I assume they consider it may be a misuse of the forum with the intent of advertising or link building. I am using absolute URLs in practice.

The php file now looks like this (with ' and ? back where they belong):

Code: Select all

<?php
$entries="First Name: $firstname\nLast Name:  
$lastname\nButton: $applying $more_information";   

mail ("email@big_isp.net", "this subject", $entries);   

header ('Location: http://next_page_url.com/confirmation.php?'.$_SERVER['QUERY_STRING']);
?>
The passed data stays appended to each URL and identifies a particular page (out of hundreds) that opens when the confirmation.php page closes.

The exact code copied from the confirmation.php page that opens the particular final page is:

Code: Select all

<script language="javascript" type="text/javascript"> 
function redirectPage()
{ 
window.location.href="<?php echo 'http://prospectzone.com/quote/register5.jsp?'.$_SERVER['QUERY_STRING']; ?>"; 
} 
</script>
tableman
Forum Newbie
Posts: 5
Joined: Mon Sep 18, 2006 3:20 pm

Post by tableman »

The solution is implied in the fact that the javascript worked but the php does not, and my reference to "the php file". I had not stated explicitly that the php is in a separate file.

Since the php is in separate file, email.php, the data being passed must be sent to it by the form.

email.php can't send a value it does not have. The javascript worked because it is in the same page as the form, but the php is in email.php, a different file.

So the form now has:

Code: Select all

<form method="post" action="<?php echo 'http://next_page_url.com/email.php?'.$_SERVER['QUERY_STRING']; ?>" name="dual_button" />
Now the form not only sends entries to email.php, but also the data being passed from page to page.

Then, now that email.php has the query string data, that data can be sent on to the next page.

Instead of header (location), print and a meta refresh tag are now being used in email.php to avoid Internet Explorer popping up a nuisance security message which may scare some visitors, just because the next page is not in a secure subdirectory like the previous page. Other browsers do not do that.

Code: Select all

print '<meta http-equiv="refresh" content="0;URL=http://next_page_url.com/confirmation.php?'
.$_SERVER['QUERY_STRING'].'">';
Now it all works!
Last edited by tableman on Fri Sep 22, 2006 12:07 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For future reference, it's better to use sessions to transfer the form submission information from page to page.
tableman
Forum Newbie
Posts: 5
Joined: Mon Sep 18, 2006 3:20 pm

Post by tableman »

Thanks for that tip, feyd.

I will check out sessions.
Post Reply