natasha_thomas Posted July 28, 2009 Share Posted July 28, 2009 Folks, I need an urgent help. Hope here ppl will help out. Requirement: What i want is, when a visitor comes to my site, his IP will be captures in $ip (refer Coding below), as you can see in coding, i want this IP to be searched in MYSQL database (I have a list of 25000 IPs there), and if the $ip matches with the MYSQL database IP, it should make $is_bot as TRUE. In the existing codign this thing has been taken care but there is not Database Lookup feature, but Text file look up, and if i have moer than 25000 IPs in the Text fie then i believe the lookup will take hell lot of time, so i think looking up in MY Sql database will be way faster. Request: I rquuest guys here, to help me out with the exact codes that i should add int he existing codign so that i can achieve my requirement. We have a Beer deal, should you happen to visit DC. Warm Regards, Natasha Thomas Reference: Coding Quote <? // PHPLOCKITOPT NOENCODE // NOTES: // $ip_array contains the list of IPs you have in your ip_list.txt file // $ip is the IP address of the current visitor. // $agent is the user agent string of the current visitor. // $referrer is the referrer sent by the visitor's browser, if any. // $cloaked_url is the cloaked URL for the campaign being visited. // You can add additional criteria for detecting "bots" below, but you better know what you are doing! To add IPs to detect, simply put them in the ip_list.txt file. if( @in_array($ip,$ip_array) || stristr(strtolower($agent),"bot") || stristr(strtolower($agent),"spider") || stristr(strtolower($agent),"slurp") ) { $is_bot = true; } // You can add additional criteria for detecting "networks" below, but you better know what you are doing! if( ( @in_array($ip,$ip_array) || $keyword == "" || $keyword == "-" || $keyword == "test" || @stristr(strtolower($keyword),"searchtext") || @stristr($keyword,"{") || @stristr($keyword,"[") || @stristr(strtolower($referrer),"trafficvance") || @stristr(strtolower($referrer),"addonnetwork") ) && @!stristr(strtolower($referrer),strtolower($cloaked_url)) // DO NOT CHANGE OR REMOVE THIS LINE && @$_GET['mode'] != "test") // DO NOT CHANGE OR REMOVE THIS LINE { $enable_cloak=true; } if($is_bot == true || $enable_cloak == true) { $fwrite=@fopen("data/bots.txt","a+"); // Will write this bot's data to a text file called bots.txt @fwrite($fwrite,$accesstime."\t".$ip."\t".$link."\t".$agent."\t".$referrer."\n"); @fclose($fwrite); die(header("Location: http://".$cloaked_url)); // This is what should happen if it is determined that the cloaked URL should be shown to the current visitor. Be SURE you know what you're doing if you change this. } ?> Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/ Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 1: You need to create a database. 2: Then a table inside that database. Do you have access to a MySQL database? Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884686 Share on other sites More sharing options...
BMurtagh Posted July 28, 2009 Share Posted July 28, 2009 Hi there, The code I came up with (untested) when I took a look at it would be: userip = mysql_real_escape_string($ip); $sql ="SELECT * FROM tbl_ip WHERE ip='" . $userip . "')"; $result = mysql_query($sql); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; die($message); else { $is_bot = "TRUE"; } Granted this is untested, but it should at least give you a beginning and allow others to comment changes or provide similar examples. Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884688 Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 If you don't have a database or a table for it yet, you'll need a setup file. <?php $db_name = "my_database"; //if you create one manually in your cpanel, change the name $db_pass = ""; //you have to get this from your host $db_user = ""; //you have to get this from your host $mysql_host = "localhost"; //could be different, check details on your cpanel $connect = mysql_connect($mysql_host,$db_user,$db_pass) or die("<h1>Could not connect to MySQL:</h1>".mysql_error()); //attempt to select database $select = mysql_select_db($db_name); //if failure to select, attempt to create it if(!$select){ $create = mysql_query("CREATE DATABASE $dn_name") or die("<h1>Could not select OR create database $dn_name</h1>".mysql_error()); $select = mysql_select_db($db_name) or die("<h1>Could not select database $db_name</h1>".mysql_error()); } //if all went well create table to hold ip address mysql_query(" CREATE TABLE IF NOT EXISTS ip_addresses( id INT(11) NOT NULL AUTO_INCREMENT, ip VARCHAR(20) NOT NULL UNIQUE, PRIMARY KEY(id))") or die("<h1>Could not create table ip_addresses</h1>".mysql_error()); echo '<h1>Setup was a success</h1>'; ?> Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884690 Share on other sites More sharing options...
natasha_thomas Posted July 28, 2009 Author Share Posted July 28, 2009 Hello, Aplozies for duplicate posts. I already have a dataabsed and a table too created for the Ip_addressess, all i need is modification in the existing codes to lookup for the IPs. BMurtagh, thanks for the codes, though i still have to test them out. TTYL Natasah T. Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884707 Share on other sites More sharing options...
natasha_thomas Posted July 28, 2009 Author Share Posted July 28, 2009 Hi BMurtagh, Thanks for your codes. I have few questions based on ur Codes. Quote $con = mysql_connect("localhost","mysql_userName","mysql_Password"); userip = mysql_real_escape_string($ip); $sql ="SELECT * FROM tbl_ip WHERE ip='" . $userip . "')"; $result = mysql_query($sql,$con); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; die($message); else { $is_bot = true; } In the codes at line no 2, before userip there is no "$" sign, did u forget to put? Is the connection to database function correct? Regards Natasha T. Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884718 Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 Hi could you tell us the structure of your tables? As in the name of the database table and its columns. Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884720 Share on other sites More sharing options...
natasha_thomas Posted July 28, 2009 Author Share Posted July 28, 2009 database name: PPC_IP Table Name: IP_TABLE Columns = 2 Columns (Number, IP_address ) Thanks Natasha T. Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884729 Share on other sites More sharing options...
trq Posted July 28, 2009 Share Posted July 28, 2009 Theres a link in my signiture to a free book (Hudzilla), within that book is an entire chapter on using mysql with php. Not much point writing you your own personal tutorial when there are literally thousands already on the net covering this very subject. Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884733 Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 What kind of columns are number and IP_address? Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884736 Share on other sites More sharing options...
natasha_thomas Posted July 28, 2009 Author Share Posted July 28, 2009 Nimber - is Numeric Data type IP_address is String Datatype Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884739 Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 <?php // PHPLOCKITOPT NOENCODE // NOTES: // $ip_array contains the list of IPs you have in your ip_list.txt file // $ip is the IP address of the current visitor. // $agent is the user agent string of the current visitor. // $referrer is the referrer sent by the visitor's browser, if any. // $cloaked_url is the cloaked URL for the campaign being visited. /************************* MySQL Addition **************************/ $db_pass = ""; //Only you know this $db_user = ""; //Only you know this $db_name = "PPC_IP"; $mysql_host = ""; //Only you know this - could be localhost or something else $conn = mysql_connect($mysql_host,$db_user,$db_pass) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error()); $ip = mysql_real_escape_string($ip,$conn); $count = mysql_query("SELECT COUNT(*) FROM IP_TABLE WHERE IP_address = '$ip'") or trigger_error(mysql_error()); $ip_exists = false; //by default - will change if proven otherwise if($count){ list($num) = mysql_fetch_row($count); if($num == 0){ $ip_exists = true; } } /********************* IF $ip_exists == true, IP exists *********************/ // You can add additional criteria for detecting "bots" below, but you better know what you are doing! To add IPs to detect, simply put them in the ip_list.txt file. if( @in_array($ip,$ip_array) || stristr(strtolower($agent),"bot") || stristr(strtolower($agent),"spider") || stristr(strtolower($agent),"slurp") ) { $is_bot = true; } // You can add additional criteria for detecting "networks" below, but you better know what you are doing! if( ( @in_array($ip,$ip_array) || $keyword == "" || $keyword == "-" || $keyword == "test" || @stristr(strtolower($keyword),"searchtext") || @stristr($keyword,"{") || @stristr($keyword,"[") || @stristr(strtolower($referrer),"trafficvance") || @stristr(strtolower($referrer),"addonnetwork") ) && @!stristr(strtolower($referrer),strtolower($cloaked_url)) // DO NOT CHANGE OR REMOVE THIS LINE && @$_GET['mode'] != "test") // DO NOT CHANGE OR REMOVE THIS LINE { $enable_cloak=true; } if($is_bot == true || $enable_cloak == true) { $fwrite=@fopen("data/bots.txt","a+"); // Will write this bot's data to a text file called bots.txt @fwrite($fwrite,$accesstime." ".$ip." ".$link." ".$agent." ".$referrer." "); @fclose($fwrite); die(header("Location: http://".$cloaked_url)); // This is what should happen if it is determined that the cloaked URL should be shown to the current visitor. Be SURE you know what you're doing if you change this. } ?> Link to comment https://forums.phpfreaks.com/topic/167765-urgent-how-to-accesss-data-from-mysql-database-in-php/#findComment-884755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.