Usuario: admin - Webeame Snippets


Añadir snippet

Últimos snippets de admin



Contador simple de visitas

  1. function load_counter() {
  2. if (file_exists('counter.txt')){
  3. $n = file_get_contents('counter.txt');
  4. return intval($n);
  5. }
  6. return 0;
  7. }
  8.  
  9. function update_counter($i=1) {
  10.  
  11. $n = load_counter();
  12. $n += $i;
  13.  
  14. $fp = fopen('counter.txt', "w+");
  15. fwrite($fp, $n);
  16. fclose($fp);
  17.  
  18. return $n;
  19. }
Uso
  1. // count the current visit and output the count
  2. $visits = update_counter();
  3.  
  4. // just load the counter data
  5. $visits = load_counter();

En PHP contador visitas por admin hace on 30/1/08 | Comentarios



Escribir en fichero con Perl

// introduce aquí la descripción
  1. # Open file to write:
  2. open(FILEHANDLE, ">fichero.txt") or die 'cannot open file!';
  3.  
  4. # Modes:
  5. #
  6. # > = Write mode (overwrites file content)
  7. # >> = Append mode - adds the content to the file
  8. # < = read only mode
  9. # +>> = read/append mode
  10. # +> = read/write mode
  11.  
  12. # write to File
  13. print FILEHANDLE "Line 1n";
  14. print FILEHANDLE "Line 2n";
  15.  
  16. # close file handle
  17. close(FILEHANDLE);

En Perl escribir fichero por admin hace on 30/1/08 | Comentarios



Patrón Singleton en PHP5

El patrón Singleton se aplica a situaciones en las cuales hay la necesidad de tener una sola instancia de una clase. El ejemplo más común de esto es una conexión de base de datos. Tendremos una simple instancia fácilmente accesible a muchos otros objetos.
  1. <?php
  2. class Example
  3. {
  4. // Hold an instance of the class
  5. private static $instance;
  6.  
  7. // A private constructor; prevents direct creation of object
  8. private function __construct()
  9. {
  10. echo 'I am constructed';
  11. }
  12.  
  13. // The singleton method
  14. public static function singleton()
  15. {
  16. if (!isset(self::$instance)) {
  17. $c = __CLASS__;
  18. self::$instance = new $c;
  19. }
  20.  
  21. return self::$instance;
  22. }
  23.  
  24. // Example method
  25. public function bark()
  26. {
  27. echo 'Woof!';
  28. }
  29.  
  30. // Prevent users to clone the instance
  31. public function __clone()
  32. {
  33. trigger_error('Clone is not allowed.', E_USER_ERROR);
  34. }
  35.  
  36. }
  37.  
  38. ?>
Esto permite que se obtenga una simple instancia de la clase Example.
  1. <?php
  2. // Error porque el constructor es privado
  3. $test = new Example;
  4.  
  5. // Devuelve siempre la misma instancia
  6. $test = Example::singleton();
  7. $test->bark();
  8.  
  9. // Lanzará un error
  10. $test_clone = clone($test);
  11.  
  12. ?>

En PHP patrón singleton por admin hace on 30/1/08 | Comentarios



Patrón Factory method en PHP5

El patrón Factory permita la instancia de objetos en tiempo de ejecución. Es llamado el patrón Factory puesto que es responsable de "manufacturar" un objeto.
  1. <?php
  2. class Example
  3. {
  4. // The factory method
  5. public static function &factory($type)
  6. {
  7. if (include_once 'Drivers/' . $type . '.php') {
  8. $classname = 'Driver_' . $type;
  9. return new $classname;
  10. } else {
  11. throw new Exception ('Driver not found');
  12. }
  13. }
  14. }
  15. ?>
Al definir este método en una clase se nos permite que los drivers sean cargados al vuelo. Si la clase Example fuera una clase de abstracción de base de datos, cargar un manejador de MySQL y SQLite podría ser hecho como sigue:
  1. <?php
  2. // Load a MySQL Driver
  3. $mysql = Example::factory('MySQL');
  4.  
  5. // Load a SQLite Driver
  6. $sqlite = Example::factory('SQLite');
  7. ?>

En PHP patrón factory method php5 por admin hace on 30/1/08 | Comentarios



Ejemplo básico para generar una imagen con GD

  1. <?php
  2.  
  3. // Definimos los headers
  4. header("Content-type: image/gif");
  5.  
  6. // Creamos la imagen
  7. $imagen = imagecreate(400,300);
  8.  
  9. // Agregamos contenido
  10. $blanco = imagecolorallocate($imagen,255,255,255);
  11. $negro = imagecolorallocate($imagen,0,0,0);
  12. $rojo = imagecolorallocate($imagen,200,0,0);
  13. $verde = imagecolorallocate($imagen,0,130,0);
  14. $gris = imagecolorallocate($imagen,140,140,140);
  15.  
  16. imagefilledrectangle($imagen,50,50,145,250,$verde);
  17. imagefilledrectangle($imagen,255,50,350,250,$rojo);
  18. imagefilledellipse($imagen,200,150,80,80,$gris);
  19. imagerectangle($imagen,50,50,350,250,$negro);
  20.  
  21. // Damos salida a la imagen
  22. imagegif($imagen);
  23.  
  24. // Destruimos la imagen
  25. imagedestroy($imagen);
  26.  
  27. ?>
