Magento Engineer
social
Journal

Programming Blogs to Improve Your Coding Skills!

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

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

How to validate Alpha Numeric password validation ?

Must contain at least one number and one uppercase and lowercase letter and one special charectar, and at least 8 or more characters.

Method First:

<form action="#">
Password: <input type="password" name="password" id="password"
pattern="(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}"
title="Must contain at least one number and one uppercase and lowercase letter and one special charectar, and at least 8 or more characters">
<input type="submit">
</form>

Method second through PHP validation :

$uppercase = preg_match('@[A-Z]@', $password); //uppercase
$lowercase = preg_match('@[a-z]@', $password); //lowercase
$number    = preg_match('@[0-9]@', $password); //numbers
$special   = preg_match('@[!@#$%^&*/()\-_=+{};:,<.>]@', $password); //special charectar

if(!$uppercase || !$lowercase || !$number || !$special || strlen($password) < 8) {
// tell the user something went wrong
}

Ad comes here

How to write databse query in mysql,pdo and mysqli format?

Databse query in mysql,pdo and mysqli format.

MySQL = Popular open source relational database management system.
MySQLi - Improved PHP MySQL extension Read here
PDO - PHP extension similar to MySQLi. Provides object oriented way to access database

MySqli

$mysqli =new mysqli("example.com","user","password","database"); 
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc(); echo htmlentities($row['_message']);

PDO

$pdo =new PDO('mysql:host=example.com;dbname=database','user','password'); 
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC); echo htmlentities($row['_message']);

MySQL

$c = mysql_connect("example.com","user","password"); 
mysql_select_db("database");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result); echo htmlentities($row['_message']);

Ad comes here

PHP Interview question and answers.

PHP Interview question and answers for experienced.

Question: What is the difference between unset() and unlink() ?
Answer: unset() sets a variable to “undefined” while unlink() deletes a file we pass to it from the file system.

Question: Can the value of a constant change during the script’s execution?
Answer: No, the value of a constant cannot be changed once it’s declared during the PHP execution.

Question: How would you declare a function that receives one parameter name hello?
Answer: If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.
Example:
<?php
function showMessage($hello=false){
  echo ($hello)?'hello':'bye';
}
?>

Question: The value of the variable input is a string 1,2,3,4,5,6,7. How would you get the sum of the integers contained inside input?
Answer: There is no unique answer to this question, but the answer must be similar to this one.You can use explode function.
Example:
<?php
echo array_sum(explode(',',$input));
?>

Question: How can we know the number of days between two given dates using PHP?
Answer:
<pre>$date1 = date(‘Y-m-d’);
$date2 = ’2006-07-01′;
$days = (strtotime() &ndash; strtotime()) / (60 * 60 * 24);
echo “Number of days since ’2006-07-01′: $days”;</pre>

Question: What are the differences between DROP a table and TRUNCATE a table?
Answer:
DROP TABLE table_name – This will delete the table and its data.
TRUNCATE TABLE table_name – This will delete the data of the table, but not the table definition.

Question: What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row()?
Answer:
mysql_fetch_array:
Fetch a result row as an associative array and a numeric array.

mysql_fetch_object:
Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead. Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows.

mysql_fetch_row():
Fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

Ad comes here

Wordpress interview important question and answers.

Question1- What are the template tags in WordPress?

Answer. A template tag is code that instructs WordPress to “do” or “get” something. Like in header.php  we will use the tag bloginfo(‘name’) to get “Site Title” from wp-options table which is set in Setting > General at WordPress dashboard.

The the_title() template tag is used to display the post title.

wp_list_cats() is  for display categories.

get_header() for getting header.

get_sidebar() for display the sidebar on page.

get_footer() for get the footer content on page.


Question2- What do next_posts_link() and previous_posts_link() do?

Answer. Because post queries are usually sorted in reverse chronological order, next_posts_link() usually points to older entries (toward the end of the set) and previous_posts_link() usually points to newer entries (toward the beginning of the set).

Question3- How to retrieve an image attachment’s alt text?

Answer. The wp_get_attachment_image() function which will return an HTML string containing these attributes:

    1.src
    2.class
    3.alt
    4.title

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