Jump to content

Whats wrong with this script?


MySQL_Narb

Recommended Posts

Alright, I don't get any errors or anything, and my website works fine. But please do tell me why this isn't storing the IP like it's suppose to.

 

I've added it, but why doesn't it submit the IP to the database?

 

Code:

 

<center><style type="text/css">

a:link {
color:#24374C;
text-decoration:bold;
}

a:visited {
color:#24374C;
text-decoration:bold;
}

a:active {
outline: none;
color:#24374C;
text-decoration:bold;
}

body {background-color:#b0c4de}

hr.{backround-color:#b0c4de}

div.box {
width:265px;
padding:10px;
border:3px double #000000;
margin:10px;
background-color:#74AFF2;
}
</style>
<?php

$con = mysql_connect('', '', '') or die(mysql_errno());
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
    $_POST = array_map('stripslashes', $_POST);
}

$name = mysql_real_escape_string($_POST['name']);
$message = mysql_real_escape_string($_POST['message']);

if (strlen($message)<=1) 
    echo "<div class='box'><b><span style='color:red'>Please enter a message!</span></b></div>";
else 
{

function validip($ip) {

if (!empty($ip) && ip2long($ip)!=-1) {

$reserved_ips = array (

array('255.255.255.0','255.255.255.255')

);


foreach ($reserved_ips as $r) {

$min = ip2long($r[0]);

$max = ip2long($r[1]);

if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;

}

return true;

} else {

return false;

}
}

function getip() {

if (validip($_SERVER["HTTP_CLIENT_IP"])) {

return $_SERVER["HTTP_CLIENT_IP"];

}

foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) {

if (validip(trim($ip))) {

return $ip;

}

}

if (validip($_SERVER["HTTP_X_FORWARDED"])) {

return $_SERVER["HTTP_X_FORWARDED"];

} elseif (validip($_SERVER["HTTP_FORWARDED_FOR"])) {

return $_SERVER["HTTP_FORWARDED_FOR"];

} elseif (validip($_SERVER["HTTP_FORWARDED"])) {

return $_SERVER["HTTP_FORWARDED"];

} elseif (validip($_SERVER["HTTP_X_FORWARDED"])) {

return $_SERVER["HTTP_X_FORWARDED"];

} else {

return $_SERVER["REMOTE_ADDR"];

}
}

//connect
$connect = mysql_connect("","","") or die("Connection failed!");
mysql_select_db("") or die("Database fail!");

//write
$write = mysql_query("INSERT INTO posts VALUES ('','$name','$message', '$ip')") or die(mysql_eror());

echo "<div class='box'><font face='arial'><b><span style='color:green'>Posted! Your name was:</span> $name</b> - Your message was....<br><br><b>$message - <a href='index.php'>View it!</a></b>";
}


?>

Link to comment
https://forums.phpfreaks.com/topic/177627-whats-wrong-with-this-script/
Share on other sites

At a quick glance, it looks like all your IP processing is done in functions, but there's never actually any call to those functions so the $ip variable remains undefined in the global scope through the duration of your script.

 

I think you just need to call one of your IP functions to actually return the IP to a variable.

 

Maybe?:

<?php
$write = mysql_query("INSERT INTO posts VALUES ('','$name','$message', 'getip()')") or die(mysql_eror());
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.