Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Fix for EE Multi-relationship field in PHP4

// http://expressionengine.com/forums/viewreply/192755/

Open your /system/modules/weblog/mod.weblog.php file and find from this line:
    
    // -------------------------------------------
    // 'weblog_entries_tagdata' hook.

to this line:
    
    //
    // -------------------------------------------

Replace between those comments with this:

    if (isset($EXT->extensions['weblog_entries_tagdata']))
    {
        // -- PHP4 Fix For call_user_func_array not passing by reference
        global $Weblog; $Weblog = $this;
        // -- End Fix
        
        $tagdata = $EXT->call_extension('weblog_entries_tagdata', $tagdata, $row, $this);
        if ($EXT->end_script === TRUE) return $tagdata;
        
        // -- PHP4 Fix For call_user_func_array not passing by reference
        $this = $Weblog; unset($Weblog);
        // -- End Fix
    

PHP will organize form values into arrays if you ask nicely

I took stole this example directly from php.net but it is worth the infraction. Name your form fields correctly and they are placed in arrays in the $_GET or $_POST arrays. This example would return something like this if you selected two of the "beer" elements.

Array
(
[personal] => Array
(
[name] => Fred Derf
[email] => fred@example.com
)

[beer] => Array
(
[0] => warthog
[1] => guinness
)

)

<?php
if ($_POST) {
    echo '<pre>';
    echo htmlspecialchars(print_r($_POST, true));
    echo '</pre>';
}
?>
<form action="" method="post">
    Name:  <input type="text" name="personal[name]" /><br />
    Email: <input type="text" name="personal[email]" /><br />
    Beer: <br />
    <select multiple name="beer[]">
        <option value="warthog">Warthog</option>
        <option value="guinness">Guinness</option>
        <option value="stuttgarter">Stuttgarter Schwabenbräu</option>
    </select><br />
    <input type="submit" value="submit me!" />
</form>

Installs pecl memcache php5 extension on a Joyent Accelerator

//

#!/bin/sh
# Installs pecl memcache php5 extension on a Joyent Accelerator
#
# $Id$

VERSION=2.2.3
TEMPDIR=`/usr/bin/mktemp`

cd ${TEMPDIR}
/usr/sfw/bin/wget http://pecl.php.net/get/memcache-${VERSION}.tgz
/opt/local/bin/tar -zxvf memcache-${VERSION}.tgz
cd memcache-${VERSION}
/opt/local/bin/phpize
./configure
/usr/bin/make
/usr/bin/make install
/opt/local/bin/gsed -i"" "s/;extension=memcache.so/extension=memcache.so/" /opt/local/etc/php.ini

关于MYSQL的用法

// MYSQL是一种非常常用的东西
//I JUST WANT TO TEST

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Test php mysql pdo_mysql

// description of your code here

<?php $link = mysql_connect('localhost','mydbuser','mydbpassword');
if (!$link) {
        die('Could not connect to MySQL: ' . mysql_error());
} echo 'mySql Connection OK';
mysql_close($link);

$dbh = new PDO('mysql:host=localhost;dbname=test','mydbuser','mydbpassword');
if ($dbh) {
        echo 'mysql_pdo CONNECTION OK';
}
else {echo 'mysql_pdo ERROR';}
?>

Enable Verbose Error Reporting

The first step to secure a PHP application is to enable PHP features that track application errors. Application errors often point directly to or provide clues about vulnerabilities.
For example, many of the register global-related uninitialized variable errors can be detected simply by raising the error reporting level within an application.
Here's some code to do just that:

error_reporting(E_ALL);   // in PHP 5.0 E_ALL | E_STRICT
ini_set("display_errors", 0);
ini_set("log_errors", 1);
ini_set("error_log", "/home/user/logs/app_xyz.php.log");


The first two lines of the code enable the tracking of all errors (E_ALL in PHP 4.0 or E_ALL | E_STRICT in PHP 5.0 and above), including warnings, fatal errors, and notices about uninitialized variables. The second line of code disables the display of errors, so that the code can be deployed in a production environment, while lines three and four specify that errors should be logged to a named file - an easy way to monitor error messages in any pager or editor utility, such as less or vim, respectively. To use this snippet, just place it at the top of any application, in a header for example.

Helpful tip for debugging arrays

I found this helpful while debugging arrays.

error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);

Sizeof() vs. Count()

// from http://digest.phpit.net/archives/23/html/. They didn't cite a source, so I'm not so sure about this.

Quick Tip: use sizeof() instead of count()

If you’re writing a PHP5 script, use the sizeof() function instead of count(). Although the manual says that sizeof() is a simple alias for count(), sizeof() is actually twice as fast, and can give a slight increase in performance, especially when used a lot.

In one of my scripts I saw performance increase by a whole second by using sizeof() instead of count(). I’m not sure why this is, especially since it’s officially an alias, but that’s just the way it is. Use it to your advantage!

Setup LAMP on Ubuntu with MyODBC support.

Go to terminal:

sudo apt-get install apache2 mysql-server php5 php5-odbc libapache2-mod-php5 php5-xsl php5-gd php-pear libapache2-mod-auth-mysql php5-mysql libmyodbc


Now hand edit (or create, if it does not exist already) /etc/odbc.ini

Here's an example odbc.ini:

[ODBC Data Sources]
odbcname     = MyODBC 3.51 Driver DSN

[odbcname]
Driver       = /usr/lib/odbc/libmyodbc.so
Description  = MyODBC 3.51 Driver DSN
SERVER       = my.database.server.com
PORT         =
USER         = USER
Password     = XXXXXX
Database     = DBNAME
OPTION       = 3
SOCKET       =

[Default]
Driver       = /usr/local/lib/libmyodbc3.so
Description  = MyODBC 3.51 Driver DSN
SERVER       = localhost
PORT         =
USER         = root
Password     =
Database     = test
OPTION       = 3
SOCKET       =


Remember to start and stop mysql and apache:

sudo /etc/init.d/mysql start
sudo /etc/init.d/apache2 start

sudo /etc/init.d/mysql stop
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/mysql restart
sudo /etc/init.d/apache2 restart

Run Apache in 32bit mode on Leopard

There is currently an issue resulting from the fact that Apache on Mac OS X 10.5 is built as a 64 bit application. On Macs that support 64 bits (all current Mac models), Apache will run in 64 bit mode. This is incompatible with most ODBC drivers, including those currently available from Actual Technologies.
Until we can make 64 bit versions of our drivers available, we recommend a workaround to force Apache to run in 32 bit mode. Using the Terminal prompt, enter the following commands (this is for ppc machines, for intel switch ppc7400 to i386):
sudo mv /usr/sbin/httpd /usr/sbin/httpd.ub

sudo lipo -thin ppc7400 /usr/sbin/httpd.ub -output /usr/sbin/httpd.ppc7400

sudo ln -s /usr/sbin/httpd.ppc7400 /usr/sbin/httpd 

Now restart Web Sharing in the System Preferences, and continue with the instructions for "Invoking ODBC Functions from ODBC" below.
NOTE: if you ever need to restore Apache to 64 bit mode, just enter the following command:
sudo mv /usr/sbin/httpd.ub /usr/sbin/httpd