How to use MySQL to search in PHP serialized fields

Well, first rule – you should not do this. But if there is good reason, consider using such query for searching in index-based arrays:

SELECT * FROM table WHERE your_field_here REGEXP '.*;s:[0-9]+:"your_value_here".*'

In case you have assoc array serialized you can use:

SELECT * FROM table WHERE your_field_here REGEXP '.*"array_key_here";s:[0-9]+:"your_value_here".*'

Of course it won’t be very fast but in small tables should be enough

Source:
http://www.blastar.biz/2013/11/28/how-to-use-mysql-to-search-in-php-serialized-fields/

MySQL: Select records from today, this week and this month

MySQL Logo

Assuming your date column is an actual MySQL date column:

SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY score DESC;        
SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK) ORDER BY score DESC;
SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY score DESC;

Source: http://stackoverflow.com/a/5293226/1794834

Verify Item Purchases with the Envato API

This article focuses on how to simply and efficiently converse with Envato’s API in order to verify authors’ customers’ purchase codes in the author’s own applications. I talk through all the steps to setting up this function, covering how to create the unique API key required, using the PHP cURL function and interpreting the output.

<?php
    function verify_envato_purchase_code($code_to_verify) {
    // Your Username
    $username = 'USERNAME';
     
    // Set API Key  
    $api_key = 'API KEY';
     
    // Open cURL channel
    $ch = curl_init();
      
    // Set cURL options
    curl_setopt($ch, CURLOPT_URL, "http://marketplace.envato.com/api/edge/". $username ."/". $api_key ."/verify-purchase:". $code_to_verify .".json");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      
    // Decode returned JSON
    $output = json_decode(curl_exec($ch), true);
      
    // Close Channel
    curl_close($ch);
      
    // Return output
    return $output;
}
 
$purchase_key = 'PURCHASE KEY TO CHECK';
 
$purchase_data = verify_envato_purchase_code( $purchase_key );
 
if( isset($purchase_data&#91;'verify-purchase'&#93;&#91;'buyer'&#93;) ) {
     
    echo '<strong>Valid License Key!</strong><br>Details;<ul>';
    echo '<li>Item ID: ' . $purchase_data['verify-purchase']['item_id'] . '</li>';
    echo '<li>Item Name: ' . $purchase_data['verify-purchase']['item_name'] . '</li>';
    echo '<li>Buyer: ' . $purchase_data['verify-purchase']['buyer'] . '</li>';
    echo '<li>License: ' . $purchase_data['verify-purchase']['licence'] . '</li>';
    echo '<li>Created At: ' . $purchase_data['verify-purchase']['created_at'] . '</li>';
    echo '</ul>'; 
}
else echo 'Invalid license key.';
?>

Sources:
http://www.wpeka.com/verify-item-purchases-envato.html
http://hbt.io/verify-item-purchases-envato/

CakePHP How to change the X-Mailer

Basically, on your CakeEmail object:

$CakeEmail = new CakeEmail();
$CakeEmail->addHeaders(array('X-Mailer' => 'My custom X-Mailer'));

If set manually Cake will not add his default value ‘CakePHP Email’.

Source:
http://stackoverflow.com/a/18032036

Verify hex color string

Verify hex color string

Verify hex color string

This php script checks is the string $color is a hex color or not. The second IF statement will add the pound symbol at the beginning if it is missing from the hex color value.

<?php

//Check for a hex color string '#c1c2b4'
if(preg_match('/^#&#91;a-f0-9&#93;{6}$/i', $color)) //hex color is valid
{
      //Verified hex color
} 

//Check for a hex color string without hash 'c1c2b4'
else if(preg_match('/^&#91;a-f0-9&#93;{6}$/i', $color)) //hex color is valid
{
      $fix_color = '#' . $color;
} 

?>

Source:
http://code.hyperspatial.com/250/verify-hex-color-string/

Cakephp 2.0 default hash type

The default hash type of Cakephp 2.0 is sha1. But when you have set a Security.Salt in the core.php this will be added before your password and before the encryption is done.

To generate a password with Salt you can either create an User::add method in Cakephp and use the Authcomponent or create some your own. I needed it for fast checking user accounts with a javascript frontend (no views are used).

// Your salt from core.php
// Configure::write('Security.salt', 'e4ac429040b9d4552bc425b677b64aca104c5f84f');
$salt = 'e4ac429040b9d4552bc425b677b64aca104c5f84f';
$yourpassword = 'mysecret';
 
$password = sha1($salt.$yourpassword);
// The password to use in Cakephp with the Authcomponent
print $password;

Thats all, you use that password in your mysql admin tool to create users on the fly.

Source

PHP: CHMOD a directory recursively

The script below loops over the specified directory and chmods its files, directories and subdirectories recursively.

<?php

  function chmodDirectory( $path = '.', $level = 0 ){  
  $ignore = array( 'cgi-bin', '.', '..' ); 
  $dh = @opendir( $path ); 
  while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory 
  if( !in_array( $file, $ignore ) ){

        if( is_dir( "$path/$file" ) ){

          chmod("$path/$file",0755);

          chmodDirectory( "$path/$file", ($level+1));

        } else {

          chmod("path/$file",0644); // desired permission settings

        }//elseif 
	}//if in array 
	}//while 
	
	closedir( $dh ); 
	}//function
	chmodDirectory("the_directory/",0);

?> 

Incrementing a field in CakePHP like Hits & Votes

Incrementing a field in CakePHP like Hits & Votes

Incrementing a field in CakePHP like Hits & Votes

Let’s say you have an application, where users can place votes for their favorite products and you’d like to increment the current number of votes by one or count number of hits(Number of page views)

It’s very easy by using updateAll:

$this->Product->updateAll(array('Product.vote'=>'Product.vote+1'), array('Product.id'=>40));

You may want to restrict Product model by using unbindModel, containable, etc.

Also, you don’t have to pass in the second argument to updateAll, if you wish to update all records in your table.

Page 1 of 3123

Pin It on Pinterest