Sunday, April 14, 2013

Create Simple Php Search Engine

Creating search engine in php was so easy and joyful . The only thing needed is little understanding 
mind and you should know php as well as sql . This script was based on an idea of how search engine works .
This script was the model based on google , yahoo , and other search engines.

           FILES TO CREATE
     1. db.php
     2. seacrh.php
     3. style.css

Creating Database For This Search Engine

1. First go to the phpMYadmin . 2. Create the database to insert the sql . 3. Click the sql link and paste the following sql lines.

 CREATE TABLE search (
title TEXT NOT NULL ,
des TEXT NOT NULL
) ENGINE = MYISAM ;

INSERT INTO search(title , des) VALUES('Hallow world','hallow world test search engine');

INSERT INTO search(title , des) VALUES('The world is best ','zet , queen , cat , dog , horse');
INSERT INTO search(title , des) VALUES('title is best','Insert your description here');

db.php

Replace the database host , database user , database password , database name with your database informations.

<?php
$database_host = "localhost";
$database_user = "root";
$database_password = "";
$database_name = "test";
mysql_connect($database_host , $database_user , $database_password);
mysql_select_db($database_name);
?>

search.php 

<?php
include('db.php');
if(isset($_REQUEST['search'])){
$search_word = $_REQUEST['search'];
$sql = "SELECT * FROM search WHERE title LIKE '%$search_word%' OR des LIKE '%$search_word%'";
$q = mysql_query($sql);
$num = mysql_num_rows($q);
echo mysql_error();
if($num > 0){
echo '<div id="result">';
while($data = mysql_fetch_array($q)){
echo '<div id="boxer"><font color="blue">'.$data['title'].'</font><br>';
echo $data['des'].'</div><br><br>';
}
echo '</div>';
}
else echo '<center> DATA NOT FOUND ON THE SERVER </center>';
}
?>

Form security script : Php Image Captcha

 
 
You may see the image captcha in the forms when you signup in google , wordpress or other networks.
Do you ever think how this images are generated and how it is useful . First security , it give high security to 
protect forms and website from hackers. Second how it works, it will be cool and exiting ! please view the code
below and understand.

       THINGS NEEDED
       1. comic.tff

      FILES TO CREATE 
      1. captca.php
      2. form.html
 

captcha.php 

<?php
session_start();
define('font' , 'comic.ttf');
function generate($characters) {
$possible = '23456789ABCDEFGHIJKL!';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;}
 
function Captcha($width='120',$height='40',$characters='5') {
$code = generate($characters);
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
$background_color = imagecolorallocate($image, 225, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 160, 149, 249);
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);}
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
$textbox = imagettfbbox($font_size, 0, font, $code);
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, font , $code);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['captcha'] = $code; }
Captcha();
?>

form.html 

<form method="post" action="check.php">
NAME : <input name="name">
captch : <img src="captcha.php"> <input name="cap_code">
<br><input type="submit" value="submit">
</form>

THINGS TO REMEMBER

The captcha image will save the code in the user browser in the name captcha. To retrive the data use this script.
 

check.php 

<?php
session_start();
if(isset($_SESSION['captcha'])) {
if($_POST['cap_code']==$_SESSION['captcha']){
echo '<font color="green">correct captcha</font>';
}
else echo '<font color="red">worng captcha</font>';
}
?>
 

Get latitude and longitude from an address



This blog will describe you how to get latitude and longitude from a ip address with the support of the Google. It also provides the user location but in this blog i had described only getting latitude and longitude. 

ks_lat-long.php 
 
<?php
function ks_get_add($ip_address){
if (!is_string($address))die("All Addresses must be passed as a string");
$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
$_result = false;
if($_result = file_get_contents($_url)) {
if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
$_coords['lat'] = $_match[1];
$_coords['long'] = $_match[2];
}
return $_coords;
}
?> 

Introduction : Pencil Lab


Hello world this is kamesh the owner of pencil lab . This is a simple introduction page about me and pencil lab .

We would write blog about php , javascript , web development and lot more ..