Monday, April 15, 2013

Username Avialablity Checker With Php And Jquery

 
This post about Twitter used jQuery plug-in JavaScript code in registration page  username Availability check and update Screen name.

        Here is the example of a user name availability checker , which was used now a days by most websites . I have a best 
 idea to make it with php , jquery and ajax. I think this script would make your knowledge shine .
		   
		FILES TO CREATE
		1. check.js
		2. check_user.php
		3. index.html
		4. db.php
		5. style.css

Creating Database For This Username Availability Checker

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 IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) NOT NULL,
  `password` varchar(250) NOT NULL,
  `email` varchar(250) NOT NULL,
  `website` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
 
INSERT INTO `user` (`id`, `name`, `password`, `email`, `website`) VALUES
(1, 'admin', 'admin', 'admin@localhost.com', 'localhost.com'); 

check.js

$('#name').change(function(){ $('#user_info').html(' <img src="load.gif"> checking whether name is available'); var name = $('#name').val(); $.ajax({ url:'check_user.php?name='+name, success: function(html) { $('#user_info').html(''); if(html == "true") { $('#user_info').html(' <font id="ok" color="green"> <img style="position:absolute;margin-top:2px" src="ok.png"> Available </font>'); } else { $('#user_info').html(' <font id="wrong" color="red"> <img style="position:absolute;margin-top:2px" src="wrong.png"> Not Available </font>'); } } }); });


check_user.php 

<?php require('db.php'); if(isset($_GET['name'])){ $name = $_GET['name']; $sql = "select * from user where name='$name'"; $q = mysql_query($sql); if(mysql_num_rows($q) > 0) echo 'false'; else echo 'true'; } ?>
 

index.php


<link rel="stylesheet" type="text/css" media="all" href="style.css">
<script src="jquery.min.js"></script>
<pre>
<form method="post" id="form">
<div id="comic"> <font color="skyblue">KAMESH</font><font color="orange">soft</font> SIGNUP HERE</div>
  <input id="name" name="name" placeholder="name" ><i id="user_info"></i>
  <input name="pass" placeholder="password">
  <input name="email" placeholder="email">
  <input name="website" placeholder="website"> <input id="bt" type="submit" value="Signup">
</form>
</pre>
<script src="check.js"></script>

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);
?>

style.css 

#form{border:1px solid #eeeeee;background:#4E7BA3;width:500px;} #comic{font-family:comic sans ms;color:white;font-size:28px;} input{color:gray} input#bt{background:#D1DBE5;;border:1px solid blue;padding:1px;color:black;cursor:pointer} #user_info{color:white;font-family:arial , verdian} #ok{padding:3px;color: #4F8A10;background-color:#EDFCED;border:1px solid green;} #wrong{padding:3px;color: #D8000C;background-color:#FDD5CE;border:1px solid red}

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