Jump to content

Urgent! How to accesss data from MYSql database in PHP?


natasha_thomas
 Share

Recommended Posts

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.  ;D

 

Warm Regards,

Natasha Thomas

 

 

Reference: Coding

 

<?

// 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
Share on other sites

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
Share on other sites

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
Share on other sites

 

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
Share on other sites

Hi  BMurtagh,

 

Thanks for your codes. I have few questions based on ur Codes.

 

$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
Share on other sites

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

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