Page 1 of 2

user registration help

Posted: Sun Mar 21, 2010 2:12 pm
by rlthompson
Hello everyone,

I am trying to get my new user registration script to log the user's ip address in a db table. I've been told to put in the following line but it doesn't log the ip.

$ip = $_SERVER['REMOTE_ADDR'];

So basically my intention is when people register on the site it logs their ip address in the same table as user name, email address etc. The db table is called user_auth and the field is called remote_addr

Thanks in advance for any help and please respond as if you're addressing a php noob since I am.

Code: Select all

<?php require_once('../Connections/user.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  if($_POST['password'] == $_POST['confirm_password'])
{
  $insertSQL = sprintf("INSERT INTO user_auth (password, confirm_password, email, refer, newsletter, `user`) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['confirm_password'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['refer'], "text"),
                       GetSQLValueString(isset($_POST['newsletter']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['user'], "text"));
 
  mysql_select_db($database_user, $user);
  $Result1 = mysql_query($insertSQL, $user) or die(mysql_error());
  } else {
echo 'Your passwords did not match';
 
} 
}
?>

Re: user registration help

Posted: Sun Mar 21, 2010 3:55 pm
by requinix
$_SERVER['REMOTE_ADDR'] is the IP address just like how $_POST['password'] is the password and $_POST['email'] is the email address.

Code: Select all

$insertSQL = sprintf("INSERT INTO user_auth (password, confirm_password, email, refer, newsletter, `user`) VALUES (%s, %s, %s, %s, %s, %s)",
                     GetSQLValueString($_POST['password'], "text"),
                     GetSQLValueString($_POST['confirm_password'], "text"),
                     GetSQLValueString($_POST['email'], "text"),
                     GetSQLValueString($_POST['refer'], "text"),
                     GetSQLValueString(isset($_POST['newsletter']) ? "true" : "", "defined","'Y'","'N'"),
                     GetSQLValueString($_POST['user'], "text"));
 
That's where you need to put the IP address. Take a guess as to how it should look.

Re: user registration help

Posted: Sun Mar 21, 2010 6:13 pm
by rlthompson
Well I took a guess and there are no errors but it doesn't put an ip into the database field it only puts a 0.
database field info: remote_addr int(16) UNSIGNED Not Null :banghead:

Code: Select all

<?php require_once('../Connections/user.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  if($_POST['password'] == $_POST['confirm_password'])
{
  $insertSQL = sprintf("INSERT INTO user_auth (password, confirm_password, email, refer, newsletter, `user`) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['confirm_password'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['refer'], "text"),
                       GetSQLValueString(isset($_POST['newsletter']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['user'], "text"),
                       GetSQLValueString($_SERVER['remote_addr'], "text"));
 
  mysql_select_db($database_user, $user);
  $Result1 = mysql_query($insertSQL, $user) or die(mysql_error());
  } else {
echo 'Your passwords did not match';
 
} 
}
?>

Re: user registration help

Posted: Sun Mar 21, 2010 6:16 pm
by flying_circus
$_SERVER['REMOTE_ADDR'] isnt an integer.

You can try ip2long($_SERVER['REMOTE_ADDR']);

Re: user registration help

Posted: Sun Mar 21, 2010 6:30 pm
by rlthompson
Couldn't I leave it and change the field to something else like varchar?

Are you sure this is right in order to post the ip in the db?

Code: Select all

   GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['confirm_password'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['refer'], "text"),
                       GetSQLValueString(isset($_POST['newsletter']) ? "true" : "", "defined","'Y'","'N'"),
                       [color=#0000FF]GetSQLValueString($_SERVER['remote_addr'], "text"));[/color]
                       
 

Re: user registration help

Posted: Sun Mar 21, 2010 11:22 pm
by arter
can i have your registration form... i need it.. hehe

Re: user registration help

Posted: Tue Mar 23, 2010 12:06 am
by Henri_IV
I was wondering if it would be possible to integrate te logs into a file instead of a db?

Re: user registration help

Posted: Tue Mar 23, 2010 2:39 am
by requinix
Henri_IV wrote:I was wondering if it would be possible to integrate te logs into a file instead of a db?
Sure. But I wouldn't say you should do it instead. Either both or set up a regular backup mechanism to copy the database logs to an archive.

If you need help create a new thread - don't use this one.

Re: user registration help

Posted: Tue Mar 23, 2010 7:24 pm
by Henri_IV
tasairis wrote:If you need help create a new thread - don't use this one.
I did : viewtopic.php?f=34&t=114501

Thank you

Re: user registration help

Posted: Tue Mar 23, 2010 10:13 pm
by rlthompson
rlthompson wrote:Couldn't I leave it and change the field to something else like varchar?

Are you sure this is right in order to post the ip in the db?

Code: Select all

   GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['confirm_password'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['refer'], "text"),
                       GetSQLValueString(isset($_POST['newsletter']) ? "true" : "", "defined","'Y'","'N'"),
                       [color=#0000FF]GetSQLValueString($_SERVER['remote_addr'], "text"));[/color]
                       
 
And now to hopefully get this thread back on topic. Does anyone have any ideas as to what I'm doing wrong here? I'm trying to log a user's ip address into a db field that is varchar 16 now.

Again, thanks in advance for any help.

Re: user registration help

Posted: Sat Mar 27, 2010 8:31 pm
by rlthompson
Bump, can anyone help me here?

Re: user registration help

Posted: Sat Mar 27, 2010 8:55 pm
by Alkis
It actually is $_SERVER['REMOTE_ADDR'] not $_SERVER['remote_addr']. Case matters.

Re: user registration help

Posted: Sun Mar 28, 2010 9:49 am
by rlthompson
I changed it and it is still not working. Here is the code at this moment.

Code: Select all

<?php require_once('../Connections/user.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  if($_POST['password'] == $_POST['confirm_password'])
{
  $insertSQL = sprintf("INSERT INTO user_auth (password, confirm_password, email, refer, newsletter, `user`) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['confirm_password'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['refer'], "text"),
                       GetSQLValueString(isset($_POST['newsletter']) ? "true" : "", "defined","'Y'","'N'"),
					   GetSQLValueString($_POST['user'], "text"),
                       GetSQLValueString($_SERVER['REMOTE_ADDR'], "text"));
					   


  mysql_select_db($database_user, $user);
  $Result1 = mysql_query($insertSQL, $user) or die(mysql_error());
  } else {
echo 'Your passwords did not match';
 
} 
}
?>

Re: user registration help

Posted: Sun Mar 28, 2010 3:27 pm
by flying_circus
rlthompson wrote:I changed it and it is still not working. Here is the code at this moment.
Of course that doesn't work. You have supplied too many arguments to sprintf(). You also havent modified your query to accept the new IP address either.

Code: Select all

<?php
  $insertSQL = sprintf("INSERT INTO `user_auth` (`password`, `confirm_password`, `email`, `refer`, `newsletter`, `user`, `ip`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s');",
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['confirm_password'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['refer'], "text"),
                       GetSQLValueString(isset($_POST['newsletter']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['user'], "text"),
                       GetSQLValueString($_SERVER['REMOTE_ADDR'], "text"));
?>

Re: user registration help

Posted: Sun Mar 28, 2010 6:12 pm
by rlthompson
thanks for the replies, I'm getting this error after I submit now though.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password'', ''password'', ''none'', ''none'', ''Y'', ''test'', ''216.14.231.20''' at line 1