Jump to content

_Post and ip address collection function


Viper69

Recommended Posts

:confused:

 

I have a basic form for collecting data, I have a function for collecting the ip address of the visitors unfortunately I cannot get the ip collection working with the rest of the form once I put the if($_POST['emailaddress']) {.........} in.

 

My aim is once the user has completed the form, data gone to MySQL database that the data will then be printed via the echo statements on to the form as the action sends it back to the same page.

 

Currently my code is

 

<?php  
/*
This script is a form handler, each section is commented as to what it does.
1. connects to the database (see connect.php).
2. it gets the users ip address.
3. Strips tags from the data entered so that no malicious code can be entered and corrupt/cause problems with the database or site.
4. Inserts the relevant data into the database in this case it is the ip, haveemail, emailaddress, browser, otherbrowser, resolution, otherresolution.
5. Sends the data just entered by the user back to the screen so they can see what they entered
*/

// 1. connection to MySQL  
require ("php/connect.php"); // if this script is unavailable then the rest of the code is pointless as need a connection to the database.  

// if fields are completed
if($_POST['emailaddress'])
{ // if this field has had data entered then process the data

// 2. collect ip address  
//$ip = getRealIpAddr(); 

// 3. Strips tags and POST variables from the form 
$haveemail = strip_tags($_POST['haveemail']);  
$emailaddress = strip_tags($_POST['emailaddress']);  
$browser = strip_tags($_POST['browser']);  
$otherbrowser = strip_tags($_POST['otherbrowser']);  
$resolution = strip_tags($_POST['resolution']);  
$otherresolution = strip_tags($_POST['otherresolution']);  


// 4. insert data to dbase  
$query="INSERT INTO datacollection1 (id, ip, haveemail, emailaddress, browser, otherbrowser, resolution, otherresolution) VALUES ('Null', '$ip', '$haveemail', '$emailaddress', '$browser', '$otherbrowser', '$resolution', '$otherresolution')";  

// Null is in the id field as this is added automatically in the database as it is set to auto increment upon an entry going in and is 	primary key.  

// message to say if database has been updated  

mysql_query($query) or die (mysql_error());  

//echo "<b>Your IP address is: $ip</b> <br />"; 
  


/*function getRealIpAddr() 
{ 
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet 
    { 
      $ip=$_SERVER['HTTP_CLIENT_IP']; 
    } 
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy 
    { 
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
    } 
    else 
    { 
      $ip=$_SERVER['REMOTE_ADDR']; 
    } 
    return $ip; 
} */

// 5. displays information on form to advise what the user just entered
echo '<br/>'."The database has just been updated with the following information: ".'<br/><br/>';
//	echo "Your ip address is ".$ip.'<br/>';
echo "You answered ".$haveemail." to having an email address.".'<br/>';
echo "Your email address is ".$emailaddress.'<br/>';
echo "You use ".$browser." to browse the internet".'<br/>';
echo $otherbrowser.'<br/>';
echo "Your screen resolution is set at ".$resolution.'<br/>';
echo $otherresolution.'<br/>';
echo "Now you have completed this form, please follow onto the main form, this form seeks to get your valuable opinion regarding your likes and dislikes of the websites researched.";
}

mysql_close($db); // closes the database, this is good practice but the database will close once stopped running.


?>

 

I have tried moving the function outside of the if($_POST['emailaddress']) but it still didnt get the ip address.

 

Just as a side note everything else works, the data is written into the database bar the ip address.

 

I would appreciate some help  ;)

 

Thanks in advance.

 

Link to comment
Share on other sites

No sorry, I commented it out because it does not work as it is, as I say I have placed the function where it is and outside of the _POST, still not working hence coming in here to see if someone can spot what I need to do.

 

The function works independent of the rest of the code just not with the POST.

 

Thanks in advance for any help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.