matt.sisto Posted March 31, 2009 Share Posted March 31, 2009 So I have preg_match working with one mail script, but exactly the same preg_match will not work with another. I am baffled. Please help: Working: <?php require "dbconn2.php"; $from = $_POST['email']; $sender = $_POST['name']; $message = $_POST['body']; $con_id =$_POST['con_id']; if (preg_match("/http:\/\//i", $from)){ header("Location: error.php"); exit(); } if (preg_match("/http:\/\//i", $sender)){ header("Location: error.php"); exit(); } if (preg_match("/http:\/\//i", $message)){ header("Location: error.php"); exit(); } else{ $sql = "SELECT email_address FROM consultant WHERE con_id = '$con_id'"; $result=mysql_query($sql); $to = mysql_result($result, 0, 0); $headers = "From: $from"; mail($to, $sender, $message, $headers); header("Location: message.php"); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>?Message Consultant</title> </head> <body> </body> </html> not working: <?php require "dbconn2.php"; $from = $_POST['email']; $sender = $_POST['name']; $message = $_POST['body']; $to = 'matt.sisto@gmail.com'; $headers = "From: $from"; if (preg_match("/http:\/\//i", $from)){ header("Location: error.php"); exit(); } else if (preg_match("/http:\/\//i", $sender)){ header("Location: error.php"); exit(); } else if (preg_match("/http:\/\//i", $message)){ header("Location: error.php"); exit(); } else{ mail($to, $sender, $message, $headers); header("Location: technical.php"); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>?Message Technical Support</title> </head> <body> </body> It just sends the message whatever the content? Appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/ Share on other sites More sharing options...
JonnoTheDev Posted March 31, 2009 Share Posted March 31, 2009 because you are using elseif and else conditions. you dont need to. compare the 2 scripts again. if you are using exit() then the script terminates so there is no requirement for else or elseif Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797820 Share on other sites More sharing options...
matt.sisto Posted March 31, 2009 Author Share Posted March 31, 2009 Sorry I posted the wrong code, That was me trying to get it to work. The actual script is: <?php require "dbconn2.php"; $from = $_POST['email']; $sender = $_POST['name']; $message = $_POST['body']; $to = 'matt.sisto@gmail.com'; $headers = "From: $from"; if (preg_match("/http:\/\//i", $from)){ header("Location: error.php"); exit(); } if (preg_match("/http:\/\//i", $sender)){ header("Location: error.php"); exit(); } if (preg_match("/http:\/\//i", $message)){ header("Location: error.php"); exit(); } else{ mail($to, $sender, $message, $headers); header("Location: technical.php"); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>?Message Technical Support</title> </head> <body> </body> It still doesn't work? ??? Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797828 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 you shouldn't need the Else {} statement. each of your IFs will exit if it is false. therefore, if all fields are valid you should be able to skip right to the send mail part Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797829 Share on other sites More sharing options...
matt.sisto Posted March 31, 2009 Author Share Posted March 31, 2009 I have just tried to remove the else {} statement and still no joy. Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797831 Share on other sites More sharing options...
JonnoTheDev Posted March 31, 2009 Share Posted March 31, 2009 Then do some debugging. Check these variables contain the expected data $from = $_POST['email']; $sender = $_POST['name']; $message = $_POST['body']; Remove the else statement at the end of the script also as lonewolf has stated Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797835 Share on other sites More sharing options...
JonnoTheDev Posted March 31, 2009 Share Posted March 31, 2009 You should also place this in a function as you are using the same test 3 times if (preg_match("/http:\/\//i", $message)){ header("Location: error.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797840 Share on other sites More sharing options...
matt.sisto Posted March 31, 2009 Author Share Posted March 31, 2009 Thank you it is sorted now, when I output the value of the variables it was not picking up 1 of the values, after checking the form there was a typo in the name. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797859 Share on other sites More sharing options...
JonnoTheDev Posted March 31, 2009 Share Posted March 31, 2009 Always, always, always check that the variables you test in conditions contain the expected values by simply printing them to the screen prior to posting your code for review! If the data is contained within the reserved variables $_POST or $_GET use: print_r($_POST); exit(); Quote Link to comment https://forums.phpfreaks.com/topic/151930-solved-why-doesnt-this-work/#findComment-797866 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.