Thursday, October 17, 2013

installed Xdebug on a Fedora 17 OS

I installed Xdebug on a Fedora 17 OS in order to use it with netbeans 7.3.1 It took me some time so I share with you how did I managed to have it working:
Install Xdebug:
sudo yum install php-pecl-xdebug
Add this lines to php.ini at the end of the file:
sudo vi /etc/php.ini
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

Add the same lines to xdebug.ini file:
sudo vi /etc/php-zts.d/xdebug.ini
xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000
Allow port for Xdebug
sudo semanage port -a -t http_port_t -p tcp 9000
Now find your netbeans.conf file in your system:
sudo find / -name netbeans.conf
Now, open up the file and add the text "-J-Dorg.netbeans.modules.php.dbgp.level=400" in netbeans_default_options so that the line looks somewhat like this:
netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dnetbeans.logger.console=true -J-ea -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.zip.disableMemoryMapping=true -J-Dsun.awt.disableMixing=true -J-Dnetbeans.extbrowser.manual_chrome_plugin_install=yes -J-Dorg.netbeans.modules.php.dbgp.level=400"
Restart both NetBeans IDE and apache:
sudo service httpd restart
if not working then use

SQLSTATE[HY000]: General error: 1 Can't create/write to file '/tmp

You messed up with /tmp permissions. To fix:
chown root:root /tmp (root:root is mysql username and password which you used)
chmod 1777 /tmp 
/etc/init.d/mysqld restart (mysqld service restart)

Tuesday, September 10, 2013

[SOLVED] How to fix filesystem error - Fedora

/dev/mapper/volgroup-lv_root contains a filesystem with errors checks forced

run below command

# /dev/mapper/volgroup-lv_root
# fsck -f  /dev/mapper/volgroup-lv_root

Thursday, September 5, 2013

MySql backup with php script

<?php

    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($database_name,$link);

    $tableName  = '(select * from table_name where date(created_at) = "2013-09-04")';
  //$tableName = 'table_name';
    $backupFile = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'backup.sql';

    $query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";

    $result = mysql_query($query);

    echo "backup complete";

    mysql_close($link);
    $link = null;

?>

Restore table script

<?php
 

    $link = mysql_connect($host,$user,$pass);
     mysql_select_db($database_name,$link);

    $tableName  = 'table_name';
    $backupFile = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'backup.sql';

    $query      = "LOAD DATA INFILE '$backupFile' INTO TABLE $tableName";
    $result = mysql_query($query);

    echo "backup restore complete";

    mysql_close($link);
    $link = null;

?>

Monday, August 12, 2013

X-Debug in php 5.4.17


I installed Xdebug on a Fedora 17 OS in order to use it with netbeans 7.3.1 It took me some time so I share with you how did I managed to have it working:

Install Xdebug:

sudo yum install php-pecl-xdebug

Add this lines to php.ini at the end of the file:

sudo vi /etc/php.ini

zend_extension=/usr/lib64/php/modules/xdebug.so

xdebug.remote_enable=1

xdebug.remote_handler=dbgp

xdebug.remote_mode=req

xdebug.remote_host=127.0.0.1

xdebug.remote_port=9000

Add the same lines to xdebug.ini file:

sudo vi /etc/php-zts.d/xdebug.ini


xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000

Allow port for Xdebug

sudo semanage port -a -t http_port_t -p tcp 9000

Now find your netbeans.conf file in your system:

sudo find / -name netbeans.conf


Now, open up the file and add the text "-J-Dorg.netbeans.modules.php.dbgp.level=400" in netbeans_default_options so that the line looks somewhat like this:


netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dnetbeans.logger.console=true -J-ea -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.zip.disableMemoryMapping=true -J-Dsun.awt.disableMixing=true -J-Dnetbeans.extbrowser.manual_chrome_plugin_install=yes -J-Dorg.netbeans.modules.php.dbgp.level=400"


Restart both NetBeans IDE and apache:

sudo service httpd restart

if not working then use

xdebug.remote_host=127.0.0.1 to ip address of computer

Saturday, March 23, 2013

how to install mongo db

Install mongo db step in centos/fedora

first check your os (32/64)
uname -m

Create a /etc/yum.repos.d/10gen.rep

open file and add bellow line in file and save the file (64bit os)
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1

open file and add bellow line in file and save the file (62bit os)
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686
gpgcheck=0
enabled=1

~]# yum install mongo-10gen mongo-10gen-server

~]# service mongo db start

~]# chkconfig --levels 235 mongod on

// step for install php mongo drive

~]# yum install php-devel git httpd

~]# export PHP_AUTOCONF=/usr/bin/autoconf

