Un poco de código PHP
Aquí van unos snippets de código PHP que pueden hacernos más sencillas algunas tareas a la hora de llevar nuestra web:
Validar una dirección de email:
include('EmailAddressValidator.php'); $validator = new EmailAddressValidator; if ($validator->check_email_address('test@example.org')) { // Email address is technically valid } else { // Email not valid }
Conseguir la dirección IP:
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; }
Generador de contraseñas:
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; }
Enviar un email:
include("class.phpmailer.php"); $mail = new PHPMailer(); $mail->From = 'noreply@htmlblog.net'; $mail->FromName = 'HTML Blog'; $mail->Host = 'smtp.site.com'; $mail->Mailer = 'smtp'; $mail->Subject = 'My Subject'; $mail->IsHTML(true); $body = 'Hello<br/>How are you ?'; $textBody = 'Hello, how are you ?'; $mail->Body = $body; $mail->AltBody = $textBody; $mail->AddAddress('asvin [@] gmail.com'); if(!$mail->Send()) echo 'There has been a mail error !';
Más código útil en The HTML Blog



No Comments Canal de comentarios
Add a Comment