1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Having Users IP Address Showing In My Email Form

Discussion in 'Programming' started by paul_whiting09, Jun 14, 2015.

  1. #1
    Hello I have created an email form but am very unfamiliar with how to code in a section that displays a users IP address in the email that is sent. Here is my code,

    
    <?php
    // no direct accessdefined('_JEXEC')ordie('Restricted access');
    
    $myNameLabel = $params->get('name_label','Name:');
    $myEmailLabel = $params->get('email_label','Email:');
    
    $recipient = $params->get('email_recipient','');
    
    $buttonText = $params->get('button_text','Subscribe to Newsletter');
    $errorText = $params->get('errot_text','Your subscription could not be submitted. Please try again.');
    
    $subject = $params->get('subject','New subscription to your site!');
    $fromName = $params->get('from_name','Newsletter Subscriber');
    $fromEmail = $params->get('from_email','newsletter_subscriber@yoursite.com');
    $sendingWithSetEmail = $params->get('sending_from_set',true);
    
    $noName = $params->get('no_name','Please write your name');
    $noEmail = $params->get('no_email','Please write your email');
    $invalidEmail = $params->get('invalid_email','Please write a valid email');
    
    $nameWidth = $params->get('name_width','12');
    $emailWidth = $params->get('email_width','12');
    $buttonWidth = $params->get('button_width','100');
    
    $saveList = $params->get('save_list',true);
    $savePath = $params->get('save_path','mailing_list.txt');
    
    $mod_class_suffix = $params->get('moduleclass_sfx','');
    
    $addcss = $params->get('addcss','div.modns tr, div.modns td { border: none; padding: 3px; }');
    
    $thanksTextColor = $params->get('thank_text_color','#000000');
    $errorTextColor = $params->get('error_text_color','#000000');
    $pre_text = $params->get('pre_text','');
    
    $disable_https = $params->get('disable_https',true);
    
    $exact_url = $params->get('exact_url',true);if(!$exact_url){
    $url = JURI::current();}else{if(!$disable_https){
    $url =(!empty($_SERVER['HTTPS']))?"https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']:"http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];}else{
    $url ="http://newrealestate.properties/buy-now-related/newsletter-thankyou.html";}}
    
    $fixed_url = $params->get('fixed_url',true);if($fixed_url){
    $url = $params->get('fixed_url_address',"");}
    
    $url = htmlentities($url, ENT_COMPAT,"UTF-8");
    
    $unique_id = $params->get('unique_id',"");
    
    $enable_anti_spam = $params->get('enable_anti_spam',true);
    $myAntiSpamQuestion = $params->get('anti_spam_q','How many eyes has a typical person? (ex: 1)');
    $myAntiSpamAnswer = $params->get('anti_spam_a','2');
    
    $myError ="";
    $errors =3;
    
    if(isset($_POST["m_name".$unique_id])){
    $errors =0;if($enable_anti_spam){if($_POST["modns_anti_spam_answer".$unique_id]!= $myAntiSpamAnswer){
    $myError ='<span style="color: '.$errorTextColor.';">'.JText::_('Wrong anti-spam answer').'</span><br/>';}}if($_POST["m_name".$unique_id]===""){
    $myError = $myError .'<span style="color: '.$errorTextColor.';">'. $noName .'</span><br/>';
    $errors = $errors +1;}if($_POST["m_email".$unique_id]===""){
    $myError = $myError .'<span style="color: '.$errorTextColor.';">'. $noEmail .'</span><br/>';
    $errors = $errors +2;}
    
    if($myError ==""){
    
    $myMessage = $myNameLabel .' '. $_POST["m_name".$unique_id].', '.
    $myEmailLabel .' '. $_POST["m_email".$unique_id].', '.
    date("r");
    
    $mailSender =&JFactory::getMailer();
    $mailSender->addRecipient($recipient);if($sendingWithSetEmail){
    $mailSender->setSender(array($fromEmail,$fromName));}else{
    $mailSender->setSender(array($_POST["m_email".$unique_id],$_POST["m_name".$unique_id]));}
    
    $mailSender->setSubject($subject);
    $mailSender->setBody($myMessage);
    
    if(!$mailSender->Send()){
    $myReplacement ='<div class="modns"><span style="color: '.$errorTextColor.';">'. $errorText .'</span></div>';print $myReplacement;}else{
    $myReplacement ='<div class="modns"><span style="color: '.$thanksTextColor.';">'. $pageText .'</span></div>';print $myReplacement;}if($saveList){
    $file = fopen($savePath,"a");
    fwrite($file, utf8_encode($_POST["m_name".$unique_id]." (".$_POST["m_email".$unique_id]."); "));
    fclose($file);}returntrue;}}
    
    if($recipient ===""){
    $myReplacement ='<div class="modns"><span style="color: '.$errorTextColor.';">No recipient specified</span></div>';print $myReplacement;returntrue;}
    
    if($recipient ==="email@email.com"){
    $myReplacement ='<div class="modns"><span style="color: '.$errorTextColor.';">Mail Recipient is specified as email@email.com.<br/>Please change it from the Module parameters.</span></div>';print $myReplacement;returntrue;}
    
    if($myError !=""){print $myError;}
    
    print'<style type="text/css"><!--'. $addcss .'--></style>';print'<div class="modns"><form action="'. $url .'" method="post">'."\n".'<div class="modnsintro">'.$pre_text.'</div>'."\n";print'<table>';if($enable_anti_spam){print'<tr><td colspan="2">'. $myAntiSpamQuestion .'</td></tr><tr><td></td><td><input class="modns inputbox '. $mod_class_suffix .'" type="text" name="modns_anti_spam_answer'.$unique_id.'" size="'. $nameWidth .'"/></td></tr>'."\n";}print'<tr><td>'. $myNameLabel .'</td><td><input class="modns inputbox '. $mod_class_suffix .'" required = "true" type="text" name="m_name'.$unique_id.'" size="'. $nameWidth .'"';if(($errors &1)!=1){print' value="'.htmlentities($_POST["m_name".$unique_id], ENT_COMPAT,"UTF-8").'"';}print'/></td></tr>'."\n";print'<tr><td>'. $myEmailLabel .'</td><td><input class="modns inputbox '. $mod_class_suffix .'" required = "true" type="text" name="m_email'.$unique_id.'" size="'. $emailWidth .'"';if(($errors &2)!=2){print' value="'.htmlentities($_POST["m_email".$unique_id], ENT_COMPAT,"UTF-8").'"';}print'/></td></tr>'."\n";print'<tr><td colspan="2"><input class="modns button '. $mod_class_suffix .'" type="image" src="images/subscribe3.png" /></td></tr></table></form></div>'."\n";returntrue;
    
    Code (markup):
    Any help with this would be greatly appreciated have been stuck on it for a few hours. Cheers.
     
    paul_whiting09, Jun 14, 2015 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    This looks very complicated for what you are doing but you could solve it like this.

    
    //Get the ip
    $ip = "";
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip = $_SERVER['HTTP_CLIENT_IP'];} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];}else{
    $ip = $_SERVER['REMOTE_ADDR'];}
    
    //So after this part
    $myMessage = $myNameLabel .' '. $_POST["m_name".$unique_id].', '.
    $myEmailLabel .' '. $_POST["m_email".$unique_id].', '.
    date("r");
    
    //Add this
    $myMessage .= " IP:".$ip;
    
    PHP:
     
    Anveto, Jun 14, 2015 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    I'd simplify that down to a single return:

    function getIp() {
    	return empty($_SERVER['HTTP_CLIENT_IP']) ? (
    		empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? (
    			empty($_SERVER['REMOTE_ADDR']) ? 
    			'Unable to resolve IP Address' :
    			$_SERVER['REMOTE_ADDR']
    		) : $_SERVER['HTTP_X_FORWARDED_FOR']
    	) : $_SERVER['HTTP_CLIENT_IP'];
    }
    Code (markup):
    Though shouldn't both isset and empty be being checked?

    function serverVal($index) {
    	return (
    		isset($_SERVER[$index]) &&
    		!empty($_SERVER[$index])
    	) ?	$_SERVER[$index] : false;
    }
    
    function getIp() {
    	return
    		serverVal('HTTP_CLIENT_IP') ||
    		serverVal('HTTP_X_FORWARDED_FOR']) ||
    		serverVal('REMOTE_ADDR']) ||
    		'Unable to resolve IP Address';
    }
    Code (markup):
    Otherwise the error log is gonna be filled up like an outhouse at a chili cookoff.
     
    deathshadow, Jun 17, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Oh, and if you are uncomfortable with inline evaluations at least learn what return actually DOES and stop wasting memory on variables FOR NOTHING.

    function getIp() {
    
    		if (
    			isset($_SERVER['HTTP_CLIENT_IP') &&
    			!empty($_SERVER['HTTP_CLIENT_IP'])
    		)	return $_SERVER['HTTP_CLIENT_IP'];
    		
    		if (
    			isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
    			!empty($_SERVER['HTTP_X_FORWARDED_FOR'])
    		) return $_SERVER['HTTP_X_FORWARDED_FOR'];
    		
    		if (
    			isset($_SERVER['REMOTE_ADDR']) &&
    			!empty($_SERVER['REMOTE_ADDR'])
    		) return $_SERVER['REMOTE_ADDR'];
    		
    		return 'Unable to resolve user IP address';
    		
    }
    Code (markup):
    Seriously I'm seeing more and more code where people are throwing in extra variables for no good reason other than wasting memory.
     
    deathshadow, Jun 17, 2015 IP