Jump to content

Inserting IP Address into MySql Table with PHP


DYWBH

Recommended Posts

Hello, I have created a very basic form that inserts user information to a MySql table. I am now trying to make it insert the useres IP into the IP column within the table.

 

Here is my code:

 

<?php


mysql_connect("mysql9.000webhost.com", "a3313201_admin", "xxxxxx") or die(mysql_error()); mysql_select_db("a3313201_admin") or die(mysql_error()); 

$Username = $_POST['Username'];
$Password= $_POST['Password'];


$query="INSERT INTO Users (Username, Password, [b]IP[/b])VALUES('".$Username."', '".$Password."', [b]???[/b])";

mysql_query($query) or die ('Error 101');

echo "Accoount Created";

?>

Thank you for your help

Link to comment
Share on other sites

The $_SERVER super global can give you the user's IP address.

 

$query="INSERT INTO Users (Username, Password, IP)VALUES('".$Username."', '".$Password."', $_SERVER['REMOTE_ADDR'])";

 

EDIT: By the way, your script is at high risk of SQL injection. You cannot just dump user input directly into your database, you need to sanitize it first.

 

At the very least, do:

 

$Username = mysql_real_escape_string($_POST['Username']);
$Password = mysql_real_escape_string($_POST['Password']);

 

Also as dougjohnson said, you should never store a password as plaintext. Use a one-way hash algorithm, like sha1() at the very least.

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.