deadline Posted November 10, 2009 Share Posted November 10, 2009 Hello all, I am a newbie to php and I am trying to get something that I am sure is simple to work. Think that I am too close to the fire or just simply over thinking the solution. K here is the problem that I am needing help with. I have a web form with java that I need to capture the ip address of the person who is filling out the form. I have scoured the web and think that I am simply missing what is right in front of my eyes. Here are the scripts that I am working with. PHP part: <?php if ( !isset($_SERVER['SPI'])) { die(); } if (!isset($_SERVER['DOCUMENT_ROOT'])) { echo("CRITICAL: we seem to be running outside of the norm.\n"); header("Location: http://".$_SERVER["HTTP_HOST"]."/"); die("CRITICAL: Document root unavailable.\n"); } $request_method = $_SERVER["REQUEST_METHOD"]; if($request_method == "GET") { $query_vars = $_GET; } elseif ($request_method == "POST") { $query_vars = $_POST; } reset($query_vars); function customsort($a,$b) { // $a is array for form vars, $b is comma seperated case sensitive field order // this is case sensitive -- good idea to hrc that. $data = array(); if ( strstr($b,',') == FALSE ) { $b = $b.","; } $ordering = split(',',$b); foreach ($ordering as $orderitem) { if ( ($orderitem != null) && ($orderitem != "") ) { if (isset($a[$orderitem])) { $data[$orderitem] = $a[$orderitem]; } } } foreach ($a as $key=>$val) { $data[$key] = $a[$key]; } return $data; } function xmlentities($string) { return str_replace ( array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $string); } $t = date("U"); $logged_string = $_SERVER['REMOTE_ADDR'] . "|" . date("j M Y g:i a"); $file = fopen("userIP.log", "a"); fputs($file, $logged_string, strlen($logged_string)); fclose($file); $formhomedir = preg_replace('/.*\/home\/content/','',$_SERVER['DOCUMENT_ROOT']); $formhomedir = explode('/',$formhomedir); $formhome="/home/content".$formhomedir[0]."/".$formhomedir[1]."/".$formhomedir[2]."/".$formhomedir[3]."/".$formhomedir[4]."/data/"; $file_order = ".default"; $file_format = ".text"; $file_interval = ".15m"; $field_order = ""; if (isset($query_vars['form_order'])) { if ($query_vars['form_order'] != "alpha") { $field_order=$query_vars['form_order']; $file_order=".custom"; $query_vars = customsort($query_vars,$field_order); } else { switch ($query_vars['form_order']) { case "alpha": uksort($query_vars,'strnatcasecmp'); $file_order=".alpha"; break; default: $file_order=".default"; break; } } } if (isset($query_vars['form_format'])) { switch ($query_vars['form_format']) { case "csv": $file_format = ".csv"; break; case "html": $file_format = ".html"; break; case "xml": $file_format = ".xml"; break; case "text": case "default": default: $file_format = ".text"; break; } } if (isset($query_vars['form_delivery'])) { switch ($query_vars['form_delivery']) { case "hourly": $file_interval = ".60m"; break; case "hourly_digest": $file_interval = ".60mc"; break; case "daily": $file_interval = ".24h"; break; case "daily_digest": $file_interval = ".24hc"; break; case "digest": $file_interval = ".15mc"; break; case "default": default: $file_interval = ".15m"; break; } } $file = $formhome."form_".$t.$file_order.$file_format.$file_interval; $fp = fopen($file,"w"); switch ($file_format) { case ".csv": $csvkeys = ""; $csvvals= ""; $firsttime = ""; while (list ($key, $val) = each ($query_vars)) { if ( ($key == "form_order") || ($key == "form_format") || ($key == "form_delivery") || ($key == "redirect") ) { } else { if ($csvkeys != "") { $firsttime=","; } $tmpkey=escapeshellcmd($key); $csvkeys = $csvkeys.$firsttime."'".$tmpkey."'"; $tmpval=escapeshellcmd($val); $csvvals = $csvvals.$firsttime."'".$tmpval."'"; } } fputs($fp,"$csvkeys\n"); fputs($fp,"$csvvals\n"); break; case ".html": fputs($fp,"<table border=\"1\" cellspacing=\"1\" cellpadding=\"2\">\n"); break; case ".xml": fputs($fp,"<form>\n"); break; } while (list ($key, $val) = each ($query_vars)) { if ($key == "redirect") { $landing_page = $val; } if ( ($key == "form_order") || ($key == "form_format") || ($key == "form_delivery") || ($key == "redirect") ) { } else { switch ($file_format) { case ".html": fputs($fp,"\t<tr>\n"); fputs($fp,"\t\t<td><b>$key</b></td>\n"); fputs($fp,"\t\t<td>$val</td>\n"); fputs($fp,"\t</tr>\n"); break; case ".csv": // content is already output break; case ".xml": fputs($fp,"\t<field>\n"); fputs($fp,"\t\t<fieldname>".xmlentities($key)."</fieldname>\n"); fputs($fp,"\t\t<fieldvalue>".xmlentities($val)."</fieldvalue>\n"); fputs($fp,"\t</field>\n"); break; case ".text": default: fputs($fp,$key.": ".$val."\n"); break; } } } switch ($file_format) { case ".html": fputs($fp,"</table>\n"); break; case ".xml": fputs($fp,"</form>\n"); break; } fclose($fp); if ($landing_page != "") { header("Location: http://www.cssc-claims.com/claimtrackerpro/ctp-main.htm" ); } else { header("Location: http://www.cssc-claims.com/claimtrackerpro/ctp-main.htm" ) ; } ?> Javascript: <script type="text/javascript"> <!-- var i = 0; function enable_submit(obj, domain) { var sb = document.getElementById("Submit"); var dom = document.getElementById('Domain' + domain); if(obj.checked === true){ sb.disabled = false; i++; } else{ if(i > 0){ i--; } if(i == 0){ sb.disabled = true; } } } function display_privacy(domain) { var sb = document.getElementById("Submit"); var privacy_label = document.getElementById('privacy_' + domain); var privacy_cb = document.getElementById('Privacy' + domain); var dom = document.getElementById('Domain' + domain); if(dom.checked === true){ privacy_label.style.visibility = 'visible'; } else{ privacy_cb.checked = false; privacy_label.style.visibility = 'hidden'; if(i > 0){ i--; } if(i == 0){ sb.disabled = true; } } } //--> </script> Form Coding: <table width="86%" border="0" align="center" cellpadding="6"> <tr> <td align="center"><strong>Click "I Agree" if you agree to the above terms.</strong><br /> <span class="copyright-txt"> <script> var ip= '<!--#echo var="REMOTE_ADDR"-->'; document.write("Your IP Address of: "+ip+" is being recorded for security purposes.");</script> </span></td> </tr> <tr> <td align="center"><form method="post" action="http://www.cssc-claims.com/webformmailer-terms.php" name="enableform" id="Form1"> <label>Enter Your Name: <input type="text" name="Name" id="Name" /> </label> <input type="checkbox" id="Domain1" name="Domain1" value="Agree" onclick="display_privacy(1)" /> I Agree <label id="privacy_1" style="visibility:hidden;"><br /> <input type="checkbox" id="Privacy1" name="Privacy1" value="Verify" onclick="enable_submit(this, 1)" /> <strong>Verify</strong> • <span class="grey-body">When prompted enter "user" for user name and "password" for password</span></label> <br /> <input type="hidden" name="ip_address" value="<?=$_SERVER['REMOTE_ADDR']?>" /> <input type="submit" name="Submit" id="Submit" value="Enter Portal" disabled="disabled" onclick="window.open('https://www.cssc-claims.com/sample-attorney/','mywindow')"/> </form></td> </tr> </table> Sorry for the long post but any help would be great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/ Share on other sites More sharing options...
mikesta707 Posted November 10, 2009 Share Posted November 10, 2009 if you want the IP address of the client just use $ip = $_SERVER['REMOTE_ADDR']; Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-954578 Share on other sites More sharing options...
deadline Posted November 10, 2009 Author Share Posted November 10, 2009 So just add the $ip = $_SERVER['REMOTE_ADDR']; to the php part. What do i need to do the call it in the form? Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-954581 Share on other sites More sharing options...
deadline Posted November 10, 2009 Author Share Posted November 10, 2009 Also I need to remove this correct: $logged_string = $_SERVER['REMOTE_ADDR'] . "|" . date("j M Y g:i a"); $file = fopen("userIP.log", "a"); fputs($file, $logged_string, strlen($logged_string)); fclose($file); Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-954584 Share on other sites More sharing options...
Dorky Posted November 10, 2009 Share Posted November 10, 2009 $ip = $_SERVER['REMOTE_ADDR']; <input type='hidden' name='whatever' value='$ip' /> Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-954623 Share on other sites More sharing options...
deadline Posted November 11, 2009 Author Share Posted November 11, 2009 thanks will try that and post results, thanks for the replies Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-955711 Share on other sites More sharing options...
deadline Posted November 13, 2009 Author Share Posted November 13, 2009 K tried that, just getting $ip in email from form. Can you tell me where I need to place the $ip line in the php script? Thanks. Form email message: Name: ip 23 Domain1: Agree Privacy1: Verify User_IP: $ip Submit: Enter Portal this is what I have now. PHP <?php if ( !isset($_SERVER['SPI'])) { die(); } if (!isset($_SERVER['DOCUMENT_ROOT'])) { echo("CRITICAL: we seem to be running outside of the norm.\n"); header("Location: http://".$_SERVER["HTTP_HOST"]."/"); die("CRITICAL: Document root unavailable.\n"); } $request_method = $_SERVER["REQUEST_METHOD"]; if($request_method == "GET") { $query_vars = $_GET; } elseif ($request_method == "POST") { $query_vars = $_POST; } reset($query_vars); function customsort($a,$b) { // $a is array for form vars, $b is comma seperated case sensitive field order // this is case sensitive -- good idea to hrc that. $data = array(); if ( strstr($b,',') == FALSE ) { $b = $b.","; } $ordering = split(',',$b); foreach ($ordering as $orderitem) { if ( ($orderitem != null) && ($orderitem != "") ) { if (isset($a[$orderitem])) { $data[$orderitem] = $a[$orderitem]; } } } foreach ($a as $key=>$val) { $data[$key] = $a[$key]; } return $data; } function xmlentities($string) { return str_replace ( array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $string); } $t = date("U"); $formhomedir = preg_replace('/.*\/home\/content/','',$_SERVER['DOCUMENT_ROOT']); $formhomedir = explode('/',$formhomedir); $formhome="/home/content".$formhomedir[0]."/".$formhomedir[1]."/".$formhomedir[2]."/".$formhomedir[3]."/".$formhomedir[4]."/data/"; $file_order = ".default"; $file_format = ".text"; $file_interval = ".15m"; $field_order = ""; if (isset($query_vars['form_order'])) { if ($query_vars['form_order'] != "alpha") { $field_order=$query_vars['form_order']; $file_order=".custom"; $query_vars = customsort($query_vars,$field_order); $ip = $_SERVER['REMOTE_ADDR']; } else { switch ($query_vars['form_order']) { case "alpha": uksort($query_vars,'strnatcasecmp'); $file_order=".alpha"; break; default: $file_order=".default"; break; } } } if (isset($query_vars['form_format'])) { switch ($query_vars['form_format']) { case "csv": $file_format = ".csv"; break; case "html": $file_format = ".html"; break; case "xml": $file_format = ".xml"; break; case "text": case "default": default: $file_format = ".text"; break; } } if (isset($query_vars['form_delivery'])) { switch ($query_vars['form_delivery']) { case "hourly": $file_interval = ".60m"; break; case "hourly_digest": $file_interval = ".60mc"; break; case "daily": $file_interval = ".24h"; break; case "daily_digest": $file_interval = ".24hc"; break; case "digest": $file_interval = ".15mc"; break; case "default": default: $file_interval = ".15m"; break; } } $file = $formhome."form_".$t.$file_order.$file_format.$file_interval; $fp = fopen($file,"w"); switch ($file_format) { case ".csv": $csvkeys = ""; $csvvals= ""; $firsttime = ""; while (list ($key, $val) = each ($query_vars)) { if ( ($key == "form_order") || ($key == "form_format") || ($key == "form_delivery") || ($key == "redirect") ) { } else { if ($csvkeys != "") { $firsttime=","; } $tmpkey=escapeshellcmd($key); $csvkeys = $csvkeys.$firsttime."'".$tmpkey."'"; $tmpval=escapeshellcmd($val); $csvvals = $csvvals.$firsttime."'".$tmpval."'"; } } fputs($fp,"$csvkeys\n"); fputs($fp,"$csvvals\n"); break; case ".html": fputs($fp,"<table border=\"1\" cellspacing=\"1\" cellpadding=\"2\">\n"); break; case ".xml": fputs($fp,"<form>\n"); break; } while (list ($key, $val) = each ($query_vars)) { if ($key == "redirect") { $landing_page = $val; } if ( ($key == "form_order") || ($key == "form_format") || ($key == "form_delivery") || ($key == "redirect") ) { } else { switch ($file_format) { case ".html": fputs($fp,"\t<tr>\n"); fputs($fp,"\t\t<td><b>$key</b></td>\n"); fputs($fp,"\t\t<td>$val</td>\n"); fputs($fp,"\t</tr>\n"); break; case ".csv": // content is already output break; case ".xml": fputs($fp,"\t<field>\n"); fputs($fp,"\t\t<fieldname>".xmlentities($key)."</fieldname>\n"); fputs($fp,"\t\t<fieldvalue>".xmlentities($val)."</fieldvalue>\n"); fputs($fp,"\t</field>\n"); break; case ".text": default: fputs($fp,$key.": ".$val."\n"); break; } } } switch ($file_format) { case ".html": fputs($fp,"</table>\n"); break; case ".xml": fputs($fp,"</form>\n"); break; } fclose($fp); if ($landing_page != "") { header("Location: http://www.claimtrackerpro.com/aboutus.html" ); } else { header("Location: http://www.claimtrackerpro.com/aboutus.html" ) ; } ?> Form: <form method="post" action="http://www.cssc-claims.com/webformmailer-terms.php" name="enableform" id="Form1"> <label>Enter Your Name: <input type="text" name="Name" id="Name" /> </label> <input type="checkbox" id="Domain1" name="Domain1" value="Agree" onclick="display_privacy(1)" /> I Agree <label id="privacy_1" style="visibility:hidden;"><br /> <input type="checkbox" id="Privacy1" name="Privacy1" value="Verify" onclick="enable_submit(this, 1)" /> <strong>Verify</strong> • <span class="grey-body">When prompted enter "user" for user name and "password" for password</span></label><br /><input type='hidden' name='User IP' value='$ip' /> <input type="submit" name="Submit" id="Submit" value="Enter Portal" disabled="disabled" onclick="window.open('http://www.claimtrackerpro.com/sample-attorney/','mywindow')"/> </form> Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956538 Share on other sites More sharing options...
gerkintrigg Posted November 13, 2009 Share Posted November 13, 2009 try value='<?php echo $ip;?>' Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956540 Share on other sites More sharing options...
mrMarcus Posted November 13, 2009 Share Posted November 13, 2009 i don't see your mail() function anywhere. and please don't post ALL your code again, it's too much to handle. but please explain very explicitly, what it is you're trying to do, ie: "i would like to have a form where when submitted, the information from that form is then emailed to a specified email account." Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956544 Share on other sites More sharing options...
deadline Posted November 13, 2009 Author Share Posted November 13, 2009 trying that now, and sorry for posting entire code. Will remember that next time Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956559 Share on other sites More sharing options...
deadline Posted November 13, 2009 Author Share Posted November 13, 2009 no dice this is what I get User_IP: <?php echo $ip;?> Think that I have the $ip = $_SERVER['REMOTE_ADDR']; coding in the wrong place. Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956569 Share on other sites More sharing options...
mikesta707 Posted November 13, 2009 Share Posted November 13, 2009 alright, well im not gonna trod through all your mass of code, so post the relevant code in code or php tags so I can take a look. Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956570 Share on other sites More sharing options...
deadline Posted November 13, 2009 Author Share Posted November 13, 2009 k then lets try it like this, do i need to place $ip = $_SERVER['REMOTE_ADDR']; in a "if" statement? Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956574 Share on other sites More sharing options...
mikesta707 Posted November 13, 2009 Share Posted November 13, 2009 that wouldn't really make sense? what would your condition be? just post the code where you set the variable and send the email Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956577 Share on other sites More sharing options...
deadline Posted November 13, 2009 Author Share Posted November 13, 2009 that is what I am saying where do i set the variable, in the form only, or also in the php script? Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956578 Share on other sites More sharing options...
Gayner Posted November 13, 2009 Share Posted November 13, 2009 USE [code TAGS PLZ SIR. THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956582 Share on other sites More sharing options...
deadline Posted November 13, 2009 Author Share Posted November 13, 2009 Went back to the drawing board and rewrote the php script, all working now , thanks everyone for the help, learned from it. Below is the final WORKING php just in case anyone else may need it. <? $mailto='someone@yoururl.com'; $subject="Email Subject here Form"; $thankyou="http://www.yoururl.com"; $body = "Line here of copy\r\n"; $body .= "---------------------------------------------------------------\r\n"; $body .= "Name: ". $_POST['Name'] ."\r\n";; // Gets post value from select menu $body .= "Agree: ". $_POST['value1'] ."\r\n";; // Gets post value from select menu $body .= "Verify: ". $_POST['value2'] ."\r\n";; // Gets post value from select menu $body .= "IP address: ". $_SERVER['REMOTE_ADDR'] ."\r\n"; $body .= "---------------------------------------------------------------\r\n"; mail($mailto,$subject,$body,"From: <noreply@yoururl.com>"); Header ("Location: $thankyou"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/180941-solved-newbie-looking-for-help-before-i-pull-out-my-hair/#findComment-956628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.