Forum Moderators: coopster
I'm working on a script that enters an ip address into .htaccess, provided certain criteria are met:
1) the ip = '".$ip."' - (as described below)
2) ($row[allow]=false
*****
$result = mysql_query("SELECT ip,allow FROM iplogging WHERE ip = '".$ip."'");
$row = mysql_fetch_array($result);
if($row[allow]=false){
$filename = ".htaccess";
$fp = fopen($filename,'a+');
fwrite($fp,"deny from $ip \r\n");
fclose($fp);
*****
However, I can't get the script to work as I want it to. The user ip address is not being added to .htaccess as requested when I write (if($row[allow]=false)) BUT when I write anything else instead of false: e.g. true, 1234567, ugabuga, ANYTHING - the ip address is being added.
Could someone please explain me what's going on and what I'm doing wrong.
It looks like there is an equal sign missing...
if($row[allow][red][b]==[/b][/red]false){
Here's a page with the different comparison operators [us2.php.net]. This has got to be the one mistake everyone, no matter how experienced, will always make.
If that doesn't fix the code, post back the revised code (and how it fails, what error it throws) and we'll take another look at it. :)
I did try and run the new script, with the extra '=', but sadly still nothing happends.
The only error it shows is that the script does'nt do as I want it to. I've posted the entire script:
<?php
***
$sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());;
$db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());;
$ip = $_SERVER['REMOTE_ADDR'];
$fetch = mysql_query("SELECT ip FROM iplogging WHERE ip ='".$ip."'") or die(mysql_error());
if ( mysql_num_rows($fetch) == 0 )
{
mysql_query("INSERT INTO iplogging(ip,visits) VALUES('$ip','1')") or die(mysql_error());
} else {
mysql_query("UPDATE iplogging SET visits=visits+1 WHERE ip = '$ip'") or die(mysql_error());
}
mysql_query("UPDATE iplogging SET allow='false' WHERE ip = '$ip' AND visits > '59'");
$result = mysql_query("SELECT ip,allow FROM iplogging WHERE ip = '".$ip."'");
if($row[allow]==false){
$filename = ".htaccess";
$fp = fopen($filename,'a+');
fwrite($fp,"deny from $ip \r\n");
fclose($fp);
}
?>
First, it adds the user IP to the database - if it is'nt there already. If the IP address already exists in the database it adds an extra visit to the specific IP address. When the number of visits from the specific IP address hits '> 59' , 'true' is changed to 'false'.
Everything works fine up until now - but the last part, I just can't get working. The final step would be, opening .htaccess and entering the ip-address - but only if allow=false.
However, as said in my first post, regardless of what I write: (if($row[allow]==true)) (if($row[allow]==hello)) etc. etc. the IP address will be added - but NOT when I write if($row[allow]==false).
What am I doing wrong :?