Archive for the ‘PHP’ Category

PHP cURL?

7.06.2008 | Author:Black iD Team

Lately, I have been doing a lot of gimmicks with various things related to sending and receiving data over web pages in the back end, i.e. without the user actually browsing or sending data. I have done quite some research on the subject. I did find some noteworthy things. cURL was the first method that I came accross. Like most of the php guys would do, I jumped on to it. Used cURL for some applications I was developing. These applications required a lot of web page openings.

Now, as most of you experienced users might be knowing, or for the newbies, I found that, many of my scripts, the timed out. And my shared host, at gohindi dot com, that idiot, suspended my account for nothing. He said, he couldn’t take the load of the scripts. So, my site hung up in a stupid suspended page (Sorry guys, you might have missed me :)).

So, I started looking for other options. Changing to a dedicated or a VPS was out of option, as it proved to be too costly. So I decided to change the technology. What I found was fsock was the next best option. Though not pirticularly easy to use, it was something that was useful. Then, this brilliant idea struck me. I explored, file_get_contents function of PHP. It did solve my problem, as long as I didnt have to post any data. So I stuck with it. It was fun. I could get the things done in an instant. And it was pretty safe and easy too.

$google = file_get_contents(”http://www.google.com”);

Thus in a single line statement I could do what required a few lines in fsock as well as cURL. This was a nice method, but had its limitations. I couldn’t post data to these sites. So, I decided to come up with a class. This would surely make an ease. This class is useful for the newbies. It is called pageFetcher. So, ladies and gentlemen, here I present to you,

pageFetcher

Finally Coming out with PHP Products

14.01.2008 | Author:Black iD Team

Well,

Finally the wait is over. Black iD is into the final testing of the class we have been working on for the last 6 months. We are running it through the phpDocumentor and trying to make the best for you all. The next releases have been planned already. We will be fully supporting the current release in the next one. So we hope you enjoy this class a lot. The code for this class is available here. We will try to expand this class even more.

Another excellent news is that we are now registered as the developers at PEAR website. We are proud to be supporting a community that has worked so hard thus far to keep up the spirits high. We will try to contribute in each way we can to come up with the best pieces of code. For now please accept this beautiful piece of code

<?php
/* =====================================================================================================================

	MySQL DATABASE ACCESS CLASS
		--> WRITTEN BY JYOT VAKHARIA. [Black iD Solutions(www.blackidsolutions.com)].
		--> Open Source GPL Lisence.
		--> VERSION 1.1 Release 1

		New
			--> Added the insert Method.
			--> Reduced the number of loops to make the class process faster and improve
				upon speed when database size is large.
			-->
===================================================================================================================== */

	class db
		{
			var $uname, $pass, $host, $db, $conn, $con_db;

			/* USEAGE : $db->connect('host','username','password','databasename');
						OR
						$db->connect(array('host','username','password','databasename'));
						OF
						$db->connect(array('host' => 'hostname', 'uname' => 'username', 'pass'=>'password', 'db'=>'db_name'));
			*/
			function connect()
				{
					$args = func_get_args();
					if(is_array($args[0]))
						$args = $args[0];
					if(!($this->uname) || !($this->host) || !($this->db))
						{
							// LOOK OUT FOR CONNECTION SETTINGS IF NOT DEFINED
							$this->host = $args['host'];
							$this->uname = $args['uname'];
							$this->pass = $args['pass'];
							$this->db = $args['db'];
						}
					if(!($this->uname) || !($this->host) || !($this->db))
						{
							// LOOK OUT FOR CONNECTION SETTINGS IF NOT DEFINED
							$this->host = $args[0];
							$this->uname = $args[1];
							$this->pass = $args[2];
							$this->db = $args[3];
						}
					$this->conn = mysql_connect($this->host,$this->uname, $this->pass) or die("Could not connect to the databae");
					$this->conn_db = mysql_select_db($this->db) or die("Could not select the database with the name " . $this->db);
					return $this->conn_db;
				}

			// USAGE $db->query($sql);
			// RETURNS THE RESULTANT DATASET.
			function query($sql)
				{
					return mysql_query($sql);
				}

			// USAGE $db->getFieldsFor('table');
			// RETURNS THE field name list as an array.
			function getFieldsFor($tablename)
				{
					$sql = "Select * From $tablename LIMIT 1";
					$fld = $this->getFieldsForSQL($sql);
					return $fld;
				}
			// USAGE $db->getFieldsForSQL("Select * from foo where bar='foo'");
			// RETURNS THE field name list as an array.
			function getFieldsForSQL($sql)
				{
					$db =  $this->query($sql);
					$i = 0;
					while($i < @mysql_num_fields($db))
						{
							$fld[] = mysql_field_name($db, $i);
							$i++;
						}
					mysql_free_result($db);
					return $fld;
				}
			// USAGE $db->table_array_from_tablename('table');
			// RETURNS THE aray containing the entire table as an array.
			// You can sort it as you like. It is currently keyed array.
			// To get a pirticular field use $tbl = $db->table_array_from_tablename('table'); $fld = $dbl[rowno]['fldname'];
			function table_array_from_tablename($tablename, $conds="1")
				{
					$sql = "Select * from $tablename WHERE $conds";
					$table = $this->table_array_from_sql($sql);
					return $table;
				}

			// USAGE $db->table_array_from_sql('Select * from tablename');
			// RETURNS THE aray containing the entire table as an array.
			// You can sort it as you like. It is currently keyed array.
			// To get a pirticular field use $tbl = $db->table_array_from_sql('table'); $fld = $dbl[rowno]['fldname'];
			function table_array_from_sql($sql)
				{
					$db = $this->query($sql);
					$flds = $this->getFieldsForSQL($sql);
					while($row = mysql_fetch_array($db,1))
						{
							$table[] = $row;
						}
					return $table;
				}
			function table_row_from_tablename($tablename, $conds="1")
				{
					$sql = "Select * from $tablename WHERE $conds";
					$table = $this->table_array_from_sql($sql);
					return $table[0];
				}

			// USAGE $db->table_array_from_sql('Select * from tablename');
			// RETURNS THE aray containing the entire table as an array.
			// You can sort it as you like. It is currently keyed array.
			// To get a pirticular field use $tbl = $db->table_array_from_sql('table'); $fld = $dbl[rowno]['fldname'];
			function table_row_from_sql($sql)
				{
					$row = $this->table_array_from_sql($sql);
					return $row[0];
				}

			function field_val_from_table($table, $field_name, $crit_field, $crit_val)
				{
					$sql = "Select $field_name from $table where $crit_field='$crit_val'";
					$res = $this->field_val_from_sql($sql);
					return $res;
				}
			function field_val_from_sql($sql)
				{
					$res = mysql_fetch_array($this->query($sql));
					return $res[0];
				}
			function flat_field_array($table, $field_name, $cond = " WHERE 1")
				{
					$sql = "SELECT $field_name FROM $table $cond";
					$res = $this->query($sql);
					while($row = mysql_fetch_array($res))
						{
							$arr[] = $row[$field_name];
						}
					return $arr;
				}

			function field_associative_array_from_table($tablename,$condition = "1",$fld_key,$fld_val,$val_seperator="-")
				{
					$tbl = $this->table_array_from_sql("SELECT $fld_key, $fld_val FROM $tablename WHERE $condition");
					$flds = explode(",",$fld_val);
					error_reporting(0);
					foreach($tbl as $row)
						{
							foreach($flds as $fld)
								{
									$row_val .= ($row_val!="") ? " $val_seperator {$row[$fld]}" : $row[$fld] ;
								}
							$retval[$row[$fld_key]] = $row_val;
							$row_val = "";
						}
					error_reporting(1);
					return $retval;
				}
			////////////////////////// USAGE /////////////////////////////////////////////
			//			$db->insert("tablename","field1=value1","field2=value2",...);   //
			//		$db->insert("tablename",array("field1=value1","field2=value2",...));//
			//		$db->insert("tablename","field1=value1;field2=value2;...");         //
			//////////////////////////////////////////////////////////////////////////////
			function insert()
				{
					$args = func_get_args();
					$first_arg = $args[0];
					if(!(is_array($first_arg)))
						$first_arg = split(";", $first_arg);
					$table = $first_arg[0];
					$opts = $first_arg[1];
					$opts = split(":",$opts);
					$ret_fld = $opts[1];
					$field_list = $this->getFieldsFor($table);
					if(is_array($args[1]))
						{
							$args = $args[1];
						}
					elseif(count($args) == 2)
						{
							$args = split(";", $args[1]);
						}
					else
						{
							$args = array_splice($args,1);
							foreach($args as $arg)
								{
									$temp_arg = split("=",$arg);
									$args2[$temp_arg[0]] = $temp_arg[1];
								}
							$args = $args2;
						}
					$flds = "";
					$vals = "";
					if(array_keys($args))
						{
							foreach($args as $fld=>$val)
								{
									if(in_array($fld,$field_list) && (!($_FILES['$fld']['name'])))
										{
									if(is_array($val))
										$val = join(";", $val);
									$flds .= $flds ? ","."`".$fld."`" : "`".$fld."`";
									$vals .= $vals ? ",'".$val."'" : "'".$val."'";
										}
								}
						}
					else
						{
							foreach($args as $arg)
								{
									$temp_arg = split("=",$arg);
									$fld = "`".$temp_arg[0]."`";
									if(in_array($fld,$field_list) && (!($_FILES['$fld']['name'])))
										{
									$val = str_replace("'","",$temp_arg[1]);
									$flds .= $flds ? ",".$fld : $fld;
									$vals .= $vals ? ",'".$val."'" : "'".$val."'";
										}
								}
						}
					$sql = "INSERT INTO $table ($flds) VALUES ($vals)";
					$this->query($sql);
					if(mysql_error())
						{
							return -1;
						}
					elseif($ret_fld)
						{
							$ret_sql = "Select " . str_replace("return","",str_replace("_","",$opts[0]))."($ret_fld) from $table";
							$ret_val = $this->field_val_from_sql($ret_sql);
							return $ret_val;
						}
				}

		////////////////////////// USAGE /////////////////////////////////////////////
			//			$db->update("tablename","updating condition","field1=value1","field2=value2",...);   //
			//		$db->update("tablename","updating condition",array("field1=value1","field2=value2",...));//
			//		$db->update("tablename","updating condition","field1=value1;field2=value2;...");         //
			//////////////////////////////////////////////////////////////////////////////
				function update()
				{
					$args = func_get_args();
					$first_arg = $args[0];
					$condition = $args[1];
					if(!(is_array($first_arg)))
						$first_arg = split(";", $first_arg);
					$table = $first_arg[0];
					$opts = $first_arg[1];
					$opts = split(":",$opts);
					$ret_fld = $opts[1];
					$field_list = $this->getFieldsFor($table);

					if(is_array($args[2]))
						{
							$args = $args[2];
						}
					elseif(count($args) == 2)
						{
							$args = split(";", $args[1]);
						}
					else
						{
							$args = array_splice($args,1);
							foreach($args as $arg)
								{
									$temp_arg = split("=",$arg);
									$args2[$temp_arg[0]] = $temp_arg[1];
								}
							$args = $args2;
						}
					$flds = "";
					$vals = "";
					if(array_keys($args))
						{
							foreach($args as $fld=>$val)
								{
									if(in_array($fld,$field_list) && (!($_FILES['$fld']['name'])))
										{
											if(is_array($val))
												$val = join(";", $val);
											$query .= ($query ? "," : "")."`$fld`='$val'";
										}

								}
						}
					else
						{
							foreach($args as $arg)
								{
									$temp_arg = split("=",$arg);
									$fld = $temp_arg[0];
									if(in_array($fld,$field_list) && (!($_FILES['$fld']['name'])))
										{

											$val = str_replace("'","",$temp_arg[1]);
		//									$flds .= $flds ? ",".$fld : $fld;
											$query .= ($query ? "," : "")."`$fld`='".addslashes($val)."'";
		//									$vals .= $vals ? ",'".$val."'" : "'".$val."'";
										}
								}
						}
					$sql = "UPDATE $table SET $query where $condition";
					$this->query($sql);
					echo mysql_error();
					if($ret_fld)
						{
							//$ret_sql = "Select " . str_replace("return","",str_replace("_","",$opts[0]))."($ret_fld) from $table";
							//$ret_val = $this->field_val_from_sql($ret_sql);
							//return $ret_val;
						}
				}

			// Function to replace a pirticular string in each row in all fields of the table.
		   	// created by binson  asif

	         function replace_string_in_entire_table($tablename,$fields,$existing_string,$replace_string)
			 {

			 		$flds= $this->getFieldsForSQL("SELECT $fields from $tablename");
					echo mysql_error();
					foreach($flds as $fld)
					{
						$sql="update $tablename set $fld = replace ($fld, '$existing_string', '$replace_string')";
						mysql_query($sql);
					}

			  		return mysql_error();
			 }

		}
?>

IP2Country

2.10.2007 | Author:Black iD Team

Hello PHP Developers,

One more innovation from Black iD Solutions. We are developing an IP2Country database. I know this is a very common application. But our target users are those PHP developers who find it difficult to get the IP information for their users. For eg. If you wanted to know the country from the ipaddress, you would have to do either of the 2.

1. Buy a costly subscription from some of the sites
2. Download the entire IP2Country Database from

But now we have come up with a way you can easily get an access of the country information from the IP Address. The method is simple.

<?php
$f = fopen( "http://scripts.blackidsolutions.com/ip2country/?ip=202.186.13.4&get=country","r");
$country = fread($f,40); // Additional reading capability to ensure the country's name is not cut
echo $country; // Displays MALASIA
?>

By using the simple code you can get the country of visitor from the IP address. I will be publishing more information on this too. To get the 2 letter country code simply replace “&get=country” with “&get=ctry“. Also to get the 3 letter country code you can replace it by “cntry”.

Hope this helps.

Thanks

Jyot

What can you find here?

2.08.2007 | Author:Black iD Team

Hello again,

I have started off this new section for you so that I can give you whatever I have and whatever I have made. I have been programming PHP for more than an year now. I must say that my style of coding has changed a lot. I used to be a procedural programmer earlier as I had begun my career from a VB 6 background. But when I entered the web world, I came in contact with the PHP language (know how). Then with a few sore comments from my clients, I came down to the world of hard core professional PHP programming. The world had no place for procedural programmers. So I had to adapt myself to OOPHP. I am glad I did that. Some bad experiences are worth having. Since then I have struggled hard to reach a real “professional programmer’s” level. Though I am quite far off the destination, atleast I know now that I am on the right track.

Now with more and more exposure to the web world, I came across my second tool, MySQL. I have written a class in PHP to handle databases for mysql. Using this class, what I believe, should ease off much of development time. In brief you should be having such a code in the class.


$db = new HTMLdb;
$db->connect('dbhost','username','password','dbname');
$db->table_array_from_tablename('tablename');
/* returns a keyed array of the entire table structure. something like this
Array(0=>Array('fld1'=>'fld1_row1','fld2'=>'fld2_row1'),
1=>Array('fld1'=>'fld1_row2','fld2'=>'fld2_row2')...)
*/

some other methods include

  • table_array_from_sql
  • flat_field_array_from_sql
  • associative_field_array
  • generateHTMLForm
  • generateHTMLUpdateForm
  • Insert
  • Update

And many more. I will be releasing the first version of this database class soon. Note that this class is currently useful for only MySQL database system. We will be working on the other database systems soon.