Zend Framework, Gmail, SMTP and SSL
Topic says everything, isn't it?
Description
I had a problem sending emails while using Zend Framework with Gmail account. So I had to handle different types of errors:
- "Unable to connect via TLS" - being created by Zend Framework Exception.
- "Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?" - PHP built in error message
Suggestions
PHP Managing zip files with ZipArchive
Requirements
- PHP 5.2 or greater (would be a bit sad to know that someone is still using PHP 4)
- Upgrade PEAR to latest version
- Upgrade PECL to latest version
- Installing PHP ZipArchive library by using PECL's zip package
Installation
I will use pecl to install ZipArchive PHP library.
- Open terminal and execute pecl install zip
- Updating your php.ini:
- Linux: find your php.ini file and add "extension=zip.so"
- Windows: same as one, just add "extension=zip.dll"
- service httpd restart
- apache2ctl restart
- or any other way that you like
Usage Example
There are many different ways to use and build Zip helpers. I will just demonstrate an example for using ZipArchive.
Pastebin: http://pastebin.com/J2M0jjQG
Inline...
How to find your php.ini file
Finding your php.ini is pretty straight forward task:
- open terminal or cmd
- execute php --ini if php is undefined you should add it to your PATH or just execute "/your/path/to/php --ini"
- php.ini has several modes (path is different sometimes...):
- using one file:
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File: /etc/php5/cli/php.ini - using several different files (will help to maintain common settings between cli php.ini and httpd php.ini
Scan for additional .ini files in: /etc/php5/conf.d
Additional .ini files parsed: /etc/php5/conf.d/ctype.ini,
/etc/php5/conf.d/curl.ini,
/etc/php5/conf.d/dom.ini,
/etc/php5/conf.d/gd.ini,
/etc/php5/conf.d/hash.ini,
............more .ini files here............files content could be "extension=gd.so" (unix) or "extension=gd.dll" (windows)
- there are two more options to get your ini files:
- execute in cli mode: "php --i | grep .ini"
- write your own script and check via browser:
<?php phpinfo(); ?>
That's it, now you can just edit your php.ini with anything you think you will need for future PHP programming.
More about php.ini may be found at http://php.net/manual/en/ini.php
MySQL Split String Function Fix (split_str)
While working on one my projects, I was needed to use split_str function in MySQL. I Googled for it and found two answers (which are exactly the same - one is leading to the other):
Federico Cargnelutti - MySQL Split String Function
Stackoverflow - MYSQL - Array data type, split string
Everything worked great while using different delimiters, such as: "," , "||", "@@@", "###" or any other delimiter. I was using delimiters which their length > 1, exmaple:
select split_str(‘ABC,,BA,,abc’,',,’,3); //result: “abc”
Seems good, ha? But then I`v noticed that there is a problem. When my full string contains two bytes characters (e.g: ¼), everything is breaking a part. Lets see the following exmaple:
select split_str(‘ABC¼,,BA,,abc’,',,’,3); //result: “,abc” (delimiter was still there)
So how do we fix it? Seems that we need to change split_str function, all we have to do is to use CHAR_LENGTH() and not LENGTH(). There is a difference between those functions which you can read at MySQL.com website or just read the following quote:
Returns the length of the string
str, measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters,LENGTH()returns10, whereasCHAR_LENGTH()returns5.
Enough talking. This is the function after my little fix:
CREATE FUNCTION SPLIT_STR( x VARCHAR(255), delim VARCHAR(12), pos INT ) RETURNS VARCHAR(255) RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos), CHAR_LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1), delim, '');
Then using the same example I`v used above:
select split_str(‘ABC¼,,BA,,abc’,',,’,3); //result: “abc”
I want to say thank to Federico Cargnelutti that helped us by writing the above function.
Shak.
Setup\Configure Zend Debugger
Using a debugger is very important while writing code, so lets see how to do it with PHP and Zend Debugger:
- Install Eclipse or Zend Studio
- If you have installed Eclipse, you should install two more things:
- PDT (PHP Development Tools)
- Zend Debugger
- After extracting everything, search for ZendDebugger.so path
- Edit your php.ini or create another ini --> zend.ini which will have to be loaded by your PHP and add:
; Loading Zend Debugger Extension zend_extension=/your/path/to/ZendDebugger.so ; ; use 127.0.0.1 for local host, or you another local network IP zend_debugger.allow_hosts=127.0.0.1 ; ; Expose Zend Debugger ; ; never - do not expose (default) ; always - expose to whoever want to know ; allowed_hosts - expose only if request comes from an IP listed above ; zend_debugger.expost_remotely=always
- Save & Restart Apache
- Verify your phpinfo()
- Download FireFox or Chrome Extension (Chrome extension is not official extension by Zend)
- Configure Eclipse \ Zend Studio settings:
- Configure your extension settings:
- Start debugging!:)
Note for Zend Studio 8.x users:
I suggest that you will update your Zend Studio application by using the following instructions:
http://forums.zend.com/viewtopic.php?f=59&t=10468
Enjoy!
PHP: Insert\Update MySQL BIT(1) field
Today I`v noticed to interested things:
- A nice sentence or a motto that I really liked:
"When you do a search, and it comes back with no results, it’s a sign that you need to write something." (Taken from Here
- Didn't find any straight answer on Google for my question: "How to insert\update a bit field with PHP":
So with MySQL you would use:
INSERT INTO t SET b = b'1'; // Or b'0'
And with PHP you would use boolean type:
//$db instanceof ActiveRecord, that's only for the following example
//you can use other ways to insert\update your MySQL DB.
$db->insert(array('b'=>true,'c'=>false)); //true = b'1' , false = b'0'
$db->update(array('b'=>true,'c'=>false)); //true = b'1' , false = b'0'
Hope I helped,
Shak.
Zend Framework – How to redirect/get application base URL
What is exactly base URL ?
Base URL is sub.domain.end, for example:
if your full URI is http://www.yoursite.com/some/params/id/1, so base URL will be www.yoursite.com
Redirect to application base URL
Sometimes you would want to be able to redirect from one page to your main homepage, for example:
- User A enter Site B.com login page
- User B enter username and password and he had been successfully logged in
- User B is being redirect to Site B.com
As I like to say, if you are reading this post, you would probably need some help about its subject:
There are two ways:
public function loginAction(){
.
.
if (user is logged in successfully) {
$this->_helper->redirector->gotoUrl(); //Option 1
// OR setGotoUrl() uses gotoUrl() when no param is being passed
$this->_helper->redirector->setGotoUrl(); //Option 2
}
.
.
}
How do I get base URL string?
public function loginAction(){
$baseURL = $this->getRequest()->getHttpHost();
/*
When using redirect helper and full URL
you will have to attach http(s)://
which means you will have to use isSSL() method
*/
}
Note:
My suggestion is to create BaseControoler that will handle your current HTTP request & base URL:
class BaseController extends Zend_Controller_Action{
/**
* @var Zend_Controller_Request_Http $_request
*/
protected $_request;
protected function init(){
//Save request
$this->_request = $this->getRequest();
//Save httpHost (base URL)
$this->_httpHost = $this->_request->getHttpHost();
$this->siteInit();
}
protected siteInit(){
/*
You don't really need it
I just prefer to know that if someone creates
another controller he won't have to call parent::init()
*/
}
}
After creating BaseController you have to setup your own controller(s):
//Extend BaseController
class IndexController extends BaseController {
public function indexAction(){
var_dump($this->_httpHost,$this->_request->getParams());
}
}
That it for today,
Shak
Opensuse – Asus yXXy (n53j) – Speakers Audio
Hey,
For long time I had an annoying problem with my speakers on my Asus n53j laptop. I`v googled this problem for long time and nothing. But finally I`v found the VERY-EASY solution:
- Open alsamixer in terminal
- Choose your sound card (F6)
- Continue till you see this screen:

- Go to "Speakers" and make volume up to 100
Highlight your text and share short URL
Today I wanted to share a text with my brother so I thought about making new tool that will highlight my text and create short URL. After some googling I have found a very nice tool that already implement it "Yellow highlighter pen for web". This plugin is working on Chrome and may be found athttp://www.marker.to or at Google chrome extension web store
Use it carefully
Shak.



