What's wrong with my simple captcha php script? - php

I also have session_start(); at the top of my index page, its not working!
<?php
if(isset($_REQUEST['Submit'])){
$key=substr($_SESSION['key'],0,5);
$captcha = $_REQUEST['captcha'];
if($captcha=!$key){
exit();
}
$EmailFrom = "****";
$EmailTo = "****";
$Subject = "****";
$contactname = Trim(stripslashes($_POST['contactname']));
$companyname = Trim(stripslashes($_POST['companyname']));
$username = Trim(stripslashes($_POST['username']));
$phone = Trim(stripslashes($_POST['phone']));
$email = Trim(stripslashes($_POST['email']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=../404.php/\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Contact Name: ";
$Body .= $contactname;
$Body .= "\n";
$Body .= "Company Name: ";
$Body .= $companyname;
$Body .= "\n";
$Body .= "Preferred Username: ";
$Body .= $username;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print '<script type="text/javascript">';
print 'alert("Your Submission will be reviewed by an Admin and you will receive an email shortly")';
print '</script>';
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://****.com/****/\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=../404.php/\">";
}
?>

You are missing a } at the end (before ?>)
Be sure to indent your code correctly to easily catch these errors.

Add another curly bracket at the end and it should be ok i think
You are not closing the curly bracket for your top level if statement if(isset($_REQUEST['Submit'])){
OR based on how you want your flow you can remove the curly bracket from
if($captcha=!$key){
exit();
So that it becomes
if($captcha!=$key)
exit();
Notice that i changed =! to != as well

Related

After submitting a form need to show a variable value on popup just before redirecting to thankyou page

I need to show a popup to user before redirecting him/her to the thankyou page.
on popup I need to show the random code I have generated. Popup contains a button "continue", after clicking on it, popup should close and then redirect to the thankyou page.
CONFIG:
include("connectiondb.php");
# LIST EMAIL ADDRESS
$recipient = "forms#brand.com";
# SUBJECT (Subscribe/Remove)
$subject = "Hello ";
# RESULT PAGE
$location = "/thank-you.php";
## FORM VALUES ##
$sender = "support#brandname.com";
if (isset($_POST['Termscheck'])) {
$termsCheck = "Yes";
} else {
$termsCheck = "No";
}
# Generating unique alphanumaric code on each submission
function random_strings($length_of_string)
{
// String of all alphanumeric character
$str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
// Shufle the $str_result and returns substring
// of specified length
return substr(str_shuffle($str_result),
0, $length_of_string);
}
$gcbcode = random_strings(6);
# MAIL BODY
$subscriber_email = $_REQUEST['Email'];
$subscriber_subject = "THANK YOU";
$subscriber_email_data = file_get_contents('/email/queryFormThankyou.html');
# assigning values to email body
$body .= "Name: ".$_REQUEST['Name']." \n";
$body .= "Email: ".$_REQUEST['Email']." \n";
$body .= "Number: ".$_REQUEST['Number']." \n";
$body .= "Company Name: ".$_REQUEST['Companyname']." \n";
$body .= "Industry: ".$_REQUEST['Industry']." \n";
$body .= "Design's Company Name: ".$_REQUEST['DesignCompanyName']." \n";
$body .= "Title of Your design: ".$_REQUEST['Titledesign']." \n";
$body .= "Caption: ".$_REQUEST['Caption']." \n";
$body .= "UniqueFeatures: ".$_REQUEST['UniqueFeatures']." \n";
$body .= "1st preferred medium of communication: ".$_REQUEST['Preferred_medium_one']." \n";
$body .= "2nd preferred medium of communication: ".$_REQUEST['Preferred_medium_two']." \n";
$body .= "Prefer Time to Call: ".$_REQUEST['Meeting_time']." \n";
$body .= "Agrees with Terms and policy: ".$termsCheck." \n";
$body .= "GCP code: ".$gcbppcode." \n";
if($_FILES["file"]["error"]>0)
{
echo "FILE ERROR";
die();
}
$info = pathinfo($_FILES['wordfile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = time().'.'.$ext;
$target = '../Folder/'.$newname;
// move file to a folder
if (!move_uploaded_file($_FILES["wordfile"]["tmp_name"], $target)) {
// echo "Sorry, there was an error uploading your file.";
// die();
$target = 'No file attached';
}
$body .= "file: ".$target." \n";
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
else{ $sql = 'insert into abctable (name,email,phone,companyname,industry,DesignCompanyName,Titledesign,Caption,UniqueFeatures,Preferred_medium_one,Preferred_medium_two,meeting_time,agreeWithPolicy,gcp_code,uploaded_file_URL) values ("'.$_REQUEST['Name'].'","'.$_REQUEST['Email'].'","'.$_REQUEST['Number'].'","'.$_REQUEST['Companyname'].'","'.$_REQUEST['Industry'].'","'.$_REQUEST['DesignCompanyName'].'","'.$_REQUEST['Titledesign'].'","'.$_REQUEST['Caption'].'","'.$_REQUEST['UniqueFeatures'].'","'.$_REQUEST['Preferred_medium_one'].'","'.$_REQUEST['Preferred_medium_two'].'","'.$_REQUEST['Meeting_time'].'","'.$_REQUEST['Termscheck'].'","'.$gcbcode.'","'.$target.'")';
mysqli_query($con,$sql);
mysqli_close($con);
}
$headers = "From: " . $sender . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
mail( $subscriber_email, $subscriber_subject, $subscriber_email_data, $headers) or die ("Unable to send email to subscriber");
header( "Location: $location" );
Actually the purpose of this work is to provide user a unique code which he use after for further process.
This isn't the prettiest solution, but to pop up a window in the browser you can use javascript.
$str_result = 'Hello World!';
echo '<script type="text/javascript">alert(\'' . $str_result . '\' );</script>';
You're sort of mixing front-end behavior with back-end code, but this will at least show your user the $str_resultvalue.

contact form sending blank emails

Can anyone think of a logical reason to why a contact form which works perfectly well on my 123 reg account, will not work on my clients account? when I test it on my side, I get the email and the form contents through to my inbox perfectly but when the same codes are used on my clients account, with his email address, he receives an email with no data..
<?php
$EmailFrom = "webmaster#pb.co.uk";
$EmailTo = "loans#website.co.uk";
$Subject = "Testing";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "eMail: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://website/thankyou.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
change
"From: $EmailFrom"
to
"From: $EmailFrom" . "\r\n"
Add header.
$Body = 'MIME-Version: 1.0' . "\r\n";
$Body .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$Body .= 'From: '. $EmailFrom . "\r\n" . 'X-Mailer: PHP/' . phpversion();

access denied. PHP script

I have a contact form, when submitting on my page says 'Access denied.' There is nothing else, so I can't seem to figure out how to debug.
Here is the code :
<?php
$EmailFrom = "username#email.com";
$EmailTo = "username#email.com, username2#email.com";
$Subject = "Subject";
$Name = Trim(stripslashes($_POST['Name']));
$Company = Trim(stripslashes($_POST['Company']));
$Email = Trim(stripslashes($_POST['Email']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Message']));
// Validation
$validationOK = true;
if (! $validationOK)
{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// Prepare Email Body Text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Further comments: ";
$Body .= $Message;
$Body .= "\n";
// Send Email
$success = mail($EmailTo, $Subject, $Body, "From: <$Email>");
// Redirect To Success Page
if ($success)
{
echo '<script>alert("Thanks for your message, somebody will get in touch with your shortly.");</script>';
echo "<meta http-equiv=\"refresh\" content=\"0;URL=../contact\">";
}
else
{
echo '<script>alert("There has been an error, please try again later.");</script>';
echo '<script>history.back(1);</script>';
exit;
}
?>
I'm not sure why it's not working, could someone shed some light on this?
The headers of the page you're submitting the form to are actually returning a HTTP 403 Forbidden error.
I'd suggest checking the file permissions & ownership of the contactengine.php file are correct as a first step.

Adding Unicode UTF8 to email

Can anyone help me please how to add a utf-8 Unicode for these characters?
žýáťčšľľzŽŘ
Now the email looks like this
Dostali ste novú správu. Tu sú podrobnosti: Jméno: tár
Tel.Äíslo: 4996611
<?php
$EmailFrom = "...";
$EmailTo = "mail#mail";
$Subject = "Zpráva s ";
$name = Trim(stripslashes($_POST['name']));
$phone = Trim(stripslashes($_POST['phone']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Thanks
You've got two ambiguous parts of your script where character-sets aren't being considered. It's possible your form isn't sending UTF-8 or it's possible you're sending UTF-8 in your email but the client is expecting a different encoding.
I assume you're using a form to post data. Ensure it's set to send UTF-8 from the browser. The strings will now be UTF-8 encoded.
<form action="myform" accept-charset="UTF-8">
You need to add character set and transfer encoding headers to the email header:
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: 8bit");
BTW, a faster way to redirect the user is to set the Location header in the response. Use:
header('Location: contactthanks.php')

SMTP problem with PHP form in Godaddy hosting!

I was testing this (nice) simple form from CSS Tricks. In a website hosted in Godaddy.
And I got the following warning:
Warning: mail() [function.mail]: SMTP server response: 451 See
http://pobox.com/~djb/docs/smtplf.html
in
D:\Hosting\4923367\html\test\contactengine
on line 32.
(I checked the page but i didn't see anything useful)
contactengine.php
<?php
$EmailFrom = "chriscoyier#gmail.com";
$EmailTo = "janoochen#gmail.com";
$Subject = "Nice & Simple Contact Form by CSS-Tricks";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Any suggestions?
This link provides a solution. Try replacing your newlines with \r\n
In mail message headers and content, new lines are supposed to be denoted by both a carriage return (CR) and a line feed (LF)

Categories