Extract Zip File Using PHP

Tags

, , ,


Sometime we need to extract a file on the server, say you don’t have access to shell and you want to extract a zip file, just put this to a php file and upload and run

     $zipObj = new ZipArchive;
     $file = $zipObj->open(’zip_file_name.zip’);
     if ($file == TRUE) {
         $zipObj->extractTo('extract_directory/');
         $zipObj->close();
         echo 'Unzip Successfully';
     } else {
         echo 'Failed to unzip';
     }

Its very simple and available idea everywhere, i jut wrote for note
Done!!! 🙂

Advertisements

SQL Error: 1030: Got error 139 from storage engine -solution

Tags

, ,


I developed a project and sometime later client faced that they are not able to saved data, so I checked database table, the table have more than 10 columns text type.

I googling and find out that table engine is InnoDB and InnoDB limitation row length of 8000 bytes, thats the problem,

I changed it to MyISAM and test, it working properly

 ALTER TABLE `table_name` ENGINE = MYISAM 

Its work with me now.

twitter api fetch twitter user feeds, manage and display on your website using php

Tags

, , , ,


I know this one is old thing and helps is available but sometime we need to fetch twitter feeds Retwittet, Mentions, to our websites. Now i used json request and format the texts and print.

 function twi_fid(){
  
  ## NUMBER OF TWITTET 
  $size = 5;
  
  ## FETCH THE TWITTER TWITTET
  $twittet = file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=bdsarwar&count=$size&include_rts=t&include_entities=t");
  
  
  $data = json_decode($twittet);
  $o_text = "";
  foreach($data as $t)
  {
      $o_text = $t->text;
      
      ## MAKING URLS ENABLE TO TEXT
      $entities = $t->entities;
      
      $i = 0;
      foreach($entities->urls as $url)
      {
          $indicate = $url->indices;

          $replace[$i] = $url->url;
          $string[$i] = "<a target='_blank' href='".$url->url."'>".$url->url."</a>";

          
          $i++;
      }
      
      ## MAKING MENTIONS 
      foreach($entities->user_mentions as $mention)
      {
          $indicate = $mention->indices;
          
          $string[$i] = "<a target='_blank' href='http://www.twitter.com/".$mention->screen_name."'>".$mention->screen_name."</a>";
          $replace[$i] = $mention->screen_name;
          
          $i++;
      }
     
      for($i = 0; $i < count($string); $i++)
      {
          
          $pattarn = $replace[$i];
          $o_text = str_replace($pattarn, $string[$i], $o_text);
     
      }
      
    echo $o_text;
    echo "<br />";
    echo date("d/m/Y, H:m a", strtotime($t->created_at));
    echo "<br/> ___ <br />";

      
  }
  }
  twi_fid();

For detail parameter please visit: http://apiwiki.twitter.com/w/page/22554679/Twitter-API-Documentation
and for user timeline json visit http://dev.twitter.com/doc/get/statuses/user_timeline

Done !!

use PHP print_r function and return a string

Tags

, , ,


print_r is one of the most useful feature of PHP, print_r prints a human-readable string representation of a variable. Sometime we need to get print_r value as sting to log or to combine the messages.

You can notice that print_r function have two argument first one is for string/array to be printed and another one use for whether it will print or return as string


#To have print_r return the data as a string, include the second argument "true":
$message = print_r($obj, true);

sometime you might be need to use var_export function instead of print_r

$msg = var_export($obj, true);

I use this several time while I debugging.

Happy Debugging 🙂

storing array to mysql and retrieve string and use it as variable using php

Tags

, , , ,


Sometime we need to store array to database then we need to retrieve and use as variable array. I used session variable to save data and used it later.

Its simple just use the following

#using serialize() method
$data = serialize($_SESSION);
$sql = "Insert into sessioninfo `data` values('$data')";

just run the above query and save the whole session data into a database table. You might be use any array here.

Now retrieve the data

# I assume you can retrieve the data from database and assign to the following variable
$data = unserialize($row['data']);

Now $data have the same value or nested array.

So just using serialize() and unserialize() we can do this.

Done !

how to install whm/cpanel and change php settings and make PHP DSO

Tags

, , ,


I used rackspace cloud to create a fresh new server with CentOS,

Login as root and used the following command

Configure the network and host
Open /etc/sysconfig/network and put HOSTNAME= your FQDN host name

[root@localhost ~]# nano /etc/sysconfig/network
HOSTNAME=server.domain.com

Update your host go to /etc/hosts

[root@localhost ~]# nano /etc/hosts

Change file to

127.0.0.1        localhost localhost.localdomain
67.12.145.20    host.domain.com  host

Now set hostname from command line

[root@localhost ~]# hostname host.domain.com

restart the network services

[root@localhost ~]# /etc/init.d/network restart

Update your server

[root@localhost ~]#  yum update

Install Perl

[root@localhost ~]#  yum install perl

Download WHM/cPanel installation files

[root@localhost ~]# wget http://layer1.cpanel.net/latest

Start cPanel installation, installation will take several hours so leave your ssh for several hours and check that your net connection is not down until installed it.
Note: when you install cPanel your ip must be authorized by cPanel, or you can registered for trial version, you can verify your ip address from http://www.cpanel.net/apps/verify/index.cgi

[root@localhost ~]# sh latest

You might me see the following message after finished the intall

cPanel Layer 2 Install Complete

