-2

As the title says it all, how do I add their IP addresses to my SQL database while they are registering. I'm hosting a game server to track which ID is on which IP.

        <?php
           if($_POST['B1'])
                {
                mysql_select_db($mydbacc);
                $userid = trim($_POST['id']);
                $password=trim($_POST['pass']);
                $passretype=trim($_POST['retpass']);
                $hash=$_POST['hash'];

            if($password != $passretype) {
                        echo "Password not equal to Retyped Password.";
                    }else{
                        if(!ereg("^[0-9a-zA-Z]{4,12}$",$password))
                        {
                            echo "Only letters or numbers, length of 4 to 12 characters";
                        }
                        else
                        {
                            $res = mysql_query("select * from account where name = '".$userid."' order by id desc");
                            if(mysql_num_rows($res) == 0)
                            {
                                mysql_query("insert into account (name,Password,Reg_date) values ('".$userid."','".$hash."','".date("y-m-d H:i:s", time())."')");
                                echo "Account registered successfully.";
                            }
                            else
                            {
                                echo "Account Already exists in database.";
                            }
                        }
                    }


                    }

           ?>

This is the code where users use to register an account, is there any code to put in between somewhere that it can save their IP addresses on IP table?

Thanks!

  • You seem to be successfully adding data to your database already. If you're asking how to get the IP address, that's already been answered here: http://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php If you're asking for someone to write all the code for you, then you're in the wrong place. – Sam Dufel Jun 12 '14 at 17:37

2 Answers2

1

try $ip = $_SERVER['REMOTE_ADDR'];

that should give you the IP

Andriy Lysak
  • 384
  • 5
  • 15
1

You will need to create a column in your Account table to hold the IP address and then insert it by changing your query like this:

       <?php
       if($_POST['B1'])
            {
            mysql_select_db($mydbacc);
            $userid = trim($_POST['id']);
            $password=trim($_POST['pass']);
            $passretype=trim($_POST['retpass']);
            $hash=$_POST['hash'];
            $IP = $_SERVER['REMOTE_ADDR'];

        if($password != $passretype) {
                    echo "Password not equal to Retyped Password.";
                }else{
                    if(!ereg("^[0-9a-zA-Z]{4,12}$",$password))
                    {
                        echo "Only letters or numbers, length of 4 to 12 characters";
                    }
                    else
                    {
                        $res = mysql_query("select * from account where name = '".$userid."' order by id desc");
                        if(mysql_num_rows($res) == 0)
                        {
                            mysql_query("insert into account (name,Password,Reg_date, IP) values ('".$userid."','".$hash."','".date("y-m-d H:i:s", time())."), '".$IP."'");
                            echo "Account registered successfully.";
                        }
                        else
                        {
                            echo "Account Already exists in database.";
                        }
                    }
                }


                }

       ?>
Chancho
  • 1,930
  • 2
  • 15
  • 20