PHP feedback form stopped working

Forgive me if this is basic, but this is my first effort at using PHP.

Until recently, I had a working feedback form using PHP which used the code below. Then one day recently it stopped working. Even if someone puts in a valid email address and name, they get the ‘feedback failed’ webpage and no feedback is emailed to me. I haven’t changed anything (I’ve taken out the live email and web addresses from the code listing below for safety is all), so does anyone have an idea why has the form stopped working, please?

<?php
/* All form fields are loaded */
$to = “me[at]myemailaddresshere.com” ;
$subject = “Feedback”;
$from = $_REQUEST[‘email’] ;
$name = $_REQUEST[‘contactname’] ;
$headers = “From: $from”;

$fields = array();
$fields{“contactname”} = “Name”;
$fields{“email”} = “Email”;
$fields{“comments”} = “Comments”;

$body = "Feedback has been received:

“; foreach($fields as $a => $b){ $body .= sprintf(”%20s: %s
",$b,$_REQUEST[$a]); }

$headers2 = “From: noreply[at]myemailaddresshere.com”;
$subject2 = “Thank you for your feedback”;
$autoreply = “Thank you text goes here”;

if (!preg_match(“/\w+([-+.]\w+)@\w+([-.]\w+)\.\w+([-.]\w+)*/”, $email)) {
header( “Location: http://www.mywebsiteaddresshere.co.uk/feedbackformfail.shtml” );
}
elseif ($contactname == “”) {
header( “Location: http://www.mywebsiteaddresshere.co.uk/feedbackformfail.shtml” );
}

/* Sends the mail and opens the feedbackformsuccess page if the mail is successfully sent, or the error page otherwise. */

elseif (mail($to, $subject, $body, $headers) /&& mail($from, $subject2, $autoreply, $headers2)/) {
header( “Location: http://www.mywebsiteaddresshere.co.uk/feedbackformsuccess.shtml” );
}
else {
header( “Location: http://www.mywebsiteaddresshere.co.uk/feedbackformfail.shtml” );
}

?>

Thanks. :slight_smile:

You don’t declare the $email variable anywhere. So your preg_match will always fail. If it worked before, it means your host has decided (finally) to switch off registrer_globals.

The condition $contactname == “” is always true. You do not define $contactname anywhere, so it is NULL. And NULL==“” is true.

I seem to have a guess on what has happened: register_globals at your system has been changed from on to off, right ? So $_REQUEST[‘contactname’] and $contactname are not the same any more.

Change


$name = $_REQUEST['contactname'] ;

to


$contactname= $_REQUEST['contactname'] ;

Edit: What Guido2004 said about $email is also absolutely correct. register_globals at your system has been definitely changed from on to off. You need to check $from instead of $email or rename the variable $from to $email everywhere …

Thanks both for the replies - form is now working again! :slight_smile:

So that I understand better what’s happening, could you explain to me…

Why did the change to register_globals on the server cause the form to fail and why did making the changes you suggested fix it?

because it’s the only register_globals effect and intention
http://php.net/manual/en/security.globals.php

Thank you for this.

I’m hoping that my form should carry on working in future now thanks to you guys. :slight_smile: