Jump to content

getting IP and Browser information into MySQL database


REM503

Recommended Posts

Hi, I am very new to PHP and would greatly appreciate a little help. I created a very simple form and a MySQL database to see if I could get the information submitted from the form to the database, and also a copy of the form submission emailed to me. I have managed to do that with the help of a few online tutorials. Now, I would like to collect the IP address and browser type of the person submitting the form and have this information transferred to my MySQL database, along with the other form field information. I would like this information to be submitted to columns in my MySQL database labeled "ip" and "browser" respectively.

 

I know how to make the IP address and browser type of a visitor appear on the screen. I simply don't know how to format my .php file in a way that will then store this information in my MySQL database. If someone could please insert whatever code is necessary into my .php file (which I am going to paste below), so I can see exactly what you input and where you put it, I would really appreciate it.  I could then test it and see if it works. Thank you!

 

<?php

$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;

$con = mysql_connect("localhost","MyUsername","MyPassword") or die('Could not connect to server.' ); 
  
if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.example.com/feedback.html" );
  }
  elseif (empty($email) || empty($name) || empty($comments)) {
    header( "Location: http://www.example.com/error.html" );
  }

  else {

mysql_select_db("MyDatabase", $con);
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$comments=mysql_real_escape_string($_POST['comments']);
$sql="INSERT INTO MyTable (name,email,comments) VALUES ('$name','$email','$comments')";
}

if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}


mail( "MyEmail@example.com", 
"Feedback Form Results", 
"Comments: $comments", 
"From: $name <$email>" );

header( "Location: http://www.example.com/thankyou.html" );

?>

Link to comment
Share on other sites

all you got to do is make sure in the database field ip and browser

are in that order and add you browser method ok.

 

good luck m8.

 

 

<?php

$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;
$ip = $_POST['ip'] ;
$browser = $_POST['browser'] ;

$con = mysql_connect("localhost","MyUserName","MyPassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

  if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.example.com/feedback.html" );
  }
  elseif (empty($email) || empty($name) || empty($comments)) {
    header( "Location: http://www.example.com/error.html" );
  }

  else {

mysql_select_db("MyDatabase", $con);
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$comments=mysql_real_escape_string($_POST['comments']);
$ip=mysql_real_escape_string($_POST['ip']);
$browser=mysql_real_escape_string($_POST['browser']);

// the database must  have in order ip and browser at the end of the table filed ok.

$ip=$_SERVER['REMOTE_ADDR'];
//$browser= add your php function as stated 

$sql="INSERT INTO MyTable (name,email,comments,ip,browser) VALUES ('$name','$email','$comments','$ip','$browser')";
}

if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}


mail( "MyEmail@example.com", 
"Feedback Form Results", 
"Comments: $comments", 
"From: $name <$email>" );

header( "Location: http://www.example.com/thankyou.html" );

?>

Link to comment
Share on other sites

all you got to do is make sure in the database field ip and browser

are in that order and add you browser method ok.

 

good luck mate.

 

redarrow, THANK YOU very much! It works great. After using your help and adding a few other things, I am now able to have the IP Address and Browser Type of the person submitting the form entered into my MySQL database and included in the results emailed to me.

 

I just thought of a couple other things I would like for my .php file to accomplish as well...

 

1. Is it possible to have the time and date the person submitted the form entered into my MySQL database? If so, do you think you could show me what I need to add to my file to accomplish this?

 

2. What would you recommend I add to my file for email validation? I am not really worried about validating the other fields (I have some basic validation for them to make sure they are not left blank), but I would like to have some basic, not overly strict, email validation. I have tried to do this, but I keep getting script errors. I must not be formatting it properly, or am putting it into the wrong place in my .php file. Any chance you can show me the email validation you recommend and where it should be placed in my file?

 

Your help is much appreciated by this PHP newbie.  :)

Link to comment
Share on other sites

Here is my current code...

 

<?php

$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;
$ip = $_POST['ip'] ;
$browser = $_POST['browser'] ;

$con = mysql_connect("localhost","MyUsername","MyPassword") or die('Could not connect to server.' ); 
  
if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.example.com/feedback.html" );
  }
  elseif (empty($email) || empty($name) || empty($comments)) {
    header( "Location: http://www.example.com/error.html" );
  }

  else {

mysql_select_db("MyDatabase", $con);
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$comments=mysql_real_escape_string($_POST['comments']);
$ip=mysql_real_escape_string($_POST['ip']);
$browser=mysql_real_escape_string($_POST['browser']);

$ip=$_SERVER['REMOTE_ADDR'];
$browser=$_SERVER["HTTP_USER_AGENT"];

$sql="INSERT INTO MyTable (name,email,comments,ip,browser) VALUES ('$name','$email','$comments','$ip','$browser')";
}

if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}

mail( "MyEmail@example.com", 
"Feedback Form Results", 
"Comments: $comments
IP Address: $ip
Browser: $browser", 
"From: $name <$email>" );

header( "Location: http://www.example.com/thankyou.html" );

?>

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.