Blog de informática: programación, internet, php, wordpress, zend framework, mysql, windows, mootools, linux,…
In: php
7 sep 2009
De htmlblog.net, extraigo 7 de los 10 trozos de código que hay publicados, que son muy prácticos y usados a menudo en proyectos.
Algunos son funciones y otros son clases.
include('EmailAddressValidator.php'); $validator = new EmailAddressValidator; if ($validator->check_email_address('test@example.org')) { // Email correcto } else { // Email incorrecto }
function generatePassword($length=9, $strength=0) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength & 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; } if ($strength & 2) { $vowels .= "AEUY"; } if ($strength & 4) { $consonants .= '23456789'; } if ($strength & 8) { $consonants .= '@#$%'; } $password = ''; $alt = time() % 2; for ($i = 0; $i < $length; $i++) { if ($alt == 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password; }
function getRealIpAddr() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; }
header("Content-type: application/octet-stream"); // permite mostrar la barra de progreso mientras se descarga header("Content-Length: " . filesize('myImage.jpg')); header('Content-Disposition: attachment; filename="myImage.jpg"'); readfile('myImage.jpg');
include("class.phpmailer.php"); $mail = new PHPMailer(); $mail->From = 'tu@email.com'; $mail->FromName = 'Mi email'; $mail->Host = 'smtp.site.com'; $mail->Mailer = 'smtp'; $mail->Subject = 'Mi asunto'; $mail->IsHTML(true); $body = 'Hola Como estas?'; $textBody = 'Hola, como estas ?'; $mail->Body = $body; $mail->AltBody = $textBody; $mail->AddAddress('asdf[@] gmail.com'); if(!$mail->Send()) echo 'Ha habido un error en el envío del email!';
$uploadedImage = new Upload($_FILES['imagen_subida']); if ($uploadedImage->uploaded) { $uploadedImage->Process('mis_subidas'); if ($uploadedImage->processed) { echo 'El archivo ha sido subido'; } }
function dirList ($directory) { // create an array to hold directory list $results = array(); // create a handler for the directory $handler = opendir($directory); // keep going until all files in directory have been read while ($file = readdir($handler)) { // if $file isn't this directory or its parent, // add it to the results array if ($file != '.' && $file != '..') $results[] = $file; } // tidy up: close the handler closedir($handler); // done! return $results; }
Entradas relacionadas:
Este blog informático pretende ser un blog de notas o portafolio de información variada: trozos de código, descubrimientos, notas sueltas, ... Para tenerla a mano, y ser compartida.