Before going to web, you might get some firewall issue, so sometime you need to disable it, you maybe need to disable iptables use the following command

[root@localhost ~]# /etc/init.d/iptables stop

Web access: now you can access your cpanel from http://YOUR_IP:2026, or https://YOUR_IP:2087 and use user as root and your server root password. [YOUR_IP might me your ip or your domain address]

After login you need to give basic setup information of cPanel like network interfaces, nameserver configuration, FTP/Mail etc.

Now our target is to enable PHP DSO and add more features like GD, TTF, MbSring, IonCube etc.

You can see in software section of left navigation “Easy Apache (Apache Update)” click on it
Step 1 Profile : check previous profile and click on the Next
Step 2 Apache Version: select your required apache version and click on Next
Step 3 PHP Major Version: select your required major php version.
Step 4 PHP Minor Version: select your required minor php version.
Step 5 Short Options List: click on Exhaustive Options List and check all your require php settings

Now save your profile and build.

Build will take 40 min appropriately, wait for it. after finished the build You’ll have a popup confirmation box and you need to select DSO from dropdown. and save the configuration.

Now You have php with DSO.

I worked on it and its working with me very well. 🙂

install ioncube loader in ubuntu


Download the current version of ioncube, check your system architecture, my system architecture is x86

  wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz

Now extract the tar file,

  tar -zxvf ioncube_loaders_lin_x86.tar.gz

Now move to extract files to your specific location, here I put the folder to /user/local/lib/

  sudo mv ioncube/ /usr/local/lib/

Now add referance to your php.ini file

  zend_extension = /usr/local/lib/ioncube/ioncube_loader_lin_5.3.so

Restart apache

  sudo /etc/init.d/apache2 restart

Done.

How to verify: Please print phpinfo() and notice the following red rectangular, its should say “”With the ionCube PHP Loader v4.0.1, Copyright(c) 2002-2010, by ionCube Ltd”

Its verified.

use swf/flash as html/php page background


I just got a task, client want to use swf file as page background instead of images. Here is the detail how we can make the swf as page background.

I used 3 css classes in 3 divs. .container contain the whole page, .flash_background contain only the flash object and .content keep all the data.

CSS

.container{
   height: 600px;
   width:  800px;
   position: relative;
}

.flash_background{
   z-index: 1;
   position: absolute;
}
.content{
   z-index: 2;
   position: relative;
} 

Here is HTML code

<div class="container">
	<div class="flash_background">
		<object width="800" height="600">
			<param name="movie" value="swf/file_name.swf">
			<param name="wmode" value="transparent">
			<embed src="swf/file_name.swf" width="800" height="600" wmode="transparent"></embed>
		</object>
	</div>
<div class="content">
data will be here, images, texts etc can working properly  <br />
<br />
Hello World <br />
<br />
Hello WOrld <br />
<br />
Hello WOrld <br />
<br />
</div>
</div>

Done 🙂
Its working with IE, FF, Chrome very well.

direct to specific page instead of get variable www.domain.com/username using php and .htaccess

Tags

, ,


Sometime we need to get direct access any specific page. We need a user profile page http://www.domain.com/username, but our actual url is http://www.domain.com/profile/profile.php?username=Abc

I’m trying to describe how to make the url friendly, I used .htaccess and php for rewrite the url.
.htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?uri=$1 [QSA,L]

In this case the uri took parameter after index.php file.
So if url is http://www.domain.com/username
uri will have the value uri=useranme

We need following code to retrieve the parameter.

 $_SERVER['QUERY_STRING']

I followed the following way inside index.php file to get the username

  $flag = false; 
  if(isset($_SERVER['QUERY_STRING']))
  {
      $q = $_SERVER['QUERY_STRING'];
      $data = explode("=",$q);  // Assume uri=username 
      if(isset($data[0]) && isset($data[1]) && $data[0] == 'uri')
      {
          $a = $data[1];
          $q = "SELECT * FROM user_profiles WHERE user_profile_name = '$a'";
          $cnt = mysql_num_rows(mysql_query($q));
          if($cnt > 0)
          {
             include("profile/profile.php");
             $flag = true;
          }
          
      }
      
  }
if($flag == false)
{
   include("home.php") // your index or homepage 
}

So above code I tried to find the uri and check it on database if username/profile name exists or not. If it found I include the profile page. if not then include the homepage. After including profile.php page, all variables (only above include) are available in profile.php file.

its working with me very well.

You may have better solutions. But I need it very urgent that’s why I write the code in this way. If you have better solution please share your thoughts.

set & reset form text value onfocus, onblur, onclick using javascript


This is simple but sometime its kill our valuable time.
for text field sometime we need to use label as text value, so when mouse click on it the text value should hide, if user doesn’t give any value and point the mouse another field its display value.

<input type="text" 
          onfocus="javascript:this.value==this.defaultValue ? this.value = '' : ''"  
          onblur="javascript:this.value == '' ? this.value = this.defaultValue : '' " 
          value="First Name"  
/>

or
HTML:

<input type='text' value='First Name' onfocus='javascript:check_text_onfous(this)' onblur='javascript:check_text_onblur(this)' />

Javascript:

function check_text_onfocus(obj)
{
    if(obj.value == obj.defaultValue) 
        obj.value = '';
}
function check_text_onblur(obj)
{
    if(obj.value=='')
        obj.value = obj.defaultValue;
}

its works with me on IE,FF,Chrome 🙂