PHP exec() Error Codes

These are what the exec() error codes mean that are returned in third parameter of exec().

1 – Catchall for general errors.
2 – Misuse of shell builtins.
126 - Command invoked cannot execute. A permission or command not executable problem.
127 - Command not found.
128 - Invalid argument to exit. Exit takes only integer range 0 – 255.
128+n – Fatal error signal “n”.
130 - Script terminated by Control-C.
255* – Exit status out of range. Exit takes only integer range 0 – 255

Installing Sublime Text 2 for Fedora 17 and 18

yum-config-manager –add-repo http://repo.cloudhike.com/sublime2/fedora/sublime2.repo

yum-config-manager –enable

yum install sublime-text

Configure Pidgin to Connect to Google Talk

In the Modify Account dialog

Basic tab settings:

Protocol: XMPP
Username: //is the username portion of your email
Domain: gmail.com
Resource: Home

Advanced tab settings:

Connection security: Use old-style SSL
Connect port: 443
Connect server: talk.google.com
File transfer proxies: proxy.eu.jabber.org

Installing Chrome Browser in Fedora

The following will install a stable chromium release.

1) Create file /etc/yum.repos.d/fedora-chromium-stable.repo with this content. Create as root.


[fedora-chromium-stable]
name=Builds of the "stable" tag of the Chromium Web Browser
baseurl=http://repos.fedorapeople.org/repos/spot/chromium-stable/fedora-$releasever/$basearch/
enabled=1
skip_if_unavailable=1
gpgcheck=0

[fedora-chromium-stable-source]
name=Builds of the "stable" tag of the Chromium Web Browser - Source
baseurl=http://repos.fedorapeople.org/repos/spot/chromium-stable/fedora-$releasever/SRPMS
enabled=0
skip_if_unavailable=1
gpgcheck=0

2) Install

sudo yum install chromium -y

Useful Sublime Text Settings

Go to Preferences -> Settings Default

// Set to true to removing trailing white space on save
“trim_trailing_white_space_on_save”: true,

// Auto wrap at column 80
“wrap_width”: 80,

// Show ruler at column 80
“rulers”: [80],

// Set to true to insert spaces when tab is pressed
“translate_tabs_to_spaces”: true,

// Set line ending to unix style
“default_line_ending”: “LF”,

Url safe base64 encoding and decoding in PHP

Whenever passing base64 encoded strings in url should always urlencode or do the following which replaces characters that should not be in a query string value with safe characters so query string will not be malformed. When you use the encode method must also use the decode method to convert those characters back to the original.


       /**
         * Custom base64 encoding. Replace unsafe url chars
         *
         * @param string $val
         * @return string
         */
        static function base64_url_encode($val) {

            return strtr(base64_encode($val), '+/=', '-_,');

        }

        /**
         * Custom base64 decode. Replace custom url safe values with normal
         * base64 characters before decoding.
         *
         * @param string $val
         * @return string
         */
        static function base64_url_decode($val) {

            return base64_decode(strtr($val, '-_,', '+/='));

        }

Opening window into tab with window.open() in Chrome

Use this method when you need to open a new window into a tab and not into a popup window through javascript. This should work in most browsers if settings have not been altered.


function open_in_tab(url) {
   var win = window.open(url, '_blank');
   win.focus();
}

Opening window into tab after Ajax Call

window.open() will only open into a tab if it’s result of direct user action. Must make javascript think clicking button is the result of button click after the ajax call. Danger of running ajax synchronously is page can freeze waiting for the request. You can open a window into a tab after an AJAX call with the following:


$('#btn').on("click", function(){
    $.ajax({
      type: 'POST',
      url: "ajax.php",
      data: {action: 'update'},
      dataType: 'json',
      context: document.body,
      async:false,   //Send Synchronously
      success: function(resp){  
         if(resp.success) {
        	window.open('http://google.com'); //Will open into a tab
         }
      }
    });
    
})

Custom File Upload Error Messages

You can set custom error messages using the built-in php upload error constants.

http://www.php.net/manual/en/features.file-upload.errors.php


$upload_errors = array (
      UPLOAD_ERR_OK         => 'No errors.',
      UPLOAD_ERR_INI_SIZE   => 'Larger then upload_max_filesize.',
      UPLOAD_ERR_FORM_SIZE  => 'File size is to large!',
      UPLOAD_ERR_PARTIAL    => 'Resume partially uploaded!',
      UPLOAD_ERR_NO_FILE    => 'You must upload a resume!',
      UPLOAD_ERR_NO_TMP_DIR => 'Temporary directory does not exist!',
      UPLOAD_ERR_CANT_WRITE => "Can't write to disk!",
      UPLOAD_ERR_EXTENSION  => 'File upload stopped by extension!'
);

......

// After form post get file info
$file = $_FILES['uploaded_file'];

// If there was an error during upload then display custom message
 if ($file['error'] != 0) {
     echo $upload_errors[$file['error']]);
 }

Extracting Current Page Handle Using Javascript Location Object

This will extract just the file name portion of the current page.


//e.g. location.href = www.domain.com/example.php
console_log(location.pathname.substring(1));
//displays: example.php

Remove all whitespace (tabs, returns and space) from string in PHP


$site_name = preg_replace('/\s+/', '', $profile_name);