Top-rated PHP recipes http://code.activestate.com/recipes/langs/php/top/2015-06-26T04:30:14-07:00ActiveState Code RecipesMicrosoft Access Database Connectivity (DSN-Less) (PHP) 2006-08-29T03:08:29-07:00Erick Guanlaohttp://code.activestate.com/recipes/users/670494/http://code.activestate.com/recipes/163447-microsoft-access-database-connectivity-dsn-less/ <p style="color: grey"> PHP recipe 163447 by <a href="/recipes/users/670494/">Erick Guanlao</a> (<a href="/recipes/tags/database/">database</a>). Revision 4. </p> <p>An example of connecting to a Microsoft Access database, executing an SQL query, and displaying the results in HTML using PHP and COM (Windows only).</p> <p>NOTE: This works with PHP 4.x. I have been unsuccessful with getting this to work with PHP 5.1.6 and Apache 2.0.59 on Windowx XP Professional.</p> PHP 'Struct' port (PHP) 2010-03-28T01:22:31-07:00Jeff Griffithshttp://code.activestate.com/recipes/users/835605/http://code.activestate.com/recipes/577160-php-struct-port/ <p style="color: grey"> PHP recipe 577160 by <a href="/recipes/users/835605/">Jeff Griffiths</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>In Ruby, the <a href="http://ruby-doc.org/core/classes/Struct.html">Struct class</a> is a convenient way to create a hash-like object on the fly and use it for your nefarious purposes. PHP 5+ can be convinced to do this type of things as well, it just doesn't have it out of the box. Here is a simple class that implements iterator and allows you to populate the internal data structure similar to how Ruby's Struct works. Syntactic sugar? Probably.</p> <p>Note: I haven't bothered to implement the Ruby Struct API per se, Instead I just got something similar by implementing the Iterator interface and keeping things very PHP-like.</p> Converting MySQL queries to XML (PHP) 2008-09-11T13:16:40-07:00sameer boratehttp://code.activestate.com/recipes/users/4167072/http://code.activestate.com/recipes/576499-converting-mysql-queries-to-xml/ <p style="color: grey"> PHP recipe 576499 by <a href="/recipes/users/4167072/">sameer borate</a> (<a href="/recipes/tags/mysql/">mysql</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>Converting MySQL queries to XML</p> Pass Javascript arrays to PHP (PHP) 2005-05-16T11:45:33-07:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/414334-pass-javascript-arrays-to-php/ <p style="color: grey"> PHP recipe 414334 by <a href="/recipes/users/103276/">Noah Spurrier</a> (<a href="/recipes/tags/session/">session</a>). </p> <p>This is a Javascript function that will convert a Javascript array to a string in PHP serialized format. You can pass this string to a PHP script and easily unserialize it to a PHP array.</p> DB_eSession PHP class facilitates having sessions stored in a MySQL database. (PHP) 2004-11-28T22:16:52-08:00Lawrence Ohttp://code.activestate.com/recipes/users/2180633/http://code.activestate.com/recipes/347808-db_esession-php-class-facilitates-having-sessions-/ <p style="color: grey"> PHP recipe 347808 by <a href="/recipes/users/2180633/">Lawrence O</a> (<a href="/recipes/tags/session/">session</a>). </p> <p>DB_eSession is a feature packed PHP class that stores session data in a MySQL database rather than flat files. It is powerful, designed with security in mind, and yet easy to utilize. The web site has a full package download containing example scripts too.</p> main - python-like if __name__ == __main__ (PHP) 2004-09-09T00:49:19-07:00Mirek Rusinhttp://code.activestate.com/recipes/users/2028384/http://code.activestate.com/recipes/303971-main-python-like-if-__name__-__main__/ <p style="color: grey"> PHP recipe 303971 by <a href="/recipes/users/2028384/">Mirek Rusin</a> . </p> <p>How to determine if we're in the (directly) launched script.</p> PHP MySQL Search Class (PHP) 2003-10-28T19:57:45-08:00Stephen Bartholomewhttp://code.activestate.com/recipes/users/350445/http://code.activestate.com/recipes/125901-php-mysql-search-class/ <p style="color: grey"> PHP recipe 125901 by <a href="/recipes/users/350445/">Stephen Bartholomew</a> (<a href="/recipes/tags/database/">database</a>). Revision 10. </p> <p>This class can perform a full text search over multiple columns of a MySQL database table, matching entries containing all search terms parsed to it.</p> Class Inspection (PHP) 2001-12-07T10:50:29-08:00Shane Caraveohttp://code.activestate.com/recipes/users/98233/http://code.activestate.com/recipes/101522-class-inspection/ <p style="color: grey"> PHP recipe 101522 by <a href="/recipes/users/98233/">Shane Caraveo</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Simple class introspection using php's class functions. This generates a list of classes, and their members and methods.</p> print qrcode to usb termal RP80 printer from web base app (PHP) 2014-09-09T09:35:32-07:00imam feriantohttp://code.activestate.com/recipes/users/633541/http://code.activestate.com/recipes/578930-print-qrcode-to-usb-termal-rp80-printer-from-web-b/ <p style="color: grey"> PHP recipe 578930 by <a href="/recipes/users/633541/">imam ferianto</a> (<a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/printing/">printing</a>, <a href="/recipes/tags/qrcode/">qrcode</a>). </p> <p>this script will print qrcode from php script. the printer is attached on webserver usb port, see my other article here: <a href="http://code.activestate.com/recipes/578925-print-directly-from-web-application-to-poseps-ther/" rel="nofollow">http://code.activestate.com/recipes/578925-print-directly-from-web-application-to-poseps-ther/</a></p> <p>and python escpos references: <a href="https://code.google.com/p/python-escpos/wiki/Usage" rel="nofollow">https://code.google.com/p/python-escpos/wiki/Usage</a></p> Print Directly from web application to POS/EPS thermal printer (PHP) 2014-09-01T14:55:56-07:00imam feriantohttp://code.activestate.com/recipes/users/633541/http://code.activestate.com/recipes/578925-print-directly-from-web-application-to-poseps-ther/ <p style="color: grey"> PHP recipe 578925 by <a href="/recipes/users/633541/">imam ferianto</a> (<a href="/recipes/tags/kasir/">kasir</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/over/">over</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/pos/">pos</a>, <a href="/recipes/tags/printer/">printer</a>, <a href="/recipes/tags/tiketing/">tiketing</a>). Revision 2. </p> <p>this php script will printout barcode label directly from the web by phpscript</p> Detect what browser your website is being accessed from using php (PHP) 2013-06-19T20:09:55-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578571-detect-what-browser-your-website-is-being-accessed/ <p style="color: grey"> PHP recipe 578571 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/browser/">browser</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/server/">server</a>). </p> <p>This is a short hack on how to figure out what OS you site is being accessed from. This is useful if you want to redirect to different versions of your site through php. </p> php backwards compatibility porting tips (PHP) 2013-02-10T00:40:26-08:00imam feriantohttp://code.activestate.com/recipes/users/633541/http://code.activestate.com/recipes/578452-php-backwards-compatibility-porting-tips/ <p style="color: grey"> PHP recipe 578452 by <a href="/recipes/users/633541/">imam ferianto</a> (<a href="/recipes/tags/backwards/">backwards</a>, <a href="/recipes/tags/compatibility/">compatibility</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/php4/">php4</a>, <a href="/recipes/tags/php5/">php5</a>). </p> <p>Hello again, on december 2012, I was porting my old php code (php4) to new php 5.4.3. There are to many errors within code, especially for session, eregi, global variables and others. This some pieces step for compatibility porting:</p> <ol> <li><p>add global variable definiton on some function that need outside variable &lt;code&gt; function getuser(){ global $loginid,$password; } &lt;/code&gt;</p></li> <li><p>replace eregi(/find/,str) with preg_match(//,str)</p></li> <li><p>add function exists for function that not exists in php5.4.3. for example: &lt;code&gt; if(!function_exists("session_is_registered")){ function session_is_registered($var){ return isset($_SESSION[$var]); } } if(!function_exists("session_register")){ function session_register($var){ $_SESSION[$var]=$_GLOBALS[$var]; } } &lt;/code&gt;</p></li> </ol> <p>Ok there is my tips, how about you? </p> Benchmark (PHP) 2015-06-26T04:30:14-07:00Roger Mbiama Assogohttp://code.activestate.com/recipes/users/4178746/http://code.activestate.com/recipes/578235-benchmark/ <p style="color: grey"> PHP recipe 578235 by <a href="/recipes/users/4178746/">Roger Mbiama Assogo</a> (<a href="/recipes/tags/perl/">perl</a>, <a href="/recipes/tags/ppi/">ppi</a>, <a href="/recipes/tags/scripts/">scripts</a>, <a href="/recipes/tags/vim/">vim</a>). Revision 6. </p> <p>perl [ -sTtuUWX ] [ -hv ] [ -V[:configvar] ] [ -cw ] [ -d[t][:debugger] ] [ -D[number/list] ] [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal/hexadecimal] ] [ -Idir ] [ -m[-]module ] [ -M[-]'module...' ] [ -f ] [ -C [number/list] ] [ -S ] [ -x[dir] ] [ -i[extension] ] [ [-e|-E] 'command' ] [ -- ] [ programfile ] [ argument ] [stap -L 'kernel.trace("*")'|sort] [feature]... BEGIN { my $b__dir = (-d '/home/angosson/perl'?'/var/www/cgi-bin/perl':( getpwuid($&gt;) )[7].'/perl');</p> <pre class="prettyprint"><code>unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux-thread-multi',map { $b__dir . $_ } @INC; </code></pre> <p>} require LWP::UserAgent;</p> <p>my $ua = LWP::UserAgent-&gt;new; $ua-&gt;timeout(10); $ua-&gt;env_proxy;</p> Web based Query Browser (PHP) 2011-06-15T03:54:02-07:00Jonathan Fenechhttp://code.activestate.com/recipes/users/4169413/http://code.activestate.com/recipes/577753-web-based-query-browser/ <p style="color: grey"> PHP recipe 577753 by <a href="/recipes/users/4169413/">Jonathan Fenech</a> (<a href="/recipes/tags/based/">based</a>, <a href="/recipes/tags/browser/">browser</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/web/">web</a>). </p> <p>Query browser works </p> <p>add password to this part of the code if you require a password for mysql</p> <p>Code =</p> <p>// Connect to the database $conn = mysql_connect('localhost', 'root' 'PASSWORD GOES HERE");</p> YouTube-like primary key generation (PHP) 2011-02-24T21:43:55-08:00Slava Yansonhttp://code.activestate.com/recipes/users/4176967/http://code.activestate.com/recipes/577571-youtube-like-primary-key-generation/ <p style="color: grey"> PHP recipe 577571 by <a href="/recipes/users/4176967/">Slava Yanson</a> (<a href="/recipes/tags/activerecord/">activerecord</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/unique/">unique</a>). Revision 4. </p> <p>If you have more then 1 primary database servers - you have probably experienced the pain of primary key overlapping... Here is a super simple solution that will generate a hash of random letters and numbers.</p> <p>Included is a sample ActiveRecord class (MyTable.php).</p> Validating Emails (PHP) 2011-06-13T15:10:47-07:00Jonathan Fenechhttp://code.activestate.com/recipes/users/4169413/http://code.activestate.com/recipes/577198-validating-emails/ <p style="color: grey"> PHP recipe 577198 by <a href="/recipes/users/4169413/">Jonathan Fenech</a> (<a href="/recipes/tags/emails/">emails</a>, <a href="/recipes/tags/validating/">validating</a>). Revision 2. </p> <p>a small script that can be used for Validating Emails on </p> <ul> <li>Login pages</li> <li>forums </li> </ul> <p>changed to preg_match from eregi function. now scripted validates everything typed in input boxes </p> <p>Old Code = </p> <pre class="prettyprint"><code> foreach($Email as $Emails) { // checks Emails - .net , .com , .au &amp; etc if(eregi("[a-zA-Z0-9]@+[a-z].{1,}com$",trim($Emails))|| eregi( "[a-zA-Z0-9]@+[a-z].{1,}net$",trim($Emails))) </code></pre> <p>New Code = </p> <pre class="prettyprint"><code> foreach($Email as $Emails) { // checks Emails - .net , .com , .au &amp; etc if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z] [0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $Emails)) </code></pre> <p>i changed from eregi function to preg_match function because eregi was taking out of PHP version 5.3.0 and 5.3.5.</p> Generate an alpha-numeric password salt (PHP) 2010-02-25T14:07:08-08:00Jayesh Shethhttp://code.activestate.com/recipes/users/4173165/http://code.activestate.com/recipes/577071-generate-an-alpha-numeric-password-salt/ <p style="color: grey"> PHP recipe 577071 by <a href="/recipes/users/4173165/">Jayesh Sheth</a> (<a href="/recipes/tags/md5/">md5</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/random_number/">random_number</a>, <a href="/recipes/tags/salt/">salt</a>). </p> <p>Generate an alpha-numeric password salt (with a default of 32 characters)</p> Generate a salt (PHP) 2012-07-10T19:52:17-07:00Xavier L.http://code.activestate.com/recipes/users/4171602/http://code.activestate.com/recipes/576894-generate-a-salt/ <p style="color: grey"> PHP recipe 576894 by <a href="/recipes/users/4171602/">Xavier L.</a> (<a href="/recipes/tags/crypt/">crypt</a>, <a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/pass/">pass</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/salt/">salt</a>, <a href="/recipes/tags/secure/">secure</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/string/">string</a>). Revision 6. </p> <p>This function will generate a salt for use with passwords ranging using characters in range a to z, A to Z, 0 to 9 and !@#$%&amp;<em>?. The characters are sorted in a random value and can appear more than one time in the string. This way, this function is more powerful than the *shuffle()</em> function. This means that the salt could also be longer than the character list.</p> Hash text simply (PHP) 2012-04-30T21:43:00-07:00Xavier L.http://code.activestate.com/recipes/users/4171602/http://code.activestate.com/recipes/576893-hash-text-simply/ <p style="color: grey"> PHP recipe 576893 by <a href="/recipes/users/4171602/">Xavier L.</a> (<a href="/recipes/tags/crypt/">crypt</a>, <a href="/recipes/tags/extensible/">extensible</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/secure/">secure</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/simple/">simple</a>). Revision 5. </p> <p>This is a simply and extensible script that can be used to rapidly has any amount of text using PHP's hash() built-in function.</p> <p>It recognizes when the page is loaded for the first time and displays a form with options. Afterward, it display the selected hash(es), with a link at the bottom to start again.</p> PHP Login page (PHP) 2010-04-17T13:31:03-07:00Jonathan Fenechhttp://code.activestate.com/recipes/users/4169413/http://code.activestate.com/recipes/576680-php-login-page/ <p style="color: grey"> PHP recipe 576680 by <a href="/recipes/users/4169413/">Jonathan Fenech</a> (<a href="/recipes/tags/login/">login</a>, <a href="/recipes/tags/page/">page</a>). Revision 2. </p> <p>this is a small php login script.</p>