Been working with this script for quite some time now but no matter what I do it will not add the IP Address into the database at all. It instead adds it as a NULL value and I cannot figure out why. I do not want to use IP2Long or long2ip as the IP Address needs to match the incoming connection to even sign into the site. The code I'm using to grab the IP Is Below.
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$IP = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$IP = $_SERVER['REMOTE_ADDR'];
}
Which works great. When viewing it in just an echo it shows the correct IP Address that I wanted, so the code is not the issue. The next function is how it inserts the code into the database upon registering.
function Register()
{
$con = mysqli_connect("removed","removed","removed","removed");
if(!empty($_POST['Key']) AND !empty($_POST['Username']))
{
$errors="";
$query="SELECT `Key` FROM `Keys`
WHERE `Key`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
if($row['Key'])
{
$query="SELECT `Activated` FROM `Keys` WHERE `Key`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
if($row['Activated']){
$errors= "This key has already been activated. You cannot create an account with this key.";
}
}
else{
$errors= "Key not found. Please contact us for assistance!";
}
if($errors){
echo $errors;
}else{
//ip insert update
$query="UPDATE `Keys`
SET `IP` = '$IP',
`Activated` = '1', `Username` = '".mysqli_real_escape_string($con,$_POST['Username'])."'
WHERE `Key`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";
$result = mysqli_query($con,$query);
if($result){
$query="UPDATE `Keys` SET `Timestamp` = NOW(), `IP` = '$IP', `DATE` = NOW() WHERE `Keys`= '".mysqli_real_escape_string($con,$_POST['Key'])."'";
$result=mysqli_query($con,$query);
}else{
$errors.="Could not sign up. Please contact us for assistance.";
}
//email check
$query = "SELECT*FROM `Users` WHERE email='".mysqli_real_escape_string($con,$_POST['Email'])."'";
$result = mysqli_query($con,$query);
$results = mysqli_num_rows($result);
if($results>0){
$errors.="The email provided is already registered. </br>";
}
//username check
$query = "SELECT*FROM `Users` WHERE username='".mysqli_real_escape_string($con,$_POST['Username'])."'";
$result = mysqli_query($con,$query);
$results = mysqli_num_rows($result);
if($results>0){
$errors.= "The username provided is already registered. </nr>";
}
if($errors){
echo $errors;
}else{
$query = "INSERT INTO `Users` (`username`,`name`,`email`, `password`) VALUES ('".mysqli_real_escape_string($con, $_POST['Username'])."','".mysqli_real_escape_string($con, $_POST['Name'])."','".mysqli_real_escape_string($con, $_POST['Email'])."', '".md5(md5($_POST['Email']).$_POST['password'])."')";
$result = mysqli_query($con,$query);
if($result){
echo '<script type="text/javascript"> window.onload = function () { alert("Registration and activation was successful."); } </script>';
}else{
echo '<script type="text/javascript"> window.onload = function () { alert("An error occurred. Please contact us for assistance."); } </script>';
}
}
}
}
}
This code just will not add the IP Address into the system and I can't figure out why. It works fine in other sites, just not this one.
Any ideas?
Wouldn't be an issue except it's for a Client's system and I want it perfect. Thanks