Thursday, August 30, 2012

How to hide PHP Notice and Warning Messages

How to hide PHP Notice & Warning Messages ?
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (bitwise 63 may be used in PHP 3)
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

How you would encrypt decrypt

You should not encrypt passwords, instead you should hash them using an algorithm like bcrypt. Still, here is how you would encrypt/decrypt
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

$key = 'password to (en/de)crypt';
$string = ' string to be encrypted '; // note the spaces

//To Encrypt
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));

//To Decrypt:
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

echo 'Encrypted:' . "\n";
var_dump($encrypted); // "ey7zu5zBqJB0rGtIn5UB1xG03efyCp+KSNR4/GAv14w="

echo "\n";

echo 'Decrypted:' . "\n";
var_dump($decrypted); // " string to be encrypted "

?>

Simple PHP Function to Detect Mobile Users

Simple PHP Function to Detect Mobile Users: iPhone, iPad, Blackberry & Android
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

if( !function_exists('mobile_user_agent_switch') ){
function mobile_user_agent_switch(){
$device = '';

if( stristr($_SERVER['HTTP_USER_AGENT'],'ipad') ) {
$device = "ipad";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
$device = "iphone";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'blackberry') ) {
$device = "blackberry";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
$device = "android";
}

if( $device ) {
return $device; 
} return false; {
return false;
}
}
}

?>

date_sun_info

date_sun_info — Returns an array with information about sunset/sunrise and twilight begin/end

array date_sun_info ( int $time , float $latitude , float $longitude )

Example :
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

$northernmost_city_latitude = 78.92; // Ny-Ă…lesund, Svalbard
$northernmost_city_longitude = 11.93;
$southernmost_city_latitude = -77.88; // McMurdo Research Station, Antarctica
$southernmost_city_longitude = 166.73;

print_r( date_sun_info( strtotime("2008-01-01") , $northernmost_city_latitude, $northernmost_city_longitude) );
print_r( date_sun_info( strtotime("2008-04-01") , $northernmost_city_latitude, $northernmost_city_longitude) );
print_r( date_sun_info( strtotime("2008-01-01") , $southernmost_city_latitude, $southernmost_city_longitude) );
print_r( date_sun_info( strtotime("2008-06-01") , $southernmost_city_latitude, $southernmost_city_longitude) );

?>

Increase PHP Script Execution Time Limit

Every once in a while I need to process a HUGE file. Though PHP probably isn't the most efficient way of processing the file, I'll usually use PHP because it makes coding the processing script much faster. To prevent the script from timing out, I need to increase the execution time of the specific processing script. Here's how I do it.


<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

ini_set('max_execution_time', 300); //300 seconds = 5 minutes
set_time_limit(0);//no limit

?>

Monday, August 6, 2012

Create Dynamic Radio Group (HTML) with PHP

Creating radio elements in some content management systems can result into writing a lot of code. Just in case your radio group with multiple options is getting a value from a database and/or a form. There need to be a check for every posted value for every option. This function will do all this work for you, just create two arrays, one for the values and labels and for the related html code. It's up to the user if he uses 2 or more elements in one group. The function works with $_POST and $_GET data.

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

$values = array('google'=>'Google Search', 'link'=>'Link on some website', 'advert'=>'Advertisement', 'news'=>'News');
$html_elements = array('before'=>'', 'after'=>'
', 'label'=>'
');

function radioGroup($formelement, $values, $html, $def_value = '') {
    $radio_group = '
'."\n"; $radio_group .= (!empty($html['label'])) ? $html['label']."\n" : ''; if (isset($_REQUEST[$formelement])) { $curr_val = stripslashes($_REQUEST[$formelement]); } elseif (isset($def_value) && !isset($_REQUEST[$formelement])) { $curr_val = $def_value; } else { $curr_val = ""; } foreach ($values as $key => $val) { $radio_group .= $html['before']."\n"; $radio_group .= '' : ' />'; $radio_group .= ' '.$val."\n".$html['after']."\n"; } $radio_group .= '
'."\n"; return $radio_group; } // place this code between the form tags: // notice 'advert' could be a database value too echo radioGroup('test', $values, $html_elements, 'advert'); ?>
 

© 2014 4everTutorials. All rights resevered.

Back To Top