Skip to main content

Posts

Showing posts with the label PHP

Serving static files in a directory.

A few different ways to serve static files in a directory on the fly for development. IIS Express C:\> "C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:C:\MyWeb /port:8000 NodeJS $ npm install -g http-server $ http-server -p 8000 PHP $ php -S localhost:8000 Python 2 $ python -m SimpleHTTPServer 8000 Python 3 $ python3 -m http.server 8000 Ruby $ ruby -run -e httpd . -p 8000

How to use Colors in the Command Line output with PHP

Here is a simple function you can copy into your php file and it will format whatever section of text you place in it with a certain background and foreground color. <?php function colorFormatText ( $string , $textColor , $bgColor ) { return chr ( 27 ) . "[" . $textColor . "m" . chr ( 27 ) . "[" . $bgColor . "m" . $string . chr ( 27 ) . "[0m" ; } ?> The Colors Here is a list of the colors available using this method. There are only 8 colors. Text Background Color 30 40 Black 31 41 Red 32 42 Green 33 43 Yellow 34 44 Dark Blue 35 45 Pink 36 46 Light Blue 37 47 White

PHP LDAP

Simple function to check if a username and password is correct with Active Directory. function checkNTuser($username, $pasword){ $Domain = "SOMEDOMAIN" ; $Server = "ldap://someaddress.co" ; $auth = $username. "@" .$Domain; if (extension_loaded( 'ldap' )){ echo "LDAP Module Enabled" ; $connect = ldap_connect($Server); if ($bind = ldap_bind($connect, $auth, $password)){ ldap_close($connect); echo "Success" ; return ( true ); } else { ldap_close($connect); echo "Failed" ; return ( false ); } } }

Palindrome

One of the first kinds of programs learned by a student is how to determine if a string is a palindrome. A palindroms is a word or string that is spelled the same backwords as it is forwards. An example would be "rotor". <?php $test = "rotor" ; if ( $test == strrev ( $test )) { echo "This is a palindrome" ; } else { echo "This is NOT a palindrome" ; } ?>

Export HTML to Excel

Export to Excel Today I learned a cool trick of how to export data that I have arranged in tables to an excel file quick and easy. Below is a sample of some code. To make it work copy the following code into its own php file. You will notice that in the code there are to function calls to the header. The first is the Content-Type which gives the browser the information needed to use excel to open the document. The second is the Content-disposition. This allows the user the option to save the document as an excel file, with a default or custom filename. <?php header( "Content-Type: application/vnd.ms-excel; name='excel'" ); header( "Content-disposition: attachment; filename=test.xls" ); ?> <table border= "1" > <tr> <td> test </td> <td> =3+4 </td> </tr> <tr> <td> test </td> <td> test2 </td> </tr> <tr> <td> test </td...