~]# export PHP_AUTOHEADER=/usr/bin/autoheader

~]# wget http://pecl.php.net/get/mongo-1.0.9.tgz

~]# tar -xzf mongo-1.0.9.tgz

~]# cd mongo-1.0.9

~]# phpize

~]# ./configure

~]# make install

~]# cd /etc/php.d

~]# vi mongo.ini

add bellow line in mongo.ini
extension=mongo.so

~]# service httpd restart

//test mongo db in prompt
//strat mongo
~]# mongo

//databse test use
>use test;

>doc = {"hello": "world", "php": "is alive!" };
>db.phptest.save(doc);
>db.phptest.findOne();

Example in php

<?php
$connection = new Mongo();
$db = $connection->test;
$collection = $db->phptest;

$obj = $collection->findOne();
echo "Hello " . $obj["hello"] . "!";

echo "Show result as an array:";
echo "<pre>";
print_r($obj);
echo "</pre>";

echo "Show result as JSON:";
echo "<pre>";
echo json_encode($obj);
echo "</pre>";


?>

install rockmongo for web view

download from rockmongo.com/downloads

extract file in web directory

create virtualhost


Friday, February 8, 2013

सरफ़रोशी की तमन्ना (Sarfaroshi ki Tamanna)

सरफ़रोशी की तमन्ना (Sarfaroshi ki Tamanna)

सरफ़रोशी की तमन्ना अब हमारे दिल में है
देखना है ज़ोर कितना बाज़ू-ए-क़ातिल में है

ऐ वतन, करता नहीं क्यूँ दूसरी कुछ बातचीत,
देखता हूँ मैं जिसे वो चुप तेरी महफ़िल में है
ऐ शहीद-ए-मुल्क-ओ-मिल्लत, मैं तेरे ऊपर निसार,
अब तेरी हिम्मत का चरचा ग़ैर की महफ़िल में है
सरफ़रोशी की तमन्ना अब हमारे दिल में है

वक़्त आने पर बता देंगे तुझे, ए आसमान,
हम अभी से क्या बताएँ क्या हमारे दिल में है
खेँच कर लाई है सब को क़त्ल होने की उमीद,
आशिकों का आज जमघट कूचा-ए-क़ातिल में है
सरफ़रोशी की तमन्ना अब हमारे दिल में है

है लिए हथियार दुश्मन ताक में बैठा उधर,
और हम तैयार हैं सीना लिए अपना इधर.
ख़ून से खेलेंगे होली अगर वतन मुश्क़िल में है
सरफ़रोशी की तमन्ना अब हमारे दिल में है

हाथ, जिन में है जूनून, कटते नही तलवार से,
सर जो उठ जाते हैं वो झुकते नहीं ललकार से.
और भड़केगा जो शोला सा हमारे दिल में है,
सरफ़रोशी की तमन्ना अब हमारे दिल में है

हम तो घर से ही थे निकले बाँधकर सर पर कफ़न,
जाँ हथेली पर लिए लो बढ चले हैं ये कदम.
ज़िंदगी तो अपनी मॆहमाँ मौत की महफ़िल में है
सरफ़रोशी की तमन्ना अब हमारे दिल में है

यूँ खड़ा मक़्तल में क़ातिल कह रहा है बार-बार,
क्या तमन्ना-ए-शहादत भी किसी के दिल में है?
दिल में तूफ़ानों की टोली और नसों में इन्कलाब,
होश दुश्मन के उड़ा देंगे हमें रोको न आज.
दूर रह पाए जो हमसे दम कहाँ मंज़िल में है,
सरफ़रोशी की तमन्ना अब हमारे दिल में है

वो जिस्म भी क्या जिस्म है जिसमे न हो ख़ून-ए-जुनून
क्या लड़े तूफ़ान से जो कश्ती-ए-साहिल में है
सरफ़रोशी की तमन्ना अब हमारे दिल में है
देखना है ज़ोर कितना बाज़ू-ए-क़ातिल में

---------------------------------------------------------------------
- By Ram Prasad Bismil


