parsed.org

Tips by tag: php

Add this to your .vimrc:

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

Try it out:

$ touch index.html
$ vim index.html
type: <ht[CTRL-X][CTRL-O]
autocompleteccsshtmljavascriptphppythonvimvimrcxml

Take a directory structure like the following:

~/example.com/
  html/
  includes/
  logs/

html is the public website and the includes are where includes like db connection settings are kept. In ~/example.com/html/index.php you can call use something like the following to get the file system path of example.com that you can use to include the resulting files. This will do it automatically so it works with different installations:

<?php
define('PATH_TO_ROOT', realpath(dirname(__FILE__).'/..'));
include_once(PATH_TO_ROOT.'/includes/config.inc.php');
?>
phpprogramming
Raw Post Data by xinu on Jan 15, 2005 03:10 PM

Sometimes it's necessary to access raw post data. The easiest way to do this is by opening the php://input stream:

$fp = fopen('php://input', 'r');
httpiolanguagesphppostprogrammingsyntax
Removing Comments by xinu on Jan 15, 2005 03:09 PM

If you're having to match lines that start with # you can avoid using an expression if you do something like this:

foreach ($lines as $line) {
    if ($line{0} != '#') {
        // We have a non-comment, print it.
        echo $line . '<br />';
    }
}
foreachlanguagesphpprogrammingsyntax
SEO: Path Info by xinu on Jun 01, 2007 11:28 AM

When trying to make a website indexable by search engine spiders, be sure to enable AcceptPathInfo in .htaccess or httpd.conf:

AcceptPathInfo On

It's not enabled by default in Apache 2 and can cause applications that use it to return HTTP 404 responses.

apache2path_infophpseo
Session ID Format by cygnus on Aug 12, 2005 03:19 PM

Beware that on some versions of PHP, the PHP session ID value is a hexadecimal hash but on some newer systems the configuration is used to adjust the contents of the session ID string:

; Define how many bits are stored in each character when converting
; the binary hash data to something readable.
;
; 4 bits: 0-9, a-f
; 5 bits: 0-9, a-v
; 6 bits: 0-9, a-z, A-Z, "-", ","
session.hash_bits_per_character = 5
configurationgotchahexadecimallanguagesphpphp.iniprogrammingsession
Strip Non-printable Characters by xinu on Jul 03, 2005 12:44 AM

You can use PHP's trim function to remove non-printable characters:

$string = trim($string, "\x7f..\xff\x0..\x1f");
charactersfunctionslanguagesnon-printablephpprogrammingtrim
Turning Off PHP Caching by cygnus on Dec 31, 2005 04:07 PM

When using sessions in PHP, the default PHP configuration causes PHP to send a no-cache header to your browser so that session-managed pages are not cached. Sometimes this is not the desired behavior, such as when writing a script whose output is binary data such as images which are stored in a database. If this is the case, you can turn off the caching by using functions to modify the PHP configuration on a per-request basis. Call these functions before calling the session_start() function:

// Let the browser and proxies cache output
session_cache_limiter('public');

// One-day (60 * 24, in minutes) cache expiration time for output
session_cache_expire(60 * 24);

(Note: this behavior is documented at http://www.php.net/manual/en/ref.session.php.)

functionsheadershttplanguagesphpprogrammingsession
RSS