redirect after send with phpmailer [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I want to redirect the user to a thank you page after the form is sucessfully submitted. I use the below script but get error message. please help.
<?php
session_start();
$name = check_input($_POST['name'], "Name cannot be empty.");
$email = check_input($_POST['email'], "Email address cannot be empty.");
if(!preg_match("/^([A-Za-z\s\-]{2,45})$/i", $name))
{
show_error("name not valid.");
}
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Email address not valid.");
}
htmlentities ($message, ENT_QUOTES);
require("../PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "$email";
$mail->AddAddress("myfriend#example.net");
$mail->Subject = "An HTML Message";
$mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!";
$mail->WordWrap = 50;
foreach(array_keys($_FILES['photo']['name']) as $key) {
$source = $_FILES['photo']['tmp_name'][$key]; // location of PHP's temporary file for this.
$filename = $_FILES['photo']['name'][$key]; // original filename from the client
$mail->AddAttachment($source, $filename);
}
/* Redirect visitor to the thank you page */
header('Location: pthankyou.php');
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlentities($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($Error)
{}
?>
Error Message
Warning: Cannot modify header information - headers already sent by
(output started at PHPMailer/class.phpmailer.php:1370) in process.php
on line 66

redirect() is a particular php built-in function that needs to be called before any output.
An output could be:
echo
html tags
var dumping
spacese before <?php tag
However, I didn't see redirect() included in your code. If you want more specific help, you have to include the snippet of code where your issue is.

Related

PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
please I'm new to web developing and PHP programming as well, I've a PHP form code and a form but whenever i test it on a server this the error message I get:
FATAL ERROR syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' on line number 18
And the code follows:
<?php
/* Set e-mail recipient */
$myemail = "test#mail.com";
/* Check all form inputs using check_input
function */
$yourname = check_input($_POST['yourname'],
"Enter your name");
$subject = check_input($_POST['subject'],
"Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'],
"Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $
email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/
i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
<html>
<body>
<b>Please correct the following error:</b>
<br />
echo $myError;
</body>
</html>
exit();
}
You have a redundant (read: wrong) line-break between $ and email. Replace:
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $
email))
with:
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))

PHP Form Header Location not working [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm trying to build a form using php error checking on the same page.
Everything seems to be working fine except redirecting visitors to another page on my website (thanks.html) after they've filled in the form correctly. First an email is sent to an email address provided at the top of my code and after that visitors should be redirected to the thankspage, which I can't get to work.
The redirecting is this part in my code:
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
Here's what I have in total:
<?php
/* Set e-mail recipient */
$myemail = "myemail#gmail.com";
if (!$_POST['submit'])
{
form();
} else {
if (empty($_POST['name'])) { $error0='<br>name'; }
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])) { $error2='<br>Valid email address'; }
if (empty($_POST['phonenumber'])) { $error1='<br>phonenumber'; }
if (empty($_POST['comment'])) { $error3='<br>comment'; }
$error_messages = $error0.$error1.$error2.$error3;
if ($error_messages)
{
echo "Please ensure the following fields are completed before submitting your form:<strong>". $error_messages ."</strong><br><br>";
form();
} else {
$name = $_POST['name'];
$email_address = $_POST['email_address'];
$phonenumber = $_POST['phonenumber'];
$comment = $_POST['comment'];
/* Message for the e-mail */
$message = "
New contact request:
Name: $name
Email address: $email_address
Phonenumber: $phonenumber
comment: $comment
";
$subject = "Contact";
$headers = "From: $email_address";
/* Send the message using mail() function */
mail($myemail, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
}}
?>
When checking for errors I get this result:
Warning: Cannot modify header information - headers already sent by (output started at form.php:11).
Form.php:11 is this line of code:
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])) { $error2='<br>Valid email address'; }
So after viewing other posts on stackoverflow I guess the issue might be whitespace or a header that has already been sent. But I still I can't it to work.
Could somebody help out and give a suggestion to fix this?
You are correct - You can't redirect if you've output anything to the browser. This goes for spaces at the beginning, anything really.
To get around this, check out the ob_* functions on php.net. Specially, check out flush. You'll be able to bypass this.
Hope that helps. Post again if you have ob_* issues.
As a side note, eregi has be deprecated as of 5.3.0 so its probably not a good idea to use it. Instead of using
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])
use
filter_var($_POST['email_address'], FILTER_VALIDATE_EMAIL)
see http://php.net/manual/en/function.filter-var.php for more info.
Regards

