function getHash( $pass , $login ) {
$salt = substr( md5( $login ) , 0 , 10 );
return hash( 'sha256' , $pass . 'ThEStAtIcSaLe' . $salt );
}Remeber to use same function when checking user credentials.It will be also more secure to use slower hashing/crypting function
PHP Coding tips & tricks. Function & error explanation, coding style, how tos etc. Short notes posted directly from developer work environment.
PHP Coding Tips
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Thursday, March 28, 2013
PHP password hash with salt
It is more secure to stora a password hashed with added static (or even dynamic salt). To achieve static salt in password just append a string when calculating a hash. Example below shows dynamic salt, most secure:
Inheritance in PHP
Inheritance (extending classes) is realised in PHP with extends keyword:
class albumController extends MfStandardController {Now, object of class albumController will be of type albumController, but also of type MfStandardController, because MfStandardController is a parent of this class
Thursday, March 21, 2013
PHP: Get last element of array
Often you need to get the last element of PHP array. This is easy to achieve with
using count() function, wich counts all elements in array. Because PHP arrays are indexed from 0,
you must remeber to get "count()-1" element of an array, not "count()" element:
$exampleArray = array( 3,6,8,11,14 );
$lastElement = $array[ count($exampleArray) - 1];
echo $lastElement; // prints 14This is the obvious way for some begginer developers. But more experienced developers will know,
that there is a function defined especially for this purpose:
echo end( $exampleArray ); // prints 14
Because this function will move internal array pointer to the end, you can reset it if you are
currently iterating over this orray, for example:
reset( $exampleArray ); // prints 14
If you are afraid about non set array, or set as other type than array, you can use function is_array() to check
if this variable is defined as array. You can also get a last key of an array in several ways:
echo end(array_keys( $exampleArray) );
// or:
end( $exampleArray );
echo key( $exampleArray );
If you don't want to move internal array pointer, you can use another one-liner to get last element from array:
$last = array_slice($array, -1, 1, true);
How to quote in echo or print in PHP?
If you are creating applications in PHP, even if you are beginner, you definatelly see echo function already.
Echo function just outputs it's argument to a user browser or to a console/terminal if PHP script is running in
the CLI mode. The trouble rises, when you have to put ' or " sign in a echo, because you have to use one
of this characters just to begin and terminate echo literal. To embed such characters in displayer string,
you need to use escape sequence. It is a special characters combination, that makes some special sense in
string literal. So, if you want to send quote sign to user, not to terminate echo string, use \" escape sequence.
If your sequence is terminated by single quotes ('), then you can also include this character in string
by escape sequence \'. See examples below:
echo "This example shows \"escape sequences\" in php string, like quotes etc";
echo 'This example shows \'escape sequences\' in php string, like quotes etc';You can be confused if you think, what to do to put a slash in php string (\), because
it has some special meaning. But I have solution for you: this character also have some custom
escape sequence, and this sequence is: "\\".
Wednesday, January 30, 2013
Best way to secure e-mail in SMARTY template and CSS
See the best way to secure your e-mail from spambots. Just rearrange text flow to reverse and reverse an email with strrev() function:
<span style="unicode-bidi:bidi-override; direction: rtl;">
{$sm_item.email|strrev}
</span> Now you have your e-mail backwarded in page source, but displayed properly
Tuesday, August 7, 2012
Organize your input checking and error reporting. Escape from too many inner if's
It's common to check some variables and report error / unsucceful operation if value is bad or to check result of operations before going forward.
It can provide a bad code structure, where you can't find yourself in middle of a ton of bracets.
Try to use return or exit instead, this will provide a cleaner, maintable code:
function compute( $a , $b ) {
if ( $a <= 5 ) {
// outpu error
return;
}
if ( $b >= 3 ) {
// output error
return;
}
// logic
}As you can see, second solution is much more clear.
Organize your global functions to static methods - alternative to structural/procedural programming
If you are not into Object Oriented Programming, and include files and functions everywhere, you can get lost in your own function names.
The good alternative is to group your functions into classes with static methods:
class UserModule {
public static function getUserId() { ... }
public static function removeUser( $id ) { ... }
}
UserModule::removeUser( 11 );
How to merge (join) two arrayis in PHP together ?
You can merge all elements of two and more arrays into one array by array_merger func:
$summaricArray = array_merge( $firstPartArray , $secondPartArray );This results in all elements from both arrays in summaricArray
User not logged in PHP when requesting from flash etc. because of invalid session.
This is way to set a session ID. Remember to call session_id before session_start. This is helpful for common issue when request from flash uploader (uploadify etc.) causes errors, because requested PHP file doesn'y knows that user is logged (see flash request as new, non-logged user).
if ( isset( $_POST['sessionid'] ) ) {
session_id( $_POST['sessionid'] );
}
session_start();Remember that you must set sessionid on POST (or GET) request. This is useful when you connect to php page from other source than web browser (especially flash upload for example). You can pass your session ID to flash object 'flashvars' and when reqiesting upload or other page from SWF you can post/get session variable to access the same session normal web browser request has and for example be logged as current user.
How to split an array to parts by comma or special character (string to array) ?
Use explode function for this job:
$names = 'John,Margaret,Thomas';
$namesArray = explode( ',' , $names );
echo $namesArray[0];
echo $namesArray[1];
echo $namesArray[2];You can also use a character sequence to split string like a "splitplace"
How to access global variable in php ?
If you are in a function on class method scope, you can gain access to a global variable by using a global keyword.
$x = 5;
function myFunc() {
global $x;
echo $x;
}Remeber that using globals usually isn't a good idea. Better idea would be divide your globals by categories/modules and store it in static variables of some classes.
How to convert string to int, or other variable to integer
To convert string to int in PHP you can use casting or intval() function. See example:
$intVariable = intval( $unknownVariable );
// or:
$intVariable = (int)$unknownVariable;You can cast with this function every php variable, like double. Remember that php can throw a error or warning, especially if variable is not set
How to show informations about current PHP install
You can show informations about current php install by function:
phpinfo();This will give you to output PHP version, variables, installed modules and versions and also all configuration directiories, ports etc.
Subscribe to:
Posts (Atom)