Last active
December 16, 2015 03:59
-
-
Save mpratt/5374028 to your computer and use it in GitHub Desktop.
Revisions
-
Michael Pratt revised this gist
May 14, 2013 . 2 changed files with 2 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,15 +7,6 @@ en vez de declarar la funcion antes. */ print_r(array_filter($misNumeros, function($n) { return ($n%2 != 0); })); /* Resulta en: Array([0] => 1, [2] => 3, [4] => 5) */ ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,6 +31,5 @@ $password = password_hash($_POST['password'], PASSWORD_BCRYPT, array('cost' => 12)); /* Seguimos usando Bcrypt, pero ahora con un costo de 12! */ ?> -
Michael Pratt created this gist
Apr 12, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ <?php // Tenemos un arreglo con números $misNumeros = array('1', '2', '3', '4', '5', '6'); /* Pero resulta que solo queremos los numeros impares, así que usamos array_filter con una función anonima, en vez de declarar la funcion antes. */ print_r(array_filter($misNumeros, function($n) { return ($n%2 != 0); })); /* Resulta en: Array ( [0] => 1 [2] => 3 [4] => 5 ) */ ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ <?php // Esta es la variable inicial, que ya tiene la & convertida en & $var = 'Me gusta la banda funkinberto & los Jacksons'; echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8'); /* Resulta en: Me gusta la banda funkinberto &amp; los Jacksons Ahí ya dañamos el HTML ya que & ahora es &amp */ echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8', false); /* Retorna el original: Me gusta la banda funkinberto & los Jacksons puesto que reconoce que la & ya fue convertida, entonces la deja pasar! */ ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ <?php function xrange($start, $end, $step = 1) { for ($i = $start; $i <= $end; $i += $step) { yield $i; } } echo 'Usando xrange (generador)'; foreach (xrange(1, 1000000) as $num) { echo $num . PHP_EOL; } /* Eso mismo se podría hacer con la función range, pero esa funcion crea el array completo con 1 millon de entradas y después la pasa al bucle, consumiendo así más memoria. La version que usa un generador va generando cada entrada una por una */ echo 'Usando la función range'; foreach (range(1, 1000000) as $num) { echo $num . PHP_EOL; } ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ <?php /* A la hora del registro solo hay que generar el hash y guardarlo en la base de datos. */ $hash = password_hash($_POST['password'], PASSWORD_DEFAULT); registrar_usuario($_POST['usuario'], $hash); /* Y luego en la parte de la autenticación, se puede usar: */ $hash = coger_hash_para_usuario($_POST['usuario']); if (password_verify($_POST['password'], $hash)) { echo 'El Password es Valido!'; } else { echo 'Tu password no es correcto!'; } /* sobra decir que la función registrar_usuario y coger_hash_para_usuario es algo que uno debería implementar por uno mismo. Usé funciones para que el ejemplo fuera claro, pero recomiendo crear una clase que se encargue del registro y otra de la autenticación */ /* Actualmente password_hash usa BCRYPT como default, con un costo de 10. Pero eso se puede cambiar fácilmente: */ $password = password_hash($_POST['password'], PASSWORD_BCRYPT, array('cost' => 12)); /* Seguimos usando Bcrypt, pero ahora con un costo de 12! Adicionalmente también se puede cambiar el algoritmo usado para hashear el password. */ ?> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ <!-- Este es un template en PHP generalmente para mostrar en pantalla el valor dentro de una variable haríamos: --!> <p>Hola <?php echo $name; ?></p> <!-- Ahora se puede hacer algo así, independiente si la directiva short_open_tag en el php.ini esta desactivada. --!> <p>Hola <?= $name; ?></p>