Category: PHP


Check Domain Name Availability

Hi Guys to check the domain available or not and get it implement on PHP in your own site, you must use whois api.

1. Create a developer account on WHOIS.
2. The use following code to use in your PHP script

$contents = file_get_contents("http://www.whoisxmlapi.com//whoisserver/WhoisService?domainName=test.in&cmd=GET_DN_AVAILABILITY&username=username&password=password&outputFormat=JSON");
$contents = str_replace('\\', '', $contents);

$res=json_decode($contents);

$domainInfo = $res->DomainInfo;
if($domainInfo){
echo “Domain name: ” . print_r($domainInfo->domainName,1) .”
“;
echo “Domain Availability: ” .print_r($domainInfo->domainAvailability,1) .”
“;

//print_r($domainInfo);
}
?>

Try it…

ere we have a table named car and we want to calculate how many NULL values are there in ‘check’ and ‘flag’ fields

CREATE TABLE IF NOT EXISTS `car` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`brand` varchar(255) NOT NULL,
`check` int(1) DEFAULT NULL,
`flag` int(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


– Dumping data for table `car`

INSERT INTO `car` (`id`, `brand`, `check`, `flag`) VALUES
(1, ‘Porsche’, NULL, 1),
(2, ‘Renault’, 1, NULL),
(3, ‘sadsa’, NULL, 1),
(4, ‘asdasd’, NULL, NULL);

Query run
————————–
select
sum(@chk:=if(ISNULL(`check`),0,@chk)+1) as CHKTOTAL,
sum(@flg:=if(ISNULL(`flag`),0,@flg)+1) as FLGTOTAL
FROM `car`

Returns
————————-
CHKTOTAL FLGTOTAL
3.000000 2.000000

How to get last 10 working days

$i=1;
$fromdate = date(‘Y-m-d’);
//$todate = date(‘Y-m-d’);
$todate = date(‘Y-m-d’, mktime(0, 0, 0, date(“m”) , date(“d”)-1, date(“Y”)));
list($y, $m, $d) = split(“-”, $todate);
$weekends = array(‘Sunday’, ‘Saturday’);

$tenDateArray = array();
while($i<=10)
{
$day = date(‘l’, mktime(0, 0, 0, $m, $d, $y));
if(!in_array($day, $weekends))
{
$i++;
$tenDateArray[] = date(‘Y-m-d’, strtotime(‘-0 day’, strtotime($todate)));
}
$todate = date(‘Y-m-d’,strtotime(‘-1 day’, strtotime($todate)));
list($y, $m, $d) = split(“-”, $todate);
}
$to10date = $tenDateArray[max(array_keys($tenDateArray))];

Add working days

Hi, need to create a function which return week working days and also add (optional) if require :

function getAddDate($date,$day=0)
{
$dates = explode(“-”, $date);

$tomorrow = mktime(0, 0, 0, $dates[1] , $dates[2]+$day, $dates[0]);
$day = date(‘D’, $tomorrow);
if($day==’Sat’)
$day_inc = 3;
if($day==’Sun’)
$day_inc = 2;
return date(‘Y-m-d’, mktime(0, 0, 0, $dates[1] , $dates[2]+$day_inc, $dates[0]));
}

getAddDate(‘YYYY-mm-dd’,1);
It work out..

To protect your website from malware attack I per form follow a few steps and get protect website from further attack.

Clean your files or upload clean files and use following line in your.htaccess file for further prevention of iframe /javascript injection.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^.*(;|<|>|’|”|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*(/\*|union|select|insert|cast|set|declare|drop|update|md5|benchmark).* [NC]
RewriteRule .* – [F]

This code is generally uploaded through FTP from infected PC, please scan your PC and change your FTP / control panel password asap.

Feel free to post comment for any query..

SFTP PHP file handling class

PHP class for SFTP Connection and file operation, set email on various steps to debug

class SFTPConnection
{
private $connection;
private $sftp;

var $headers  = “MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\nTo: Tarun <name@mydomain.com>\r\nFrom: <admin@mydomain.com>\r\n”;

public function __construct ($host, $port=22)
{
$message = “Connectivity started”;
$subject = “This is to confirm that the connectivity started but not done”;
//mail ($this->to, $subject, $message, $this->headers);

$this->connection = ssh2_connect ($host, $port);
if (! $this->connection)
{
$message = “Connectivity done”;
$subject = “Could not connect to $host on port $port.”;

//mail ($this->to, $subject, $message, $this->headers);

throw new Exception (“Could not connect to $host on port $port.”);
}
else
{
$message = “Connectivity Success”;
$subject = “success connect to $host on port $port.”;
mail ($this->to, $subject, $message, $this->headers);
}

//mail($this->to, “test”, “Test “, $this->headers);

}

public function login ($username, $password)
{
if (! @ssh2_auth_password ($this->connection, $username, $password))
{
$message = “Could not authenticate with username $username ” .
“and password $password.”;
$subject = “connection error”;
//mail ($this->to, $subject, $message, $this->headers);

throw new Exception (“Could not authenticate with username $username ” .
“and password $password.”);
}

$this->sftp = @ssh2_sftp ($this->connection);
if (! $this->sftp)
{
$message = “Could not initialize SFTP subsystem.”;
$subject = “initialization error”;
//mail ($this->to, $subject, $message, $this->headers);
throw new Exception(“Could not initialize SFTP subsystem.”);
}

}

public function createFile ($newfilepath)
{
$sftp = $this->sftp;
@ssh2_sftp_mkdir ($sftp, $newfilepath);
}

public function uploadFile ($local_file, $remote_file)
{
$sftp = $this->sftp;
$stream = fopen (“ssh2.sftp://$sftp$remote_file”, ‘w+’);

if  (! $stream)
{
$message = “Could not open file: $remote_file”;
$subject = “error1″;
//mail ($this->to, $subject, $message, $this->headers);
throw new Exception (“Could not open file: $remote_file”);
}

$data_to_send = @file_get_contents ($local_file);

if  ($data_to_send === false)
{
$message = “Could not open local file: $local_file”;
$subject = “error2″;
//mail ($this->to, $subject, $message, $this->headers);
throw new Exception (“Could not open local file: $local_file.”);
}

if  (@fwrite ($stream, $data_to_send) === false)
{
$message = “Could not send data from file: $local_file”;
$subject = “error3″;
mail ($this->to, $subject, $message, $this->headers);
throw new Exception (“Could not send data from file: $local_file.”);
}
else
{
$message = “File transferd success”;
$subject = “correct”;
//mail ($this->to, $subject, $message, $this->headers);

$success = TRUE;
}
@fclose ($stream);
return $success;
}
}

Checkout various functions…

Use Bake for Cake PHP

The most basic functions class you can develop with bake if you are using cakePHP framework.

First do following in your PHP for Cake :

  • Change the config file of php/php.ini : register_globals = On
  • Enable mod rewrite in apache/conf/httpd.conf : remove # from begining,
    LoadModule rewrite_module modules/mod_rewrite.so

Following easy steps to get the bake “myproject” is name :

  • Define database configuration in myproject/app/config/

in dateabase.php (that you get on new downloaded files)

  • Then set the system path variable:

;c:\xampp\php;c:\xampp\htdocs\myproject\cake\console\

you can find it

- My Computer [properties]
– Advanced
— Environment Variables
—- System Variables

Find for Path and append above path as per your folder.

Now run on command prompt

cd c:\xampp\htdocs\myproject\
c:\xampp\htdocs\myproject\>cake\console\cake bake

It simply run the bake file and menu ridden program comes you can make model, view and controller by  following the simple questions.

Hope this help you out..

How to use .HTACCESS

Hi frnd’s during a regular project I setup .htaccess file with the following code for general rewrite with name :

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^mylink/(.*)$ myfile.php?request_variable_name=$1

RewriteRule ^(.*)/mylink2/(.*).html$ mylinkfile2.php?request_variable_name1=$1&static_request_variable_name=value&request_variable_name2=$2 [NC]

———————————————–

So in the above ex. I try to rewrite mylink – seo url with the slug (the request variable) to take there values in myfile.php with the request variable names the only trick is $1, $2 that to be taken as value.

Excel parsing using PHP

Hi all, I go through a regular search to parse excell file using PHP and finally I get the solution.

For this we need – excel foder, contains – oleread.inc and reader.php files.

<?

if(is_uploaded_file($_FILES['data']['tmp_name']))

{
$file_type_arr=explode(“.”,$_FILES['data']['name']);
if($file_type_arr[1]==”xls”)

{
ini_set(‘session.gc_maxlifetime’,86400);
require_once ‘Excel/reader.php’;

// ExcelFile($filename, $encoding);
$data = new Spreadsheet_Excel_Reader();

// Set output Encoding.
$data->setOutputEncoding(‘CP1251′);

$data->read($_FILES['data']['tmp_name']);

//Do the rest work with excell data

}

}

?>

Download – reader.php the parsing file.

/*** How to create directory in remote server ***/
$ftp_server = “server”;
$ftp_user = “user”;
$ftp_pass = “pass”;

// set up a connection or die
$conn_id = ftp_connect($ftp_server);

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
// echo “Connected as $ftp_user@$ftp_server\n”;
} else {
// echo “Couldn’t connect as $ftp_user\n”;
}

$dir=$rootpath.$dirname; // $rootpath = /public_html/folder

// check for already exist else create
if (!@ftp_chdir($conn_id, $dir)) {
ftp_mkdir($conn_id, $dir);            // to create directory
ftp_chmod($conn_id, 0777, $dir);    // change mode to editable
}

// close the connection
ftp_close($conn_id);

Powered by WordPress. Theme: Motion by 85ideas.