user registration help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

rlthompson
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:59 pm

user registration help

Post 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';
 
} 
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: user registration help

Post 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.
rlthompson
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:59 pm

Re: user registration help

Post 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';
 
} 
}
?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: user registration help

Post by flying_circus »

$_SERVER['REMOTE_ADDR'] isnt an integer.

You can try ip2long($_SERVER['REMOTE_ADDR']);
rlthompson
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:59 pm

Re: user registration help

Post 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]
                       
 
arter
Forum Newbie
Posts: 3
Joined: Sun Mar 21, 2010 10:25 pm

Re: user registration help

Post by arter »

can i have your registration form... i need it.. hehe
Henri_IV
Forum Newbie
Posts: 3
Joined: Mon Mar 22, 2010 10:15 pm

Re: user registration help

Post by Henri_IV »

I was wondering if it would be possible to integrate te logs into a file instead of a db?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: user registration help

Post 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.
Henri_IV
Forum Newbie
Posts: 3
Joined: Mon Mar 22, 2010 10:15 pm

Re: user registration help

Post 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
rlthompson
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:59 pm

Re: user registration help

Post 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.
rlthompson
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:59 pm

Re: user registration help

Post by rlthompson »

Bump, can anyone help me here?
Alkis
Forum Commoner
Posts: 31
Joined: Fri Mar 26, 2010 8:41 am

Re: user registration help

Post by Alkis »

It actually is $_SERVER['REMOTE_ADDR'] not $_SERVER['remote_addr']. Case matters.
rlthompson
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:59 pm

Re: user registration help

Post 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';
 
} 
}
?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: user registration help

Post 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"));
?>
rlthompson
Forum Newbie
Posts: 13
Joined: Sun Mar 21, 2010 1:59 pm

Re: user registration help

Post 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
Post Reply