I have a php code for a sign up form, it stores the details to a table(temp_members_db), then sends a confirmation code to the email entered. If the code is clicked, the details from temp_members_db is transferred to a new table registered_members, from which login details are checked if a user logs in.
The problem is related to the signup code, how can I avoid duplication of emails and username?
My code is:
<?php
include('config.php');
$tbl_name=temp_members_db;
$confirm_code=md5(uniqid(rand()));
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$password_confirm=$_POST['password_confirm'];
$sql="INSERT INTO $tbl_name(confirm_code, name, email, password)VALUES('$confirm_code', '$name', '$email', '$password')";
$result=mysql_query($sql);
if
($password != $password_confirm)
{
echo "Password do not match";
}
else if
($result)
{
$to=$email;
$subject="Your confirmation link here";
$header="from: Name <confirm#domain.org>";
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="http://mydomain#domain.org/confirmation.php?passkey=$confirm_code";
$sentmail = mail($to,$subject,$message,$header);
}
else {
echo "Email not found";
}
if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "<BR/>Cannot send Confirmation link to your e-mail address";
}
?>
Thanks :D
Run a SQL to determine if the name or email already exists in any table before INSERT into the database.
You can display error message if the query returns >0.
For example,
SELECT COUNT(*) FROM temp_members_db, registered_members WHERE email = $email OR name = $name;
Related
What I'm looking for exactly is a button that says something like, "Report a typo," and notifies me when somebody clicks that button on that page.
To clarify, I've got a big writing project with many short pages -- a couple paragraphs in an html template each, which I know could be done much more concisely, but that's another matter. It would help me if, when reading through, I could just click a button every time I (or whoever is reading) see something screwy, then get a message saying on which page the button was clicked without having to send an email. I'm fine with an automatic message to my email (even just as simple as the page name, like 'example.html' in the body), but I don't want to have to open my email client, etc. Honestly, any way I can get notification is fine (can html5 send carrier pigeons?), as long as the user interaction remains minimal. Is there a relatively simple way to do this? I'm happy to learn, but not exactly an expert as it stands.
I apologize in advance if I was unclear, if this has been asked before (I couldn't really think of how to search for what I'm asking), or if the answer is ridiculously simple and I'm just completely missing it. I'm kind of a noob at all this, so I appreciate any patience if this isn't an ideal question, but I'll try to update with suggested fixes. Thanks!
if ($this->form_validation->run() == FALSE) {
$this->load->view('welcome_message');
} else {
$this->load->view('formsuccess');
$name = $this->input->post('name');
$email = $this->input->post('email');
$cell = $this->input->post('cell');
$msg = $this->input->post('msg');
// $captcha=$this->input->post('captcha');
// echo $this->session->flashdata('email_sent');
$data = array('name' => $name, 'email' => $email, 'cell' => $cell, 'msg' => $msg);
$this->load->model('mymodel');
if ($this->mymodel->add($data)) {
$this->load->library('email');
//SMTP & mail configuration
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'consecutive.development#gmail.com',
'smtp_pass' => 'devAccountCB!',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
//Email content
$subject = 'Call Back Request';
$message = $this->input->post('msg');
$name = $this->input->post('name');
$from = $this->input->post('email');
$to = 'consecutive.development#gmail.com';
$this->email->from($from, $name);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message("From : " . $from . " <br> Message: " . $message . "<br>Phone Number:" . $cell);
if ($this->email->send()) {
$this->mymodel->add($data);
$this->session->set_flashdata('success_msg', 'Message sent successfully');
//$success= $this->session->set_flashdata('success_msg');
} else {
$this->session->set_flashdata('error_msg', 'Error sending message!');
//redirect('Welcome/index');
// show_error($this->email->print_debugger());
}
echo '<script>alert("Your Message has been sent. Thankyou!");</script>';
redirect('');
It is impossible to do that in Html5 , however it can be done using Jquery or php, as you may need some backend site scripting language.
test.html
<!DOCTYPE html>
<html>
<head>
<title>Database test</title>
<link rel="stylesheet" type="text/css" href="test.css">
<script>
function validateForm(n,mes)
{
valid=true;
var x=n.value;
var errn="error_"+n.id;
var email = document.getElementById('EMAIL').value;
var filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
//alert(errn);
if (x==null || x=="" || x.trim()=="")
{
document.getElementById(errn).innerHTML=mes;
valid=false;
}
else
{
document.getElementById(errn).innerHTML="";
if(n.id == 'EMAIL')
{
if (!filter.test(email)) {
document.getElementById(errn).innerHTML="Please Provide a valid email address";
email.focus();
valid=false;
} else {
document.getElementById(errn).innerHTML="";
valid=true;
}
}
}
return valid;
}
</script>
</head>
<body>
<h1>Just a Database test</h1>
<form name="SignUp" action="http://127.0.0.1/cgi-bin/connectivity.cgi" onsubmit="return validateForm();" method="post">
Name :<input id="NAME" type="text" name="name" onblur="validateForm(this,'Name must be filled out');"> <p id="error_NAME"></p>
Email :<input id="EMAIL" type="text" name="email" onblur="validateForm(this,'Email must be filled out');"> <p id="error_EMAIL"></p>
<input type="submit" value="Send">
</form>
</body>
</html>
connectivity.cgi
#!C:/usr/bin/perl -w
use CGI;
use strict;
use DBI();
use CGI::Carp qw(fatalsToBrowser);
print "content-type: text/html; charset=iso-8859-1\n\n";
my $q=new CGI;
my $name=$q->param('name');
my $email=$q->param('email');
print $q->header;
#connect to database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","root","mukesh",
{'RaiseError' => 1});
eval {$dbh->do("CREATE TABLE IF NOT EXISTS emp (name VARCHAR(20), email VARCHAR(50) UNIQUE NOT NULL)")};
print "creating table emp failed: $#" if $#;
print "<br>";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM emp WHERE email = ?");
$sth->execute($email);
my $rows = $sth->fetchrow_arrayref();
$sth->finish();
if (!$rows) {
$sth=$dbh->prepare_cached(<<SQL);
INSERT INTO emp(name,email) values(?,?)
SQL
$sth->execute($name,$email);
$sth->finish();
} else {
print "<h4>$email already exists, please enter a new email address<br></h4>";
exit 1;
}
my $sql = qq/select * from emp order by name/;
$sth = $dbh->prepare($sql) or die "Can't prepare $sql:$dbh->errstrn";
#pass sql query to database handle
my $rv = $sth->execute() or die "can't execute the query: $sth->errstrn";
while (my #row = $sth->fetchrow_array) {
print join(", ",#row),"<br>";
}
$sth->finish();
$dbh->disconnect();
print "<br>Total no of records : $rv";
if ($rv>=1){
print "<br>Record has been successfully updated !!!<br>";
}else{
print "<br>Error!!while inserting record<br>";
exit;
}
my problem is that after quring the mysql db i want the output to be displayed on the same page as the form is
but when i hit the send button it takes me to a new page and the output/error is displayed like this.
or
I want the email already existing error to come below the Email box and the output to come
below the send button on the same page as below.
Do i need to include some other language in my code for this like ajax or php.
i am not sure how to do this as i am being redirected to another page where the details
are displayed.
You have to understand that:
-The page with the form is a HTML page which displays on its own.
-The activity.cgi is a seperate page - which also displays on its own. The webserver just happens to exceute it and display the output.
One easy way to do what you want is to simply get rid of the HTML file, and output the HTML from the CGI. In perl you can do something like:
print "<html> (all your html here down to the end of the form) ";
if (!defined $q->param('name'))
{
# No parameters were passed - user has not submitted the form.
print "Please fill in the form and press send.";
}
else
{
# The parameter exists - so the user has submitted the form
# Go on with your code to do the database work here and output the results
}
print "end html code here </html>";
So what happens is that script executes one to show the page, then it executes again when the submit is pressed - only that time it shows the page AND shows the database output.
You could also do what you want with ajax. In that you can make the form submit button perform a javascript xmlhttprequest to your current cgi page and insert the results into your page using innerHTML of a div.
Good Day,
am Facing a code error with my Admin control panel PHP page.
I want to prevent any other user from accessign this page unless his job_title= admin.
it works but even the admin himslef redirects back to login page again !!
here is the code
<?php
include('db.php');
?>
<?php
// Inialize session
#session_start();
ob_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['name']) || ($_SESSION['job_title'] != "admin")) {
header('Location: index.php');
}
ob_flush();
?>
>?
<!-- STARTING HTML FORMAT !-->
?>
Any help ?
Try this, if still problems, let me know.
$_SESSION['name'] = 'dennis';
$_SESSION['job_title'] = 'admin';
if (!isset($_SESSION['name']) || (!isset($_SESSION['job_title']) ? false: ($_SESSION['job_title'] !== 'admin'))) {
echo 'Redirecting';
} else {
echo 'You\'re good! Not redirecting!';
}
This may be an easier way to understand, just put into a function.
$_SESSION['name'] = 'dennis';
$_SESSION['job_title'] = 'admin';
if (!isset($_SESSION['name']) || !isBoss()) {
echo 'Redirecting';
} else {
echo 'You\'re good! Not redirecting!';
}
function isBoss() {
if (isset($_SESSION['job_title']))
if ($_SESSION['job_title'] === 'admin')
return true;
return false;
}
I think your problem is a fubar newline :) Have added login page reference, which is unsetting the session variables you check for if logins fail.
Be very careful with where you put your <?php ?> tags - and in .inc files, such as db.php include - you may leave out the closing ?> tag on last line to avoid accidental ENTER, then CTRL + S failures, sneaking in an unwanted newline char in your output buffer (ob). IF ob_start is activated, nothing is written from server before you choose or script ends. ELSE if its not, default is that every \n will flush output and start the Content part of the payload.
login.php:
<?php
session_start(); // put this on top-most line in your script
$ok = check($_POST['user'], $_POST['pass']);
if($ok) {
$user = db_get_user_creds($_POST['user']);
$_SESSION['name'] = $user['name'];
$_SESSION['job_title'] = $user['job_title'];
} else {
// session_unset();
unset($_SESSION['name']);
unset($_SESSION['job_title']);
}
?>
admin.php
<?php
session_start(); // put this on top-most line in your script
// or, use ob_start at the very first line
// (with no widespace what so ever written out before it)
include('db.php');
?> I am writing out a newline here, session / header section is going to become unstable
<?php
// Inialize session
// #session_start(); moved up top
ob_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['name']) || ($_SESSION['job_title'] != "admin")) {
header('Location: index.php');
}
ob_flush();
?>
See the 'I am writing out newline here' bit
A little deeper down rabbithole goes; the communication flow is like this:
1) HEADERS such as
Connection: keep-alive\r\n
Content-Type: text/html\r\n (etc)
2) DOUBLE NEWLINE (one newline with no previous chars on that line)
\r\n
3) CONTENTS
Body
Of Page
I hope I am in the right place!!
http://www.phpbb.com/kb/article/phpbb3-cross-site-sessions-integration/
http://www.phpbb.com/kb/article/phpbb3-sessions-integration/
What I am trying to do is integrate the phpbb forum with my existing site. I have already looked at the links above, and it doesn't seem to work. I have copied this code
define('IN_PHPBB', true);
define('ROOT_PATH', "/path/to/forums");
if (!defined('IN_PHPBB') || !defined('ROOT_PATH')) {
exit();
}
$phpEx = "php";
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : ROOT_PATH . '/';
include($phpbb_root_path . 'common.' . $phpEx);
$user->session_begin();
$auth->acl($user->data);
into a loginforum.php file, which I include in every page I want the sessions to be kept. I have done the three steps indicated in the sessions integration section, but when I try to check whether the user is authenticated, it doesn't seem so. Using the same code here:
<?php
if ($user->data['user_id'] == ANONYMOUS){
echo 'Please login!';
}
else{
echo 'Thanks for logging in, ' . $user->data['username_clean'];
}
?>
I only get the "Please login" phrase, even when I login.
I've been over this for hours, I don't understand where the problem is. Shouldn't it work after the three miraculous steps?? :(
I would be thankful to anyone who would try to help!
Cheers,
Den
This appears to be a duplicate of this question
However, try this answer:
if ($user->data['username'] == 'Anonymous')
{
echo 'Please login!';
}
I'm trying to send a thank you email to the user submitting the form in HTML.
I found out by using a hook in my template.php file like this works to set the header correctly:
function mythemename_webform_mail_headers($form_values, $node, $sid) {
$headers = array(
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')',
);
return $headers;
}
This works freat for the "Thank You" email.
The email that the site admin gets with the form results is also html, but it's not converting newlines to breaks in this email. I can't figure out how to use a hook for this, so I had to edit the webform.module file and do this:
function webform_mail($key, &$message, $params) {
$message['headers'] = array_merge($message['headers'], $params['headers']);
$message['subject'] = $params['subject'];
//$message['body'][] = drupal_wrap_mail($params['message']); // replaced this with line below
$message['body'][] = nl2br(drupal_wrap_mail($params['message']));
}
Can this be done with a hook in template.php?
You can use hook_mail_alter to edit mails created with hook_mail, which is what webform uses.
You can't use hook_mail_alter() in a theme, only in a custom module.
Old topic but still usefull I guess. On the webform module's edit page there is a option/fieldset with additional processing:
<?php
$to = $form_values['submitted_tree']['uw_gegevens']['e_mail'];
$from = "no-reply#example.com";
$achternaam = $form_values['submitted_tree']['uw_gegevens']['uw_naam'];
$message = drupal_mail('webform_extra', 'reply', $to, language_default(), array('body' => $body), $from, TRUE);
function webform_extra_mail($key, &$message, $params) {
$message['subject'] = "TEXT.";
$message['body'] = "
TEXT, " . $params['achternaam'] . "
TEXT.
KIND REGARDS,
TEXT
";
} ?>
Hope this helps
Guus van de Wal