I am trying to create a working sql query that will give an accurate FLAG for a known or unknown IP address in our database upon hitting our landing page. I use this
// get the IP of the user on this landing page
$ip_address=$_SERVER['REMOTE_ADDR'];
echo $ip_address. ' ip address<br/>';
// see if we know this user..
$search_first="SELECT COUNT(DISTINCT(ip_address)) FROM `qb_sessions` WHERE `ip_address` = '$ip_address' ";
$has_ips = mysql_query($search_first);
$has_ip = mysql_num_rows($has_ips);
echo $has_ip. ' has IP<br/>'; // should show count in table
Using this snippet does not provide the result that I need. If the table is empty, (no match to this IP), I still get a '1' as the echo of $has_ip. I really need '0' if there is no match, and anything '> 1' if this IP has been recorded in qb_sessions
table before. The purpose of this snippet is to identify a new, non-registered hit on this landing page and later to redirect them to another page, like join a newsletter, get a discount coupon, etc.
I have tried various combinations of the sql to no avail, including escaping the (') $ip_address quotes.
Thank you in advance for any help.