I have many pages which has the same form so I made a page called contactsGo.php and all the forms on the other pages should send to this page what I need is to redirect from contactsGo.php to each page sent the form

this is my code



    $subject="Contact us"; 

        $content = "New Message Form Contact us\n From: ".$name.",\n Email: ".$email.",\n Phone: ".$phone.",\n Company: ".$company.",\n Message: ".$message;
        $headers = 'From: info@siliconerubber-rtv2.com'."\r\n" .
        'Reply-To: info@siliconerubber-rtv2.com'."\r\n".
        'X-Mailer: PHP/' . phpversion();
        $retval=mail($recipient, $subject, $content, $headers);
        if( $retval == true ){header('Location:'.$host.$uri.'?msg=Message Has been sent successfully');}
        else{header('Location:'.$host.$uri.'?msg=Error sending message please try again later');}

and this is the form code

<form id="form1" name="form1" method="get" action="contactsGo.php?<?php echo $_SERVER['HTTP_HOST']; echo rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); ?>">
3 Years
Discussion Span
Last Post by diafol

hi you can use something like an identifier that will be send together with the data in contactsGo.php where you will use a simple IF statement to check if the data is from that specific form or not. and base on the IF Statement you can use a simple header redirect

something like

if ($identifier=="from_form1"){
 header("Location: form1.php")
header("Location: form2.php")

Edited by ehpratah


hi ehpratah there is no 'form2.php' it's just the name of the form, I need to get the url of the pages that send this form and redirect to this pages, beside it will be long code if, else I believe there is aother simple way to do that

Edited by johnef_sh


there are really other way to do it. i just show you a simple solution. can you elavorate what your trying to achieve without using this and this. from what i understand you just wanna know what page send the contact info and when the info is emailed succesfully it will return to the form where the info originated.


If you have a reusable form, I suggest that you have a php file just for the form (let's call it form.php for now). The form then has a hidden redirect control:

<form action="process.php" method="post">
    <!--other controls here-->
    <input type="hidden" name="redirect" value="<?php echo (isset($form_redirect)) ? $form_redirect : '';?>" /> 

IN your pages you can then do...

In contact.php

$form_redirect = "contact.php"; //this could be created dynamically from $_SERVER vars too.
require "includes/form.php";

In sendcomment.php

$form_redirect = "sendcomment.php"; //this could be created dynamically from $_SERVER vars too.
require "includes/form.php";

Alternatively, you could do something like this...

<form action="process.php" method="post">
    <!--other controls here-->
    <input type="hidden" name="redirect" value="<?php echo basename($_SERVER['PHP_SELF']);?>" /> 

And then just this in every page that needs it...

require "includes/form.php";

As users could spoof your form or use curl to send data including a malicious redirect, you would do well to provide a "whitelist" of allowable redirect values in your process.php:

$redirect = $_POST['redirect'];
$allowed_redirects = array("contact.php","sendcomments.php");

if(in_array($redirect, $allowed_redirects))
    ...do processing... then...
    header("Location: $redirect");
    ...bad call...
Votes + Comments
I second this approach
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.