50% OFF!!!

Sunday, November 22, 2015

How to install php imap extension on Centos (or any)

The php package (extension) for IMAP is usually:
php-imap

Sometimes, for example on amazon ec2, when installing PHP ver. 5.5 or 5.6,
In order to install this (or any other) PHP extension,
that not come with the default PHP,
You should installed the one related to your PHP version.

For example,
one of my servers have PHP 5.6 installed on it,
but when running 
 yum install php-imap 
which gives:
Error: php56-imap conflicts with php-imap-5.3.29-1.8.amzn1.x86_64
Error: php56-common conflicts with php-common-5.3.29-1.8.amzn1.x86_64
Error: php55-common conflicts with php-common-5.3.29-1.8.amzn1.x86_64
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest 


so instead, check first which version currently installed:
yum list installed | grep php
 which gives:
php56.x86_64                          5.6.14-1.119.amzn1           @amzn-updates
php56-cli.x86_64                      5.6.14-1.119.amzn1           @amzn-updates
php56-common.x86_64                   5.6.14-1.119.amzn1           @amzn-updates
php56-imap.x86_64                     5.6.14-1.119.amzn1           @amzn-updates
php56-jsonc.x86_64                    1.3.6-1.19.amzn1             @amzn-main
php56-mbstring.x86_64                 5.6.14-1.119.amzn1           @amzn-updates
php56-mysqlnd.x86_64                  5.6.14-1.119.amzn1           @amzn-updates
php56-pdo.x86_64                      5.6.14-1.119.amzn1           @amzn-updates
php56-process.x86_64                  5.6.14-1.119.amzn1           @amzn-updates
php56-xml.x86_64                      5.6.14-1.119.amzn1           @amzn-updates 
 

Which means that your PHP version is 5.6 (you can also run php -v)
php -v
 which gives:
PHP 5.6.14 (cli) (built: Oct 16 2015 22:58:32)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

All needed now is to install the correct version.
If you install php55-imap (or any other package) it won't work!
Some have several PHP versions installed.
command:
 
yum install php56-imap

 
don't forget to restart your apache after that.
service httpd restart


and done... :)
Hope it helps.

Friday, November 13, 2015

PHP | XML to Html Table elment convertion

In this thread,
I will show generic & universal function that
gets XML string,
convert it,
and display it as an HTML table.
(you can add any css style to design your table later on of course)


The following code is using XML example from http://www.w3schools.com/xml/simple.xml.
The XML looks like that:
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Two of our famous...</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>Light Belgian waffles...</description>
    <calories>900</calories>
  </food>
  <food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>Light Belgian....</description>
    <calories>900</calories>
  </food>
  <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>Thick slices made....</description>
    <calories>600</calories>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>Two eggs, bacon...</description>
    <calories>950</calories>
  </food>
</breakfast_menu>



The function xmlToHtmlTable 
gets a SimpleXMLElement parameter which points to main parent for which will print all his children,
and returns html table representing the XML as string.

The Code:
 <?php  
   
 // get XML remotely / locally / or / just set it as string '<root>...</root>'  
 $sXml = file_get_contents('http://www.w3schools.com/xml/simple.xml');  
   
 // parse XML  
 $oXML = simplexml_load_string($sXml);  
 if (!$oXML) {  
      die('xml format not valid or simplexml module missing.');  
 }  
   
 // assuming running the root  
 $oXmlRoot = $oXML; // or can be [$oXML->food]  
   
 //echo '<pre>'; print_r( $oXmlRoot ); echo '</pre>';  
 echo xmlToHtmlTable($oXmlRoot);  
   
   
 function xmlToHtmlTable($p_oXmlRoot) {  
      $bIsHeaderProceessed = false;  
        
      $sTHead = '';  
      $sTBody = '';       
      foreach ($p_oXmlRoot as $oNode) {  
           $sTBody .= '<tr>';  
           foreach ($oNode as $sName => $oValue){  
                if (!$bIsHeaderProceessed) {  
                     $sTHead .= "<th>{$sName}</th>";  
                }  
                $sValue = (string)$oValue;  
                $sTBody .= "<td>{$sValue}</td>";                 
           }  
           $bIsHeaderProceessed = true;  
           $sTBody .= '</tr>';  
      }  
        
      $sHTML = "<table border=1>  
                     <thead><tr>{$sTHead}</tr></thead>  
                     <tbody>{$sTBody}</tbody>  
                </table>";  
      return $sHTML;  
 }  

This will output:

 
Hope it helps!
MDB BLOG!




 

Friday, June 19, 2015

MySQL | find median (Nth element) without join!

This is my suggestion to find on mysql select command
the Median (Nth element) without joining the table to itself (or to new custom one).

My method, uses string manipulation
so I'm not sure it is good for big tables.
(I tested it with medium size tables on Mysql 5.5.28)

The advantage of this method, 
that it works also if we need to find the MEDIAN for each group in the select query.

the code is using: SUBSTRING_INDEX and GROUP_CONCAT methods.


Here is test code for test table:
DROP TABLE test.test_median
CREATE TABLE test.test_median AS
SELECT 'book' AS grp, 4 AS val UNION ALL
SELECT 'book', 7 UNION ALL
SELECT 'book', 2 UNION ALL
SELECT 'book', 2 UNION ALL
SELECT 'book', 9 UNION ALL
SELECT 'book', 8 UNION ALL
SELECT 'book', 3 UNION ALL

SELECT 'note', 11 UNION ALL

SELECT 'bike', 22 UNION ALL
SELECT 'bike', 26 



and the code for finding the median for each group:
SELECT grp,
         SUBSTRING_INDEX( SUBSTRING_INDEX( GROUP_CONCAT(val ORDER BY val), ',', COUNT(*)/2 ), ',', -1) as the_median,
         GROUP_CONCAT(val ORDER BY val) as all_vals_for_debug
FROM test.test_median
GROUP BY grp

The output:

grp the_median all_vals_for_debug
bike 22 22,26
book 4 2,2,3,4,7,8,9
note 11 11


Hope it helps you there...


Wednesday, May 27, 2015

php | destroy/delete all sessions

I recently searched for an option deleting all running/active sessions,
for changing some important logic on my server.

Important:
just restarting apache won't help!
because php handles php-sessions by itself.

now,
I wonder how to make it, without changing php code.
The solution, is to delete the PHP session files created by PHP engine.

Step #1 - find where PHP store php files
on php.ini there is configured session.save_path 
which points to the correct path.
by default, it configured to "" (empty), and it is:



  • On Ubuntu or Debian machines: saved on /var/lib/php5
  • On RHEL and CentOS systems: saved on /var/lib/php/session
  • If you need to find it: print session_save_path() on your php file.    You may run from command line:  php -r 'echo session_save_path(), "\n";'

Step #2 - just delete the files
Execute the following bash script:
rm  -f   /var/lib/php/session/*
or
rm  -f   /var/lib/php5/sess*
(-f will force delete without prompting!!) 



And that's it...
Now when a user will execute web-request, 
his session will be considered empty, 
and will be re-generated (under the same session name)