是否可以在php中没有表单的情况下获得用户的电子邮件?我希望能够向某人发送邮件列表的电子邮件,而不是他们填写表格,他们在那里输入他们的电子邮件,我在电子邮件链接的php页面。他们点击链接,它会把他们送到网站上的一个感谢页面。然后它将他们的IP,日期/时间和电子邮件存储在我设置的数据库中。我已经用表格填好了,但我想知道有没有可能不用表格也可以填。这是我的表单代码,我使用Drupal表单API。我在谷歌上搜索了几个小时,但一无所获。我看到了一些关于URL参数的东西,但它与我的问题无关。任何帮助都是感激的。
<?php
//This custom module will be used on the website to gain consent from our clients because of the recent CASL anti spam laws that were passed in Cnada.
//Menu hook starts here, implements menu and sets the title, url of the page.
function form_casl_menu() {
$items = array();
$items['casl-consent/form'] = array( //this creates a URL that will call this form at "url"
'title' => 'CASL Subscription', //page title
'description' => 'A form that allows us to send emails to clients with their consent.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_casl_form'), //put the name of the form here
'access callback' => TRUE
);
return $items;
}
//permission hook
function form_casl_permission() {
return array(
'administer my module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
);
}
//form hook, form elements start here
function form_casl_form($form, &$form_state) {
//sometext here
$form['some_text'] = array(
'#markup' => '<p><b>Simply enter your email address to subscribe</b>
</p>'
);
$form['email'] = array(
'#type' => 'textfield', //their email
'#title' => 'Email:',
'#size' => 30,
'#maxlength' => 150,
'#required' => TRUE,
);
//submit button
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Submit Data'),
);
return $form;
}
//validate hook
function form_casl_form_validate($form, &$form_state) { //invalid email error
if (!valid_email_address($form_state['values']['email'])) {
form_set_error('mail', t('You must enter a valid e-mail address.'));
}
}
//submit hook
function form_casl_form_submit($form, $form_state) {
$sDate = date("Y-m-d H:i:s"); //returns the date and time
global $name;
$subbed = 'Yes';
//----------------------------------------------------------------''
$ip55 = &drupal_static(__FUNCTION__);
//ip get function, returns a clients IP address and checks if they're behind a proxy.
if (!isset($ip55)) {
$ip55 = $_SERVER['REMOTE_ADDR'];
if (variable_get('reverse_proxy', 0)) {
$reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');
if (!empty($_SERVER[$reverse_proxy_header])) {
// If an array of known reverse proxy IPs is provided, then trust
// the XFF header if request really comes from one of them.
$reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
// Turn XFF header into an array.
$forwarded = explode(',', $_SERVER[$reverse_proxy_header]);
// Trim the forwarded IPs; they may have been delimited by commas and spaces.
$forwarded = array_map('trim', $forwarded);
// Tack direct client IP onto end of forwarded array.
$forwarded[] = $ip55;
// Eliminate all trusted IPs.
$untrusted = array_diff($forwarded, $reverse_proxy_addresses);
// The right-most IP is the most specific we can trust.
$ip55 = array_pop($untrusted);
}
}
}
//-----------------------------------------------------------------''
//inserting data into database
db_insert('CASL')
->fields(array(
'email' => $form_state['values']['email'],//email
'ip' => $ip55,//ip
'substatus' => $subbed,
'datetime' => $sDate,//date and time
))->execute();
//sending confirmation email to the user, letting them know they can unsub at any time.
$values = $form_state['values'];
$to = $form_state['values']['email'];
$subject = 'Confirmation';
$message ="Thank you for your submission. You may unsubscribe at any time by refilling out this form http://www.localhost.ca/casl-consent/form and selecting the 'unsubscribe' option. Or, you can simply email us a request to unsubscribe, and we will remove you from our database immediately.
If you have any questions or concerns, you can email us at this link: http://www.localhost.ca/contact";
mail($to, $subject, $message);
drupal_set_message("Thank you! Your information has been received successfully and you have been sent a confirmation email.");
//thank you message after submission
}
?>
根据您的评论,听起来您正在尝试将已知的电子邮件地址与IP地址相关联。你说你正在发送一封电子邮件,他们正在点击一个链接。我假设每封电子邮件上的每个链接都是唯一生成的,并与特定地址相关联。在这种情况下,您的问题归结为"我如何找到客户端的IP地址?"
在PHP中,您可以使用$_SERVER['REMOTE_ADDR']
来完成此操作。这是最可靠的,但绝非万无一失。
"REMOTE_ADDR"用户正在查看当前页面的IP地址。
如果用户在代理之后,则可能已经设置了$_SERVER['HTTP_X_FORWARDED_FOR']
,但是该值可以被客户端和代理欺骗。它不能保证准确,不应该被信任。如果用户使用代理,那么REMOTE_ADDR
中的值将是访问您的web服务器的代理的IP地址,而不是客户端的IP地址。
那么,假设您要使用REMOTE_ADDR
,您如何将电子邮件地址与IP地址相关联?
用户单击您用已知电子邮件地址存储的唯一链接。该链接运行一个非常简单的脚本,查看REMOTE_ADDR
和HTTP_X_FORWARDED_FOR
,并存储这两个值。
您的表应该像这样简单:
email_address | unique_link | remote_addr | http_x_forwarded_for
最后两个值在用户单击unique_link
后填写。您最初如何填充email_address
仍然是您的秘密。