सरफरोशी की तमन्ना अब हमारे दिल में है,
देखना है जोर कितना बाजुए कातिल में है ।
करता नहीं क्यों दुसरा कुछ बातचीत,
देखता हूँ मैं जिसे वो चुप तेरी महफिल मैं है ।
रहबर राहे मौहब्बत रह न जाना राह में
लज्जत-ऐ-सेहरा नवर्दी दूरिये-मंजिल में है ।
यों खड़ा मौकतल में कातिल कह रहा है बार-बार
क्या तमन्ना-ए-शहादत भी किसी के दिल में है ।
ऐ शहीदे-मुल्को-मिल्लत मैं तेरे ऊपर निसार
अब तेरी हिम्मत का चर्चा ग़ैर की महफिल में है ।
वक्त आने दे बता देंगे तुझे ऐ आसमां,
हम अभी से क्या बतायें क्या हमारे दिल में है ।
खींच कर लाई है सब को कत्ल होने की उम्मींद,
आशिकों का जमघट आज कूंचे-ऐ-कातिल में है ।
सरफरोशी की तमन्ना अब हमारे दिल में है,
देखना है जोर कितना बाजुए कातिल में है ।
__
रहबर - Guide
लज्जत - tasteful
नवर्दी - Battle
मौकतल - Place Where Executions Take Place, Place of Killing
मिल्लत - Nation, faith
-------------------------------------------------------------------------------------

 'Rang De Basanti'. Here it goes -- though remember these lines are not part of the original poem written by 'Ram Prasad Bismil'.

है लिये हथियार दुश्मन ताक मे बैठा उधर
और हम तैय्यार हैं सीना लिये अपना इधर
खून से खेलेंगे होली गर वतन मुश्किल में है
सरफरोशी की तमन्ना अब हमारे दिल में है
हाथ जिनमें हो जुनून कटते नही तलवार से
सर जो उठ जाते हैं वो झुकते नहीं ललकार से
और भडकेगा जो शोला सा हमारे दिल में है
सरफरोशी की तमन्ना अब हमारे दिल में है
हम तो घर से निकले ही थे बांधकर सर पे कफ़न
जान हथेली में लिये लो बढ चले हैं ये कदम
जिंदगी तो अपनी मेहमान मौत की महफ़िल मैं है
सरफरोशी की तमन्ना अब हमारे दिल में है
दिल मे तूफानों की टोली और नसों में इन्कलाब
होश दुश्मन के उडा देंगे हमे रोको न आज
दूर रह पाये जो हमसे दम कहाँ मंजिल मे है
सरफरोशी की तमन्ना अब हमारे दिल में है

Monday, January 21, 2013

Reading a Remote File Using PHP

To read a remote file from php you have at least four options :
1. Use fopen()
2. Use file_get_contents()
3. CURL
4. Make your own function using php's socket functions.
First i need to warn you about something. You can only use fopen() and file_get_contents() when fopen wrappers is enabled. This parameter is specified in the php.ini file and cannot be changed at run time using ini_set(), To know whether you can use these two or not you can use the following code to check the value of fopen wrapper seting.
if (ini_get('allow_url_fopen') == '1') {
   // use fopen() or file_get_contents()
} else {
   // use curl or your custom function
}

1 . Using fopen()

If you use fopen() to read a remote file the process is as simple as reading from a local file. The only difference is that you will specify the URL instead of the file name. Take a look at the example below :
// make sure the remote file is successfully opened before doing anything else
if ($fp = fopen('http://www.google.com/', 'r')) {
   $content = '';
   // keep reading until there's nothing left
   while ($line = fread($fp, 1024)) {
      $content .= $line;
   }
   // do something with the content here
   // ...
} else {
   // an error occured when trying to open the specified url
}
Now, the code above use fread() function in the while loop to read up to 1024 bytes of data in a single loop. That code can also be written like this :
// make sure the remote file is successfully opened before doing anything else
if ($fp = fopen('http://www.google.com/', 'r')) {
   $content = '';
   // keep reading until there's nothing left
   while ($line = fgets($fp, 1024)) {
      $content .= $line;
   }
   // do something with the content here
   // ...
} else {
   // an error occured when trying to open the specified url
}
instead of fread() we use fgets() which reads one line of data up to 1024 bytes. The first code is much more preferable than the second though. Just imagine if the remote file's size is 50 kilobytes and consists of 300 lines. Using the first code will cause the loop to be executed about fifty times but using the second the loop will be executed three hundred times.
If you consider the cost to call a function plus the time required to make 300 requests compared to just 5 then clearly the first one is the winner.

2. Using file_get_contents()

This is my favorite way of reading a remote file because it is very simple. Just call this function and specify a url as the parameter. But make sure you remember to check the return value first to determine if it return an error before processing the result
$content = file_get_contents('http://www.google.com/');
if ($content !== false) {
   // do something with the content
} else {
   // an error happened
}

3. CURL

