I currently have the below email/db script that works fine but the redirect doesn't seem to be working. Instead of redirecting to the url its just showing the blank php page. How can I fix this?
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("register", $con);
$sql="INSERT INTO register_interest (Name, Email, Message, Website)
VALUES ('$_POST[Name]', '$_POST[Email]', '$_POST[Message]', '$_POST[Website]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
$to = "email";
$subject = "Interest";
$email = $_POST['Email'] ;
$message = $_POST['Message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if ($sent) {
header("Location: http://www.url.co.uk");
exit();
} else {
print "We encountered an error sending your email";
}
?>
<?php
$con = mysqli_connect("localhost","user","pass","register");
if (mysqli_connect_errno())
{
die('Could not connect: ' . mysqli_connect_error());
}
$name = mysqli_real_escape_string($con, $_POST['Name']);
$email = mysqli_real_escape_string($con, $_POST['Email']);
$message = mysqli_real_escape_string($con, $_POST['Message']);
$website = mysqli_real_escape_string($con, $_POST['Website']);
$sql = "INSERT INTO register_interest (Name, Email, Message, Website)
VALUES ('$name', '$email', '$message', '$website')";
if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
$to = "email#address.com";
$subject = "Interest";
$email = $_POST['Email'] ;
$message = $_POST['Message'] ;
$headers = "From: $email\n";
$sent = mail($to, $subject, $message, $headers) ;
if ($sent) {
header("Location: http://www.url.co.uk");
exit();
} else {
print "We encountered an error sending your email";
}
?>
Try this code :
if(!$sent){
print "We encountered an error sending your email";
exit;
}
header("Location: http://www.url.co.uk");
Check if you have any blank lines before the opening PHP tags), then the header directive will not work.
Related
Let me start by saying I have looked into the documentation and unfortunately did not manage to fix my problem. Mainly what I am trying to do is, after the user registers, an email is sent to him and a vkey is sent to my database. After the user clicks on the verification link there the "verified" on my database is supposed to change to 1.
However I am not being able to do so. I am using cpanels emails and I have sent an email through the webmail, so I know they are working. But my script for some reason isn't, I tried following the documentation though...
Here is the function.php:
function createUser($conn, $name, $email, $username, $pwd, $vkey){
$sql = "INSERT INTO users (usersName, usersEmail, usersUid, usersPwd, vkey) VALUES (?, ?, ?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql) ) {
header("location: index.html?error=stmtfailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "sssss", $name, $email, $username, $hashedPwd, $vkey);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../mainpage/index.html");
exit();
$to = $email;
$subject = "Verificação da conta";
$message = "
Thanks for signing up!
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.
------------------------
Username: '.$name.'
Password: '.$pwd.'
------------------------
Please click this link to activate your account:
https://universitymmt.com/includes/verify.php?vkey=$vkey'.$email.'&hash='.$pwd.'";
$headers = "De: suporte#universitymmt.com \r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" ."\r\n";
mail($to, $subject, $message, $headers);
header("location: index.html?accountverified");
}
This page is has the php that calls for the functions.php:
<?php
if(isset($_POST["submit"])){
$name = $_POST['name'];
$email = $_POST['email'];
$username = $_POST['uid'];
$pwd = $_POST['pwd'];
$pwdRepeat = $_POST['pwdrepeat'];
$vkey = md5(time() .$uid);
require_once 'db_connection.php';
require_once 'functions.php';
if (emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) !== false) {
header("location: ../index.html?error=emptyInput");
exit();
}
if (invalidUid($username) !== false) {
header("location: ../index.html?error=invalidUid");
exit();
}
if (invalidEmail($email) !== false) {
header("location: ../index.html?error=invalidEmail");
exit();
}
if (pwdMatch($pwd, $pwdRepeat) !== false) {
header("location: ../index.html?error=passwordsdontmatch");
exit();
}
if (uidExists($conn, $username, $email) !== false) {
header("location: ../index.html?error=usernameTaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd, $vkey);
$to = $email;
$subject = "Verificação da conta";
$message = "<a href='https://universitymmt.com/includes/verfiy.php?vkey=$vkey'> Confirmar a minha conta</a>";
$headers = "De: suporte#universitymmt.com \r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" ."\r\n";
mail($to, $subject, $message, $headers);
}
I may add, I added the mail function on both files, hoping one of them would work, which it didn't lol.
Thank you for any help guys.
I am making a database validation system. Upon validation, the handler is supposed to send the data to an email address, while simultaneously updating the data to another database. The values of id, name, board, query and rand are received from the previous page.
Now, with the given code, I am unable to update the table named 'answers' within my database. The line of code for this functionality is in bold.
<?php
$servername = "localhost";
$username = "root";
$password = '';
$dbname = "bsp";
$conn1 = new mysqli($servername, $username, $password, $dbname);
$id="";
$value = "";
$name = "";
$board = "";
$query="";
$id = $_POST['id'];
$name = $_POST['name'];
$board = $_POST['board'];
$query = $_POST['query'];
$rand = $_POST['rand'];
$value=$_POST['option'];
$to="";
$subject="";
$msg="";
$headers="";
if($value=='delete')
{
$sql="DELETE FROM rti WHERE ID=$id";
if ($conn1->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn1->error;
}
}
else if($value=='validate')
{
**$sql="INSERT INTO answers (name, board, query, rand) VALUES (\"$name\", \"$board\", \"$query\", \"$rand\")";**
if($board=='BRCA')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = "Your code is " . $rand . ".";
$msg .= " Visit localhost/BSP/landing.php and post the code, and subsequently your answer.";
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSW')
{
$to = 'email#rediffmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSA')
{
$to = 'email#iitd.ac.in';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSP')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BHM')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
}
if(mail($to, $subject, $msg, $headers)==TRUE)
{
echo "The mail was sent successfully.";
}
$conn1->close();
?>
Hi I am sending confirmation e-mail after signup.How can the link be expired after a few seconds can anyone suggest me.Because if i click on the link after few days also it is getting activated.That should not be happen.Here is my code:
<?php
session_start();
$sessionCaptcha = $_SESSION['vercode'];
$inputStream = file_get_contents("php://input");
$data = json_decode($inputStream);
$connection = mysql_connect("localhost", "enjoytax_account", "account") or die(mysql_error());
$db = mysql_select_db("enjoytax_accounting", $connection);
if($db)
{
$confirm_code=md5(uniqid(rand()));
$username = $data->username;
$email = $data->email;
$password = md5($data->password);
$confirmpassword = md5($data->confirmpassword);
$mobileno = $data->mobileno;
$captcha=$data->captcha;
$check=mysql_query("select email from register where email = '$email'");
$num_rows = mysql_num_rows($check);
if ($num_rows == 0)
{
if($captcha == $sessionCaptcha)
{
$query = mysql_query("insert into register(username,email, password, repassword,mobile,confirm_code) values ('$username','$email', '$password' , '$confirmpassword', '$mobileno','$confirm_code')");
if ($query)
{
$from .= 'info#mail.com' . "\r\n\r\n";
$to = $data->email;
$subject="Your confirmation link here";
$message.="Click on this link to activate your account \r\n";
$message.="http://www.example.com/model/confirmation.php?email=$email&passkey=$confirm_code";
$success = mail($to, $subject, $message);
$successJson='{"success":"We have sent a verification email ' .
'to your email id '.$email.', please check your ' .
'Inbox and verify your email in order to proceed further."}';
print_r($successJson);
}else{
$failureJson='{"error":"We are encountering some issue. Please try after some time."}';
print_r($failureJson);
}
}else{
$failureJson='{"error":"Please Enter Correct Captcha."}';
print_r($failureJson);
}
}else{
$failureJson='{"error":"Email-Id already Exists."}';
print_r($failureJson);
}
}
?>
I have not tested the code but this might help you.
session_start();
if ($query)
{
$from .= 'info#mail.com' . "\r\n\r\n";
$to = $data->email;
$subject="Your confirmation link here";
$message.="Click on this link to activate your account \r\n";
$message.="http://www.example.com/model/confirmation.php?email=$email&passkey=$confirm_code";
$success = mail($to, $subject, $message);
$successJson='{"success":"We have sent a verification email ' .
'to your email id '.$email.', please check your ' .
'Inbox and verify your email in order to proceed further."}';
$_SESSION['now'] = date('i:s');
$now =date('Y-m-d H:i:s');
$futureDate = $now+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
if($_SESSION['now'] > $formatDate)
{
$failureJson='{"error":"We are encountering some issue. Please try after some time."}';
print_r($failureJson);
}
}
else
{
echo " Query Not Executed";
}
I have a piece of code that will not work i don't understand why, i am trying to get the information sent to the database and also sent to the email address, it is saving it to the database but will not send to the email, even though it echoes your email was successfully sent. Any help appreciated.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$to = "edonaghy--#hotmail.co.uk";
$subject = "Dress fitting";
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$headers = "From: $email";
$sent = mail($to, $date, $headers);
if($sent)
{
print "Your mail was sent successfully";
}
else
{
print "We encountered an error sending your mail";
}
mysql_select_db("lr", $con);
mail("edonaghy--#hotmail.co.uk", $time, $date, $place, $comments);
$sql="INSERT INTO fitting (time, date, place, comments) VALUES ('$_POST[time]','$_POST[date]','$_POST[place]','$_POST[comments]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Information has been sent!";
?>
First of all, it wont send your e-mail if your localhost werent properly configured, so I suggest you to test on your server if you have one.
There are libraries that can help you with a better code like php-simple-mail
Example:
require 'class.simple_mail.php';
$mailer = new SimpleMail();
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$message = "<strong>My message on date:".$date." ".$time."</strong>";
$send = $mailer->setTo('edonaghy--#hotmail.co.uk', 'Your Email')
->setSubject('Test Message')
->setFrom('no-reply#domain.com', 'Domain.com')
->addMailHeader('Reply-To', 'no-reply#domain.com', 'Domain.com')
->addMailHeader('Cc', 'bill#example.com', 'Bill Gates')
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage($message)
->setWrap(100)
->send();
echo ($send) ? 'Email sent successfully' : 'Could not send email';
You're using mail wrong.
It's meant to be used like this:
mail(to,subject,message,headers,parameters)
So yours would be:
$to = "email#hotmail.co.uk";
$subject = "Dress fitting";
$headers = "From: email#hotmail.co.uk";
$message = "Time:".$_REQUEST['time']."\r\n";
$message .= "Date:".$_REQUEST['date']."\r\n";
mail($to,$subject,$message,$headers);
This is assuming your $_REQUEST's are working.
Your second mail() is using variables that you haven't even set yet ($comments, $place) etc.
Set them first:
$place = $_POST['place'];
$comments = $_POST['comments'];
I tried to a user registration and email verification using PHP, everything responded very well but a certain point after the user has submitted the registration form then the form is posted to verify.php then the script will send an activation code to the user's email.
The error is that the moment the activation mail is sent to the user's mail box, the page should display:
Thank you! An email has been sent to {Form.email}. To complete your registration, click on the email verification link sent to your email address.
Instead, it will automatically refresh the page and redirect the user to registrationcomplete.php page. which suppose to come after the user has verified.
I used the following code:
<?php
require ('Connections.php');
$activationkey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$name = mysql_real_escape_string($_POST['name']);
$country = mysql_real_escape_string($_POST['country']);
$state = mysql_real_escape_string($_POST['state']);
$add = mysql_real_escape_string($_POST['add']);
$phone = mysql_real_escape_string($_POST['phone']);
$email = mysql_real_escape_string($_POST['email']);
$userid = mysql_real_escape_string($_POST['userid']);
$password = mysql_real_escape_string($_POST['password']);
$lrname = mysql_real_escape_string($_POST['lrname']);
$lraccount = mysql_real_escape_string($_POST['lraccount']);
$wmz = mysql_real_escape_string($_POST['wmz']);
$form_submitt = $_POST['button'];
if ($form_submitt == true){
$sql = "INSERT INTO customers (`activationkey`, `name`, `country`, `state`, `add`, `phone`, `email`, `lrname`, `lraccount`, `comment`, `wmz`, `okpay`, `userid`, `password`, `status`) VALUES ('$activationkey', '$name', '$country', '$state', '$add', '$phone', '$email', '$lrname', '$lraccount', '', '$wmz', '', '$userid', '$password', 'verify');";
mysql_query($sql) or die(mysql_error());
##Send activation Email
$to = $_POST['email'];
$subject = "Complete registation";
$message = "Welcome to sitename!\r\rYou, or someone using your email address, has completed registration at www.sitename.com.\r\r You can complete registration by clicking the following link:\rhttp://www.sitename.com/verify.php?$activationkey \r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\r\r www.sitename.com Team";
$headers = 'From: noreply#sitename.com' . "\r\n" .
'Reply-To: noreply#sitename.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
<?php
##User isn't registering, check verify code and change activation code to null, status to activated on success
if(isset($_SERVER['QUERY_STRING'])){
$queryString = $_SERVER['QUERY_STRING'];
$query = "SELECT * FROM `DBName`.`customers`";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if ($queryString == $row['activationkey']){
$sql = "UPDATE `DBName`.`customers` SET `activationkey` = '', `status` = 'verified' WHERE `customers`.`id` = $row[id];";
mysql_query($sql) or die(mysql_error());
echo "<meta http-equiv='refresh' content='0;url=registrationcomplete.php'>";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
}
}
}
?>
Replace,
echo "<meta http-equiv='refresh' content='0;url=registrationcomplete.php'>";
to
header('location:verify.php?action=success');
exit();
Now using the action variable you can display the message
if(isset($_GET['action']) && $_GET['action'] == "success"){
echo 'Thank you message........';
}