Jump to content

Multiple variables in header() function


slyslick

Recommended Posts

I'm having an issue that I hope someone can help me with, I have a form that I am trying to get submitted to two PHP scripts. I submit to one, which takes the values does what it's suppose to do with them. Then I try to pass the values to the second script with the header() function, like this

 

header("Location:http://www.mydomain.com/form.php?first=$first&second=$second&third=$third");

die();

 

The scripts will work when used separately but will not when I use the code above to combine them. I think the issue is somewhere in this code that passes the value to the second form.

 

Any ideas anyone...

 

 

Link to comment
https://forums.phpfreaks.com/topic/127093-multiple-variables-in-header-function/
Share on other sites

Well I got redirected to the home page of my site. The second script is one from my hosting provider, it takes any values passed to it and sends them to me in an email with the exception of the subject and the redirect values which specify the subject of the email sent and the page the user should be redirected to after the form is submitted. It works on its own when the values are passed directly from the form but doesnt work when i try to pass them from myscript

Is the form's method POST? If so this may be the problem, as the script at the other end may be trying to retrieve POST values not GET values.

 

If you can modify the 2nd script it would be far easier. You can then change it to use GET values instead of POST or you can make it the REQUEST variables.

 

$_REQUEST stores both the GET and POST.

 

But I'm pretty sure the problem may lie in the fact that the script is expecting POST values but you are giving it none.

Please forgive my ignorance, I am teaching myself PHP (pretty sure you've heard that before).

 

First, the form method I used is POST and I am assuming the script is looking for that since it works when I use it by itself.

Second, I am not sure if I can modify the script from my host without affecting it. Here is the first few lines from that script

 

$request_method = $_SERVER["REQUEST_METHOD"];
    if($request_method == "GET"){
      $query_vars = $_GET;
    } elseif ($request_method == "POST"){
      $query_vars = $_POST;
    }
    reset($query_vars);
    $t = date("U");

 

Don't know if that will help.

 

Also if I understand you correctly, what you were suggesting is to use

$_REQUEST['value']

instead of

$_POST['value']

in the second script and that should fix it.

first of all, you should use $_POST instead of $HTTP_POST_VARS, as that is a deprecated variable name (e.g. use $_POST['first_value'] instead of $HTTP_POST_VARS['first_value'], they do the same thing, but the latter will probably be phased out in future versions of PHP.  Same thing with $_GET and $HTTP_GET_VARS)

 

secondly, when you pass data through the query string, e.g. page.php?variable=data&variable2=data2 etc., that is by definition a GET method.  Do you account for this on your second page?  Do you use $_GET (or $HTTP_GET_VARS) in your second page to retrieve the data that you're passing?  How does your second php page look as far as retrieving the passed data?

I did figure out that I should use $_POST instead of $HTTP_POST_VARS from something I read but I had already written the script.

 

I did not write the second script, it is one that was provided by my hosting provider. I do not fully understand everything that it is doing but the first few lines look to me like they check to see if the method ifs POST or GET. If it helps any, let me tell you what I am trying to do, maybe you can suggest another method to obtain the same result. I want to get name, email and comment from a user on my site

 

<form method="post" action="email.php">
<input type="hidden" name="subject" value="Email from mydomain.com" />
<input type="hidden" name="redirect" value="confirm.php" />
<p>Name:<input type="text" name="name" />
  </p>
<p>E-Mail:
    <input type="text" name="email" />
    
  </p>
<p>Comments:<br />
  <textarea name="comments" cols="50" rows="10"></textarea>
</p>
<p>
  <input type="submit" name="submit" value="Send message"/>
</p>
</form>

 

Then, I want to send them a confirmation email, that says your email was received, etc. This is what is done with the first script

 

<?php
$mysubject=$HTTP_POST_VARS['subject'];
$email=$HTTP_POST_VARS['email'];
$redirect=$HTTP_POST_VARS['redirect'];
$name=$HTTP_POST_VARS['name'];
$comments=$HTTP_POST_VARS['comments'];
$myredirect = "http://www.mydomain.com/gdform.php?subject=$mysubject&redirect=$redirect&name=$name&email=$email&comments=$comments";
$subject = "Confirmation email";
$body = "Thank you for your email";
$headers = "Content-Type: text/plain; charset=us-ascii\nFrom: Me <no_reply@mydomain>\nReply-To: [email protected]>\nReturn-Path: <[email protected]>\nX-Mailer: PHP";

if ($email != "") 
{
mail($email,$subject,$body,$headers);
}
header("Location:http://www.mydomain.com/gdform.php?subject=$mysubject&redirect=$redirect&name=$name&email=$email&comments=$comments");
die();

?>

 

This is where the problem lies, the data is not being passed to the second script which is from my hosting provider and I am not sure if it will still work if modified.

 

<?php
    $request_method = $_SERVER["REQUEST_METHOD"];
    if($request_method == "GET"){
      $query_vars = $_GET;
    } elseif ($request_method == "POST"){
      $query_vars = $_POST;
    }
    reset($query_vars);
    $t = date("U");

    $file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t;
    $fp = fopen($file,"w");
    while (list ($key, $val) = each ($query_vars)) {
     fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\n");
     fputs($fp,"$val\n");
     fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\n");
     if ($key == "redirect") { $landing_page = $val;}
    }
    fclose($fp);
    if ($landing_page != ""){
header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
    } else {
header("Location: http://".$_SERVER["HTTP_HOST"]."/");
    }


?>

 

These are the codes exactly as they are right now with the exception of my domain. As I said I have been learning PHP and have been trying to use this as my learning project but if there is a better way to do this then you can let me know.

A good way to check if certain parts of your code is working is to use print_r() on variables.  This will display them as output (sort of like echo, but it will work for all data types).

 

In the second script, try putting

print_r($query_vars);

after the $t = ... line.  You should see a list of variables that corresponds to the ones you put in your URL if it worked correctly.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.