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.
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
I have made a contact form in HTML5 and I would like to validate It with PHP. I used a method what was show by Brad Hussey in a UDEMY course. So I got the data with a POST method. Then comes this code:
if ($_POST) {
if (!$_POST['nev']) {
$error .= "Kérem töltse ki a név mezőt!<br>";
}
if (!$_POST['email']) {
$error .= "Kérem adja meg az email címet!<br>";
}
if ($_POST['email'] && STfilter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "A(z) $emailcim cím nem valós";
}
if ($error != "") {
$error ='<div class="alert alert-danger" role="alert">Hiba lépett fel!<br>' . $error . '</div>';
} else {
$nev = $_POST['nev'];
$emailcim = $_POST['email'];
include 'phpmailer.php';
}
}
The phpmailer.php works fine if I rune it alone. And If I skip the validation and go directly to phpmailer.php everything is okay. So the problem should be inside this 3 IF statements, just I can't figure It out :(
Do you have any idea?
update: Sorry I didn't write down well the problem. It is just simple white page. So when I click to the submit button, It doesn't send the mail, and not going back to the site. It became a totaly empty page. Something like when I miss a ; sign. But I checked It many times (with syntax checker too), and nothing is missing.
I think the problem could be somewhere here:
if ($_POST['email'] && STfilter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "A(z) $emailcim cím nem valós";
}
I don't really understand the working of this part.
Tuğca Eker was right!
The problem was the STfilter_varprefix.
The correct statement is this:
if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
$error .= "A(z) $emailcim cím nem valós";
}
This question already has answers here:
Checking if string contains "HTTP://"
(7 answers)
Closed 7 years ago.
recently I started getting bots from one website that keeps posting their website links in my "Customer Feedback" form. I want to make my form deny any text that contains "http://" or any other words/phrases I will add (they will surely find a way to bypass the "http://"), but the thing is I don't know how to do so. Here is the code (the forms that need to be checked for "http://" are $name and $comment):
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$ip = $_SERVER['REMOTE_ADDR'];
$datetime = date('Y-m-d H:i');
$checkIp = mysql_query("SELECT ip from comments WHERE ip = '$ip'");
if (mysql_num_rows($checkIp) > 0) {
echo "Only 1 feedback per IP allowed!";
$IP = mysql_fetch_array($checkIp);
print_r($IP);
}
if($name){
if($email){
if($comment){
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
mysql_query("INSERT INTO comments (id, name, email, comment, ip, datetime) VALUES ('','$name','$email','$comment','$ip','$datetime')");
}
else
echo "The email address is invalid!<br><br>";
}
else
echo "You haven't entered any comment!<br><br>";
}
else
echo "You haven't entered an email address!<br><br>";
}
else
echo "You haven't entered your name!<br><br>";
}
Thank you!
You need to check and see if a substring is contained in the $name or $comment variables like so:
if (strpos($comment,'http://') === false and strpos($name, 'http://') === false) {
echo 'continue executing your code here';
}else{
echo 'Its a bot!';
}
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
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.
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)))