Unlike the two methods above using CURL cannot be said as straigthforward. Although this library is very useful to connect and communicate with may different protocols ( not just http ) it requires more effort to learn. And another problem is that not all web host have this library in their php installation. So we better make sure to check if the library is installed before trying to use it.
Here is a basic example on fetching a remote file
// make sure curl is installed
if (function_exists('curl_init')) {
   // initialize a new curl resource
   $ch = curl_init();

   // set the url to fetch
   curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');

   // don't give me the headers just the content
   curl_setopt($ch, CURLOPT_HEADER, 0);

   // return the value instead of printing the response to browser
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   // use a user agent to mimic a browser
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');

   $content = curl_exec($ch);

   // remember to always close the session and free all resources
   curl_close($ch);
} else {
   // curl library is not installed so we better use something else
}
In some cases using CURL is faster than using file_get_contents() or fopen(). This is because CURL handles compression protocols by default ( for example gzip ). Many sites, big and small, use gzip compression to compress their web pages in order to save bandwidth. This site, for example, also use gzip compression which cut the bandwidth used into half. So if you're the type who just can't wait CURL will fit you most.


4. Custom functions

In the worst case your server will have fopen wrappers disabled and don't have CURL library installed. In this sad situation you just have to make your own way.
Our function shall be named getRemoteFile() which takes only one parameter, the url for the remote file. The skeleton for this function is shown below
function getRemoteFile($url)
{
   // 1. get the host name and url path

   // 2. connect to the remote server

   // 3. send the necessary headers to get the file

   // 4. retrieve the response from the remote server

   // 5. strip the headers

   // 6. return the file content
}
To extract the host name and url path from the given url we'll use parse_url() function. When given a url this function will spit out the followings :
  • scheme
  • host
  • port
  • user
  • pass
  • path
  • query
  • fragment
For example, if the url is http://www.php-mysql-tutorial.com/somepage.php then parse_url() will return :
Array
(
    [scheme] => http
    [host] => www.php-mysql-tutorial.com
    [path] => /somepage.php
)
and if the url is http://myusername:mypassword@www.php-mysql-tutorial.com/somepage.php?q=whatsthis#ouch then parse_url() will return this :
Array
(
    [scheme] => http
    [host] => www.php-mysql-tutorial.com
    [user] => myusername
    [pass] => mypassword
    [path] => /somepage.php
    [query] => q=whatsthis
    [fragment] => ouch
)
For our new function we only care about the host, port, path and query.
To establish a connection to a remote server we use fsockopen(). This function requires five arguments, the hostname, port number, a reference for error number, a reference for the error message and timeout
function getRemoteFile($url)
{
   // get the host name and url path
   $parsedUrl = parse_url($url);
   $host = $parsedUrl['host'];
   if (isset($parsedUrl['path'])) {
      $path = $parsedUrl['path'];
   } else {
      // the url is pointing to the host like http://www.mysite.com
      $path = '/';
   }

   if (isset($parsedUrl['query'])) {
      $path .= '?' . $parsedUrl['query'];
   }

   if (isset($parsedUrl['port'])) {
      $port = $parsedUrl['port'];
   } else {
      // most sites use port 80
      $port = '80';
   }

   $timeout = 10;
   $response = '';    // connect to the remote server
   $fp = @fsockopen($host, $port, $errno, $errstr, $timeout );
   if( !$fp ) {
      echo "Cannot retrieve $url";
   } else {
      // send the necessary headers to get the file
      fputs($fp, "GET $path HTTP/1.0\r\n" .
                 "Host: $host\r\n" .
                 "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3\r\n" .
                 "Accept: */*\r\n" .
                 "Accept-Language: en-us,en;q=0.5\r\n" .
                 "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
                 "Keep-Alive: 300\r\n" .
                 "Connection: keep-alive\r\n" .
                 "Referer: http://$host\r\n\r\n");
      // retrieve the response from the remote server
      while ( $line = fread( $fp, 4096 ) ) {
         $response .= $line;
      }
      fclose( $fp );

      // strip the headers
      $pos      = strpos($response, "\r\n\r\n");
      $response = substr($response, $pos + 4);
   }
   // return the file content
   return $response;
}
The code above sends nine lines of headers but only the first two is mandatory. So even if you send only these
fputs($fp, "GET $path HTTP/1.0\r\n" .
           "Host: $host\r\n\r\n");
the function will likely be working correctly. Not always though. Since the file is stored in a remote server It really up to that server to reply to your request or not. Some people code their page to block any request without the proper referer header. Some will only accept a specific user agent. Other will require cookies set in the header.
If you want to see what headers should be sent to successfully fetch a specific remote file try using firefox plus live http headers plugin. It's really a useful little tool

how to see xml preview in browser php

xml preview view in browser $xml = new DOMDocument('1.0', 'UTF-8');                 $xml->preserveWhiteSpace = false; ...