php code is not changing the header [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
It is giving me this and not changing the header:
Warning: Cannot modify header information - headers already sent by
(output started at /home/content/27/10711827/html/contact.php:2) in
/home/content/27/10711827/html/contact.php on line 24
Here is the code.
<?php
if (empty($_POST) === false){
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) === true || empty($email) === true || empty($message) === true){
$error[] = 'Name, email and message is required!';
} else{
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'That\'s not a valid email address';
}
if (ctype_alpha($name) === false){
$errors[] = 'Name must only cotain letters';
}
}
if(empty($errors) === true){
mail('houseblendrecords#gmail.com', 'Contact form', '$message', 'From: ' . $email);
header('Location: contact.php?sent');
exit();
}
}
This happens when your code has already produced some content by the time you're trying to send a header. Make sure that your code isn't producing any warnings. If you're sure that's not the case, make sure that there aren't any empty lines before the opening <?php tag.
If it is not an issue with white space before the opening <?php tag also check your file encoding. If your files are saved as UTF-8 make sure they are saved as UTF-8 without bom, or else resave it as ANSI.

PHP Validation showing all errors at once

I am using the below script for validation. I want to know if there is a way to convert this script to show all errors at once instead of one at a time? Also, is there anything more I can do to prevent header injection?
thanks.
<?php
session_start();
/* Check all form inputs */
$fname = check_input($_POST['fname'], "Friend's Name cannot be empty.");
$femail = check_input($_POST['femail'], "Friend's email cannot be empty.");
$yname = check_input($_POST['yname'], "Your Name cannot be empty.");
$yemail = check_input($_POST['yemail'], "Your email cannot be empty.");
$subject = check_input($_POST['subject'], "Subject cannot be empty.");
$comments = check_input($_POST['comments'], "Comments cannot be empty.");
/* alphabet only */
if(!preg_match("/^([A-Za-z\s\-]{2,45})$/i", $fname))
{
show_error("Friend's name is not valid.");
}
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $femail))
{
show_error("Your friend's email address is not valid.");
}
if(!preg_match("/^([A-Za-z\s\-]{2,45})$/i", $yname))
{
show_error("Your name is not valid.");
}
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $yemail))
{
show_error("Your email address is not valid.");
}
htmlentities ($message, ENT_QUOTES);
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlentities($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
Something I often do is use an $errors array. So you would define an empty array before all your checks, and then inside of each check for show_error you would add that string to your errors array:
$errors[] = "Friend's name is not valid.";
Then at the end, check to see if the error array is empty. If it is, then nothing failed. Otherwise, you now have an array of all the errors that you can display however you'd like.

Form not sending, function eregi() is deprecated [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
Escape string to use in mail()
I am trying send my form, but I get this error...
Deprecated: Function eregi() is deprecated
I tried replacing it with preg_match(), but no luck. Here is my code:
$all_valid = $name_valid = $email_valid = $comments_valid = true;
if (isset($_POST['submit'])) {
if ($_POST['name'] == '') {
$all_valid = $name_valid = false;
}
if ($_POST['comments'] == '') {
$all_valid = $comments_valid = false;
}
if (!$validator->check_email_address($_POST['email'])) {
$all_valid = $email_valid = false;
}
if ($all_valid) {
// #### NO PROBLEMS FOUND - PROCESS THE FORM DATA HERE
$mail_to = 'cat30#hotmail.com'; // recipient address
$subject = "Email from website"; // email message subject line
$name = mysql_real_escape_string(trim($_POST['name'])); // sanitize the name
if (eregi("\r",$name) || eregi("\n",$name)){ // avoid email header injection
die();
}
$mail_from = mysql_real_escape_string(trim($_POST['email'])); // sanitize their email address
if (eregi("\r",$mail_from) || eregi("\n",$mail_from)){ // avoid email header injection
die();
}
$comments = htmlspecialchars(trim($_POST['comments'])); // convert HTML characters into entities
$headers = 'From: '. $mail_from. "\r\n";
mail($mail_to, $subject, $comments, $headers);
$response = '<h2>Thanks for contacting us, will get back to you soon</h2>';
}
}
It returns a slightly different value than eregi but if I'm reading your code correctly you should be able to use the strpos() function to determine if a substring exists in a string. Eregi ignores case so you might have to combine this with a strtolower($string) call too.
Something like this:
if (strpos("\r",strtolower($name)) || strpos("\n",strtolower($name)))

Categories