Archive for the Category » Framework «

Add Security Math to Protect Unauthorized Access

You may need to add security math while visitor post their comments. Or even more sensitive admin login panel. whatever the case is here is php code which take a minute to implement security math.

View File(form_login_view.php):
<?php echo $x=rand(1,10);?> + <?php echo $y=rand(11,20);?> =
<?php $this->session->set_userdata(‘security_math_result’ , $x+$y);?>
[input type="text" name="security_math_result" value="<?php echo set_value('security_math_result', $default['security_math_result']); ?>” size=”5″ ]

Controller (auth.php):
if ($_POST)
{
if($this->session->userdata(‘security_math_result’)!=$_POST['security_math_result'])
{
$data['msg_class'] = ‘error’;
$data['msg'] = “Security Math is incorrect. Please check below.”;
$data['default'] = $_POST;
}
…… other codes…….

 

A quick start of Symfony2 from Symfony.com

I was looking for an easy to understand Symfony2 tutorial. I found a very good resource in symfony website where they explain basics of symfony using easy example. They start from a spaghetti php coding and convert into strong symfony2 application by step by step conversion. I like the tutorial very much and will try to understand clearly very soon(hopefully from tomorrow).

Here is the link of that crazy tutorial.

Another good & descent basic concept of symfony2 link is here.

Install PHP5-CLI in WAMP at Win-XP

 

let us have a quick start of CLI(Command Line Interface) activation/installation in WAMP(Windows Apache MySQL & PHP). for me windows xp is my operation system. however, lets start step by step.

  1. Install windows XP server pack II
  2. Install wamp (version WampServer2.1e-x32.exe, Apache 2.2.17,PHP 5.3.5,MySQL 5.5.8). i haven’t change the default installation location. but you may change it as you need.
  3. let’s open: C:\wamp\bin\php\php5.2.5\php.ini and remove “;” from the line:  ;extension=php_xsl.dll
  4. let’s open: C:\wamp\bin\php\php5.2.5\php.ini and remove “;” from the line: ;extension=php_openssl.dll
  5. Click WAMP Icon Apache > Apache Modules > rewrite_module (tick mark by click). WAMP will restart.
  6. Now we should add environment variables to have access to PHP i MySQL under the command line. Right-click on My Computer, than Properties. Switch to Advanced tab and click the Environment Variables button. At the end of variable PATH let’s add ;C:\wamp\bin\php\php5.3.5;C:\wamp\bin\mysql\mysql5.5.8\bin (paths to MySQL and PHP files separated by a semicolon).
  7. edit the ‘go-pear.bat’ file at C:\wamp\bin\php\php5.3.0\go-pear.bat to look like the following(simply copy following 5 lines and replace all content of go-pear.bat
  8. @ECHO OFF
    set PHP_BIN=php.exe
    rem %PHP_BIN% -d output_buffering=0 PEAR\go-pear.phar
    php -d phar.require_hash=0 PEAR\go-pear.phar
    pause
  9. Download latest version of go-pear.phar from here and replace C:\wamp\bin\php\php5.3.5\PEAR\go-pear.phar file.
  10. open command prompt. Start->run->cmd press ender.
  11. at command prompt C:\wamp\bin\php\php5.3.5>go-pear.bat
  12. A command prompt window will be opened. press Enter to proceed.
  13. Afterward, a list of up to 10 (this length may differ on XAMPP and others) filesystem locations will be displayed. Once again press Enter.
  14. Then it will ask if we want to modify php.ini; Type in the letter “Y” (in capital letters) and press Enter.
  15. Double click C:\wamp\bin\php\php5.3.5\PEAR_ENV.reg file. When asked to confirm the addition of registry values choose ”Yes”and then click ”OK”.
  16. copy the file “C:\wamp\bin\php\php5.3.5\pear.bat” to “C:\WINDOWS\system32″.
  17. Reboot PC.
  18. Right-click on My Computer, than Properties. Switch to Advanced tab. Find variable php_pear_php_bin (which current value is .\php.exe) and change value to C:\wamp\bin\php\php5.3.5\php.exe
  19. Test PEAR installation by cmdprompt>pear. it output says unrecognized then you have to review above mentioned steps more carefully
  20. Whenever you update PEAR or some of its packages, copy pear.bat into system32 to update our list of PEAR commands. This extra step can be avoided if the path to pear.bat has been added to your PATH environment variable.
  21. CLI of PHP & PEAR SUCCESSFUL !! START SYMFONY !!
  22. Add symfony channel to pear by C:\>pear channel-discover pear.symfony-project.com
    (output: Adding Channel “pear.symfony-project.com” succeeded
    Discovery of channel “pear.symfony-project.com” succeeded)
  23. Install Symfony by C:\>pear install symfony/symfony (output: WARNING: channel “pear.symfony-project.com” has updated its protocols, use “pear
    channel-update pear.symfony-project.com” to update
    downloading symfony-1.4.13.tgz …
    Starting to download symfony-1.4.13.tgz (3,186,371 bytes)
    ……………………………………………………………………..
    ……………………………………………………………………..
    ……………………………………………………………………..
    ……………………………………………………………………..
    ……………………………………………………………………..
    ………….done: 3,186,371 bytes
    install ok: channel://pear.symfony-project.com/symfony-1.4.13)

Symfony installed correctly and now you are ready for Symfony project with the power of ORM (Object Relationship Mapping)

Let me start a project in Symfony.

Error: during PEAR installation by go-pear.bat you may get error in Windows 7, and get
Solution: it working by running the command prompt as administrator. Start -> Type in “cmd” -> right-click “cmd.exe” -> click “run as administrator”

Error:  phar “C:\wamp\bin\php\php5.3.5\PEAR\go-pear.phar” does not have a signaturePHP Warning:  require_once(phar://go-pear.phar/index.php): failed to open stream: phar error: invalid url or non-existent phar “phar://go-pear.phar/index.php” in C:\wamp\bin\php\php5.3.5\PEAR\go-pear.phar on line 1236PHP Stack trace: PHP   1. {main}() C:\wamp\bin\php\php5.3.5\PEAR\go-pear.phar:0
Solution: php -d phar.require_hash=0 ./PEAR/go-pear.phar (not sure)

  1. Double click C:\wamp\bin\php\php5.3.5\PEAR_ENV.reg file. When asked to confirm the addition of registry values choose ”Yes”and then click ”OK”.

Error:  Discovering channel pear.symfony-project.com over http:// failed with message: channel-add: Cannot open “http://pear.symfony-project.com/channel.xml” (Connectio
n to `pear.symfony-project.com:80′ failed: php_network_getaddresses: getaddrinfo
failed: No such host is known. ) Trying to discover channel pear.symfony-project.com over https:// instead Discovery of channel “pear.symfony-project.com” failed (channel-add: Cannot open  “https://pear.symfony-project.com/channel.xml” (Connection to `pear.symfony-project.com:443′ failed: Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?))
Solution: let’s open: C:\wamp\bin\php\php5.2.5\php.ini and remove “;” from the line: ;extension=php_openssl.dll (Step-4)

Some Resourceful/Helpful Links:

http://trac.symfony-project.org/wiki/HowToInstallPearOnWindowsWithWamp

http://trac.symfony-project.org/wiki/HowToInstallSymfonyOnWindowsWithWamp

http://www.sencha.com/forum/showthread.php?120958-Php-Pear-Hell-can-t-install-Logger-%28Solved%29

Category: Symfony  Leave a Comment

Convert CI version 1.7 to 2.0

You may have some projects build using Code Igniter version 1.7.1 / 1.7.2 or earlier. And now Code Igniter 2+ version released. So eventually a question should arise in your mind “How to convert old projects into new version easily”

Here are 8 steps by which you can easily make compatible your old projects into new version of CI.

1.  Replace new system folder and index.php file.
2.  All Controllers now extend CI_Controller instead of Controller
3.  in all controllers constructor
function __construct()
replace parent::Controller(); by parent::__construct();
4.  All Models now extend CI_Model not extend Model
5.  All models constructor function __construct()
function Model() replace by function __construct()
6.  All model files replace orwhere(‘..’) by or_where (‘..’), orderby(‘..’) by order_by(‘..’), groupby(‘..’) by group_by(‘..’)  [ it's a change of CI 2+]
7. Open application/config/config.php,
Set encryption key like this:
$config['encryption_key'] = 'aminulsumonvi';
8. Create a folder named logs at application/logs and folder permission 0777

Enjoy your codeigniter version 1.7+ is now fully comply with version 2.0+.

Integrate Re-Captcha in CI

In order to implement Re-Captch in Code Igniter  you have to ad lines of code in
1. view file
2. controller
3. config file (2 files, config.php & form_validation.php)

View File (ex: form_login_view.php): more »

Character problem in CI active recordset

How to turn off single quote (‘) in database query?
Simply add false after query as follows:

$query = $this->db->where(“e.event_date>=” , “‘$today’”, false);

Third parameter automatically prevent to generate single quote.

Setup Port Based WAMP Server

Step 1: Open c:\wamp\bin\apache\apache2.2.*\conf\httpd.conf

Step 2: httpd.conf-> NEAR line 469

# Virtual hosts

#Include conf/extra/httpd-vhosts.conf

replace by [remove front #]

# Virtual hosts

Include conf/extra/httpd-vhosts.conf

Step 3: Open c:\wamp\bin\apache\apache2.2.*\conf\extra\httpd-vhosts.conf

Step 4: httpd-vhosts.conf-> Remove all text in this file and add following as much as port you like to add

#

# Use name-based virtual hosting.

#

NameVirtualHost *:80

<virtualhost *:80>

ServerName localhost

DocumentRoot “D:\wamp\www”

</virtualhost>

Listen 81

<virtualhost *:81>

ServerName localhost

DocumentRoot “D:\wamp\www\job\public”

</virtualhost>

Step 5: Test by http://localhost:81 you will get job project.

Add new project: Go to httpd-vhost-> add 82 83 84 85 86 and pointing to the

directory where index.php is located

Best practice of sharing common variable among functions of a class

here is two ways of share value of variable all through member functions of a class

class Clients extends Controller {
function __construct(){
parent
::Controller();
$uri_array = $this->uri->uri_to_assoc(1);
$this->folder_id = $uri_array['folder']; //This not works
$this->session->set_userdata("folder_id",$uri_array['folder']);
}

public function sample() {
$folder_id
= $this->folder_id; //ERROR: Message: Undefined property: Clients::$folder_id
$folder_id = $this->session->userdata('folder_id');
}
}

Taking mysql db backup in a second

Easy way to backup of database(mysql) using CI

$this->load->dbutil();
$this->load->helper(‘download’);
$this->load->helper(‘file’);

$prefs = array(
‘tables’      => array(),  // Array of tables to backup.
‘ignore’      => array(‘ssc_dakhil_exam’),           // List of tables to omit from the backup
‘format’      => ‘zip’,             // gzip, zip, txt
‘filename’    => ‘mybackup.zip’,    // File name – NEEDED ONLY WITH ZIP FILES
‘add_drop’    => TRUE,              // Whether to add DROP TABLE statements to backup file
‘add_insert’  => TRUE,              // Whether to add INSERT data to backup file
‘newline’     => “\n”               // Newline character used in backup file
);

$backup =& $this->dbutil->backup($prefs);
//write_file(‘c:\mybackup.txt’, $backup);
force_download(‘mybackup.zip’, $backup);

Multiple image manipulation(resize/crop/thumb/rotate)in a single script using CI

This is really nightmare to sort out why loop operation get success only for first time. Say, with or without loop i want to resize two images in a single script. i write the resize code but when i execute i found only first image is resized but not second one. WHY??????

after search and search, R&D, test and test finally able to find out the reason. a single line of extra code resolve my issue. and now i am able to resize 100 images inside a loop or write code multiple times to manipulate multiple images. This is the extra line of code:

$this->image_lib->initialize($config);

and here i am croping 100 of images using CI

foreach (glob(“./newspaper-jobs/prothom-alo/2009-05-22/*.jpg”) as $filename)
{
list($width, $height, $type, $attr) = getimagesize($filename);
//here are some logic. set it as you need or discard this portion but adjust variables by yourself.
$crop_x_axis = 0;
if($width<$height)
{
$crop_height_width = $width;
}
else // $width>$height
{
$crop_height_width = $height;
$crop_x_axis = ($width-$height)/2;
}

unset($config);
//Crop an image(Height = Width) Depend on current Height and Width of image
$config['image_library'] = ‘gd2′;
echo $config['source_image'] = ‘./source/image/location/’.basename($filename);
$config['new_image'] = ‘./destination/loc/’.basename($filename);
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
echo $config['width'] = $crop_height_width;
echo $config['height'] = $crop_height_width;
echo $config['x_axis'] = $crop_x_axis;
$config['y_axis'] = 0;

$this->load->library(‘image_lib’, $config);
$this->image_lib->initialize($config);

if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}

Enjoy image resizing/croping/or whatever it is !!!