Magento Engineer
social
Codeigniter

Codeigniter

The industry's top wizards and other experts share their advice and research findings.

Ad comes here

How do you Encrypt and Decrypt a PHP String?

/**Function to encrypt or decrypt the given value**/
/**Create a function for Encryption url**/

function encrypt_decrypt($string){   
    $string_length=strlen($string);
    $encrypted_string="";     
    for ($position = 0;$position<$string_length;$position++){         
        $key = (($string_length+$position)+1);
        $key = (255+$key) % 255;
        $get_char_to_be_encrypted = SUBSTR($string, $position, 1);
        $ascii_char = ORD($get_char_to_be_encrypted);
        $xored_char = $ascii_char ^ $key;  //xor operation
        $encrypted_char = CHR($xored_char);
        $encrypted_string .= $encrypted_char;
    }
    /***Return the encrypted/decrypted string***/
    return $encrypted_string;
 }

/*** While passing the unique value to a link- Do the following steps ***/
$pid=88;//Let's 88 is the actual id
/***For more security multiply some value-You can set the multiplication value in config file*/
$passstring=$pid*12345;
$encrypted_string=encrypt_decrypt($passstring);
$param=urlencode($encrypted_string);

/*** Add this url to your anchor link***/
$url='your_target_path.php?id='.$param;
 
/*** While fetching the params in the target file- Do the following steps ***/
$getid=$_GET['id'];
$passstring=urldecode(stripslashes($getid));
$decrypted_string= encrypt_decrypt($passstring);
/***Divide the decrypted value with the same value we used for the multiplication***/
$your_actual_id= $decrypted_string/12345;

/*** Now fire your MySql query by this ID***/

Ad comes here

How to validate Alpha Numeric, special charecter and minimum 8digit password validation in CodeIgnetter ?

CodeIgniter Strong Password Validation:

public function forgotpassword(){    
        if(isset($_POST['submit']) && $_POST['submit']=='Submit'){    
            $valid = array(
                    array(
                    'field' => 'userpassword',
                    'label' => 'User password',
                    'rules' => 'callback_valid_password',
                    ),
                    array(
                        'field' => 'userpassword1',
                        'label' => 'Confirm Password',
                        'rules' => 'matches[userpassword]',
                    ),
                  );
            $this->form_validation->set_rules($valid);
            if($this->form_validation->run() == TRUE) {        
                //Your success thing goes here
            }else{
                 //Your error thing goes here
            }
        }
}

//Now create a function for checking password
public function valid_password($userpassword = ''){    
        $userpassword=trim($userpassword);    
        $regex_lowercase = '/[a-z]/';
        $regex_uppercase = '/[A-Z]/';
        $regex_number = '/[0-9]/';
        $regex_special = '/[!@#$%^&*()\-_=+{};:,<.>§~]/';        

        if(empty($userpassword)){
            $this->form_validation->set_message('valid_password', 'The password field is required.');
            return FALSE;
        }        
        if (preg_match_all($regex_lowercase, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must be at least one lowercase letter.');
            return FALSE;
        }
        if (preg_match_all($regex_uppercase, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must be at least one uppercase letter.');
            return FALSE;
        }
        if (preg_match_all($regex_number, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must have at least one number.');
            return FALSE;
        }
        if (preg_match_all($regex_special, $userpassword) < 1){
            $this->form_validation->set_message('valid_password', 'The field must have at least one special character.' . ' ' . htmlentities('!@#$%^&*()\-_=+{};:,<.>§~'));
            return FALSE;
        }
        if (strlen($userpassword) < 8){
            $this->form_validation->set_message('valid_password', 'The field must be at least 8 characters in length.');
            return FALSE;
        }
        if (strlen($userpassword) > 32){
            $this->form_validation->set_message('valid_password', 'The field cannot exceed 32 characters in length.');
            return FALSE;
        }
            return TRUE;    
}

Ad comes here
Error's and solution's
Update Cookies Preferences