PHP
Compress css files using php
Have you ever noticed the css files from giants like google, yahoo or facebook. If you look at them you will see they are very tightly coded no space, tabs or line breaks yet a very large css file. How they manage it to debug when it is not human friendly to view the code.
5 CommentsClean URLs by removing /index.php/ from urls in codeigniter
In codeigniter you can easily use very search engine friendly urls.
By default it is shown as
http://www.yoursite.com/index.php/controller_name/function/id
You can easily remove this disgusting index.php from your urls.
3 CommentsExecute shell command on server using php
Accessing shell command in server may not be available for you at all due to security reasons. But still you can access it using php since this will give you some extra features and time saving efforts. One of the example is to creating backup of your files. You might want to create backup of your files in gz format. But you may not have this feature in your cpanel or being banned by your hosting company. If you can make access to the shell of your server and execute tar command who can stop you from??!!
No Comments3 important settings to make with custom php.ini
PHP.INI is the php configuration file that server uses. It makes lots of settings in an web server. It may include your site base directory, include path, temporary directory, timezone and lots of different php varaibles. Most of them are set by web hosts and you cannot make any change of them unless your are the host owner. But some setting you must make to let your site run smoothly and securely. So we must override some settings by putting an php.ini file. This will allow you to override some configurations of your host.
No CommentsCustom shopping cart library for codeigniter
I have created this shopping cart library for my super shop management application I created few days ago. I would like to share this with others. This library contains default functionality of creating shopping cart and holding info about products in user session.
<?php
/*********************************************
* Custom Shopping Cart for codeigniter *
* Author: Burhan Uddin *
* Date: 17 March, 2010 *
* Web: http://www.dscripts.net *
**********************************************/
class Shopping_cart {
public $ci; // holds reference pointer to CI object
public $cart = array(); // array to store cart
private $session_var = 'shopping_cart'; // name of the session variable to be used
// constructor
function Shopping_cart() {
$this->ci = &get_instance(); //put reference to codeigniter object for this application
$this->_get_cart(); // get the cart from session to array
}
/* Gets the cart from session
* access: private
*/
function _get_cart() {
$cart = $this->ci->session->userdata($this->session_var);
if(!empty($cart))
$this->cart = unserialize($cart);
}
/* saves the cart to session
* access: private
*/
function _save_cart() {
return $this->ci->session->set_userdata($this->session_var, serialize($this->cart));
}
/* add items to cart
* access: public
* @param: id = String or integer, unique id used to determine the product generally database id
* @param: quantity = Integer, quantity of product added in cart
*/
function add($id, $quantity) {
$this->cart[$id] = $quantity;
$this->_save_cart();
}
/* Updates cart specific item
* access: public
* @param: id = String or integer, unique id used to determine the product generally database id
* @param: quantity = Integer, quantity of product added in cart
*/
function update($id, $quantity) {
$this->cart[$id] = $quantity;
$this->_save_cart();
}
/* Removes items from cart
* access: public
* @param: id = String or integer, unique id used to determine the product generally database id
*/
function remove($id) {
unset($this->cart[$id]);
$this->_save_cart();
}
/* Clear all contents of cart
* access: public
*/
function clear() {
$this->cart = array();
$this->_save_cart();
}
/* Returns the total number of items in cart
* access: public
* return integer
*/
function count() {
return count($this->cart);
}
/* Returns the items array in cart
* access: public
* return array
* sample return data
* $cart = $this->shopping_cart->items();
* when integer id
* $cart = array(
* [1] => 3,
* [2] => 5
* )
* when string id
* $cart = array(
* [mango] => 3,
* [banana] => 5
* )
*/
function items() {
return $this->cart;
}
}
?>
1 CommentDecimal validation using codeigniter form validation class
Codeigniter is a really cool light weight php framework with lots of cool features. One of its cool features in codeigniter form validation. Using codeigniter you can set rules for specific fields and validate them without any hastle of rewritting validation code. Here is the manual of this validation library. If you dont have idea about it go and read.
http://codeigniter.com/user_guide/libraries/form_validation.html
So here we can see we can set validation rules for a input field like this
No CommentsValidating username in drupal
Have you ever noticed that drupal system allows any sort of username with and without spaces. Like “Hello World!”, “N/A”, “#MY SPECIAL && USER % NAME”
.I think you would never like to see these sort of usernames to be allowed in your website. But unfortunately drupal don’t provide any administration to validate username. The only way is to add an access rule from User Management > Access Rule and block each invalid characters. Which is really ineffecient and time consuming.
So what the solution is ?? frown
No CommentsBackup mysql database using php
When working with the library automation system of our university, we had to develop a system to backup database to sql file. After searchin in the net for a while we found two solution one is using exec to call mysqldump and generate the output file.
But we found another simple php script which will also generate sql output from database. Here is the code.
No CommentsDefining optional parameters in php functions
Defining option parameter is always been a great feature of php. Using it you can stop writting repeatative overloaded functions like java. On the other hand you can also define default value for parameter right inside function declaration
To create a optional paramert set it as $param=”” and if need a default value then set the value instead $param = “defaultValue”.
Just take a look at this function
No CommentsHow to find page execution time in php
To find out the execution time of page you have to use the function “microtime()” . This returns the current time in microsecond.
So at the beginning of your page add this code.
$start_t = microtime(true);No Comments
