* You are viewing the archive for the ‘PHP’ Category

Immediate availability of PHP

The PHP development team announces the immediate availability of PHP
5.4.11 and PHP 5.3.21. These releases fix about 10 bugs. All users of
PHP are encouraged to upgrade to PHP 5.4. PHP 5.3.20 is recommended
for those wishing to remain on the 5.3 series.

The full list of changes are recorded in the ChangeLog on
http://www.php.net/ChangeLog-5.php

For source downloads of PHP 5.4.11 and PHP 5.3.21 please visit our
downloads page at http://www.php.net/downloads.php

Windows binaries can be found on http://windows.php.net/download/

PHP Array Function: array_change_key_case

The array_change_key_case function allows you to manipulate the textual data in an array by transforming it into uppercase or lowercase.

Syntax:
array_change_key_case(array, CASE_LOWER or CASE_UPPER)
The array is the name of the array whose values you wish to convert to upper or lowercase.
The parameter CASE_LOWER or CASE_UPPER is used to determine whether or not you are going to convert the values in the array to upper or lower case.

Example:
<?php
$arrLevel = array (“a” => “Programmer”, “b” => “Analyst”, “c” => “Administrator”);
print_r(array_change_key_case($arrLevel, CASE_LOWER));
?>
The above code takes the values in $test and converts them to lowercase.
Converting your array values to uppercase is as simple as changing … Continue Reading

Tips for Coding Cleaner PHP

PHP is lovely, though if you ever work with other people having good code is important. Here are some tips to improve your code.

Comment & Document
It’s really important that people understand why you are doing certain things in certain ways. Adding a quick comment above sections of code should be adequate, but documenting classes and functions is fantastic.

Give Variables, Functions and Classes Meaningful names
Nothing is worse than trying to figure out what a function does. Give everything a name that if someone else looked at it, they could figure out what it does.
Example:
function fetch_user_info();
Never Delete – Comment out
This one is … Continue Reading

Speeding Up Your PHP Web Site

Google already announced that the speed of a web site is now used as a factor in determining its search rank. This is great news for the skillful PHP developer, since it allows you to gain an advantage over those not as dedicated to their art.

Following techniques you can easily adapt to your own web sites and web applications.

When I set out to speed up this web site, I used the Page Speed and YSlow plug-ins for Firebug as a basis for determing what to do. As a result, the key things that were looked at were as follows:

- Using … Continue Reading

Date Compare in PHP

Now a days date comparison in PHP becomes much easy.
Simply convert your dates to unix timestamp, compare them with each other and you will get your result.

Code Snippet:
<?php
$prev_date = “2010-01-02″;
$todays_date = date(“Y-m-d”);

$today = strtotime($todays_date);
$result_date = strtotime($prev_date);

if ($result_date > $today) {
$valid = “Valid”;
} else {
$valid = “Invalid”;
}
?>

URL rewriting using .htaccess – examples

If you are looking for URL rewriting code, following examples will help you.

1] Rewriting category.php?id=12 to category-12.html
It’s a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^category-([0-9]+)\.html$ category.php?id=$1
2] Rewriting category.php?id=12 to category/electronics/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique, you can display the name of the category in URL.
RewriteEngine on
RewriteRule ^category/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ category.php?id=$2
3] Redirecting non www URL to www URL (http:// to http://www.)
If user type kapilsakhare.in in browser it will be redirected to … Continue Reading

How to Fetch FeedBurner feed with PHP and CURL

The code below fetch the feed contents using PHP and CURL request using SimpleXML.
<?php
// URL location of your feed
$feedUrl = “http://feeds.feedburner.com/kapsblog?format=xml”;
$feedContent = “”;

// Fetch feed from URL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $feedUrl);
curl_setopt($curl, CURLOPT_TIMEOUT, 3);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);

// FeedBurner requires a proper USER-AGENT…
curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
curl_setopt($curl, CURLOPT_ENCODING, “gzip, deflate”);
curl_setopt($curl, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3″);

$feedContent = curl_exec($curl);
curl_close($curl);

// check if content received
if($feedContent && !empty($feedContent)) {
$feedXml = @simplexml_load_string($feedContent);
if($feedXml) {
?>
<h2>From The Blog…</h2>
<ul>
<?php foreach($feedXml->channel->item as $item) { ?>
<li style=”padding:4px 0;”><a href=”<?php echo $item->link; ?>”><?php echo $item->title; ?></a></li>
<?php } // end foreach ?>
</ul>
<?php
} // end if
} … Continue Reading

Auto Optimize Your MySQL Tables Script

To make MySQL driven websites running fast, I’ve pieced together a script and cron job that will save you some support calls.

Step 1: Create a PHP script:
<?php
// Change vars as needed here
$server = “localhost”;
$user = “mysql_user”;
$pwd = “mysql_password”;
$dbName = “mysql_dbName”;

$link = mysql_connect($server, $user, $pwd);

if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}

$db_selected = mysql_select_db($dbName, $link);

if (!$db_selected) {
die (‘Can\’t use $dbName : ‘ . mysql_error());
}

// Find all tables in the selected DB
$alltables = mysql_query(“SHOW TABLES”);

// Process all tables.
while ($table = mysql_fetch_assoc($alltables))
{
foreach ($table as $db => $tablename)
{
Continue Reading

Few Tips for Optimizing PHP code

1. If a method can be static, declare it static. Speed improvement is by a factor of 4.

2. echo is faster than print.

3. Use echo’s multiple parameters instead of string concatenation.

4. Set the maxvalue for your for-loops before and not in the loop.

5. Unset your variables to free memory, especially large arrays.

6. Avoid magic like __get, __set, __autoload

7. require_once() is expensive

8. Use full paths in includes and requires, less time spent on resolving the OS paths.

9. If you need to find out the time when the script started executing, INSERT:CONTENT:END SERVER['REQUEST_TIME'] is preferred to time()

10. See if you can use … Continue Reading

Function to check BOT using php script

Here is the script to check bot crawling:
function is_bot(){
$botlist = array(“Teoma”, “alexa”, “froogle”, “Gigabot”, “inktomi”,
“looksmart”, “URL_Spider_SQL”, “Firefly”, “NationalDirectory”,
“Ask Jeeves”, “TECNOSEEK”, “InfoSeek”, “WebFindBot”, “girafabot”,
“crawler”, “www.galaxy.com”, “Googlebot”, “Scooter”, “Slurp”,
“msnbot”, “appie”, “FAST”, “WebBug”, “Spade”, “ZyBorg”, “rabaz”,
“Baiduspider”, “Feedfetcher-Google”, “TechnoratiSnoop”, “Rankivabot”,
“Mediapartners-Google”, “Sogou web spider”, “WebAlta Crawler”,”TweetmemeBot”,
“Butterfly”,”Twitturls”,”Me.dium”,”Twiceler”);

foreach($botlist as $bot){
if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
return true; // Is a bot
}

return false; // Not a bot
}