http://www.washeebo.com/sargento/03_php/0301/0301.php

En PHP GD imagen por admin hace on 30/1/08 | Comentarios



Transparencia con CSS

// introduce aquí la descripción
  1. #caja {
  2. opacity: 0.2;
  3. -moz-opacity: 0.2;
  4. filter: alpha(opacity=20);
  5. }
- La especificación CSS3 tiene la propiedad opacity, la cual toma valores entre 0 (invisible) y 1 (opaco). - Los navegadores Mozilla poseen la propiedad -moz-opacity que toma los mismos valores que la propiedad anterior. - Internet explorer posee el filtro opacity que toma valores entre 0 y 100.

En CSS transparencias por admin hace on 30/1/08 | Comentarios



Esquinas redondeadas con Prototype

  1. <script>
  2.  
  3. var Rounded = Class.create();
  4. Rounded.prototype = {
  5. initialize : function(el){
  6. var x, el = $(el), background = el.style.backgroundColor;
  7.  
  8. for(x=0;x<11;x++){
  9. var d = "<div style='background: " + background + "; border-style: solid; border-color: white; height: 1px; overflow: hidden; border-width: 0 " + this.index[x] + "px'>&nbsp;</div>";
  10. new Insertion.Top(el, d);
  11. new Insertion.Bottom(el, d);
  12. }
  13. },
  14. index : new Array(0,1,1,1,2,2,3,4,5,7,10)
  15. }
  16.  
  17. new Rounded('blah');
  18.  
  19. </script>

En JavaScript esquinas prototype por admin hace on 30/1/08 | Comentarios



Hack CSS para Internet Explorer 7

Un asterisco delante de la propiedad CSS y así sólo será reconocida por Internet Explorer 6 y 7
  1. body {
  2. background: #fff; /* Todos los navegadores */
  3. *background: #000; /* IE6 e IE7 */
  4. }
Usar !important cuando no queremos dar cierta propiedad a IE6
  1. body {
  2. background: #fff !important; /* Firefox, IE7 y los demás */
  3. background: #000; /* IE6 y anteriores */
  4. }
Hack exclusivo para IE7
  1. body {
  2. background: #fff !important; /* Firefox y los demás */
  3. *background: #000 !important; /* Sólo IE7 */
  4. *background: #ccc; /* Sólo IE6 */
  5. }
Más en http://granimpetu.com/articulos/hack-css-para-internet-explorer-7/

En CSS hacks por admin hace on 30/1/08 | Comentarios



htaccess para protegerse contra el Hotlinking

Visto en http://www.perraco.com/protegete-de-manera-efectiva-contra-el-hotlinking/
  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteBase /
  4.  
  5. # Options +FollowSymlinks
  6.  
  7. RewriteCond %{REQUEST_FILENAME} .(jpg|png|gif|bmp)$ [NC]
  8. RewriteCond %{HTTP_REFERER} !^$ [NC]
  9. RewriteCond %{HTTP_REFERER} !tu_ web.com [NC]
  10. RewriteCond %{HTTP_REFERER} !feedburner.com [NC]
  11. RewriteCond %{HTTP_REFERER} !bloglines.com [NC]
  12. RewriteCond %{HTTP_REFERER} !newsgator.com [NC]
  13. RewriteCond %{HTTP_REFERER} !netvibes.com [NC]
  14. RewriteCond %{HTTP_REFERER} !newsalloy.com [NC]
  15. RewriteCond %{HTTP_REFERER} !gritwire.com [NC]
  16. RewriteCond %{HTTP_REFERER} !rojo.com [NC]
  17. RewriteCond %{HTTP_REFERER} !blogrovr.com [NC]
  18. RewriteCond %{HTTP_REFERER} !alesti.org [NC]
  19. RewriteCond %{HTTP_REFERER} !fastladder.com [NC]
  20. RewriteCond %{HTTP_REFERER} !google. [NC]
  21. RewriteCond %{HTTP_REFERER} !yahoo. [NC]
  22. RewriteCond %{HTTP_REFERER} !msn. [NC]
  23. RewriteCond %{HTTP_REFERER} !ask. [NC]
  24. RewriteCond %{HTTP_REFERER} !altavista. [NC]
  25. RewriteCond %{HTTP_REFERER} !attensa.com [NC]
  26. RewriteCond %{HTTP_REFERER} !search?q=cache [NC]
  27. RewriteRule (.*) http://www.otro_servidor.com/imagen_alt.jpg [NC,L]
  28.  
  29. </IfModule>

En Otros hotlinking htaccess apache por admin hace on 30/1/08 | Comentarios



Filtro de parámetros en PHP5

Comprobar y limpiar una variable POST
  1. <?php
  2. if (filter_has_var ( INPUT_POST , ’submit’)) {
  3. $submit = filter_input(INPUT_POST, ’submit’, FILTER_SANITIZE_SPECIAL_CHARS);
  4. }
  5. ?>
Validar email
  1. <?php
  2. var_dump(filter_var('nombre@correo.com', FILTER_VALIDATE_EMAIL));
  3. ?>
Más: http://www.anieto2k.com/2007/12/05/filtro-de-parametros-en-php5/

En PHP filter sanitaze parametros por admin hace on 30/1/08 | Comentarios