Articulos PHP
Octubre 06, 2008, 09:50:56 *
Bienvenido(a), Visitante. Por favor, ingresa o regístrate.

Ingresar con nombre de usuario, contraseña y duración de la sesión
Noticias: Canciones Traducidas - Daforos.com - Fondos Gratis - Portatiles - Hosting - - Recursos Gratuitos Webmaster - elhacker dragonjar - sexo gratis - letras de canciones - Trucos Gratis - Juegos Gratis - Sexe gratuit - Juegos De Coche - porno - Start - Page0 - Page1 - Page2 - Page3 - Page4 - Page5 - Page6 - Page7 - Page8 - Page9 - Page10Page11 - Page12 - Page13 - Page14 - Page15 - Page16 - Page17 - Page18 - Page19 - Page20Page21 - Page22 - Page23 - Page24 - Page25 - Page26 - Page27 - Page28 - Page29 -

Páginas: 1   Ir Abajo
  Imprimir  
Autor Tema: Contador de visitas  (Leído 3684 veces)
administrador
Administrator
Hero Member
*****
Desconectado Desconectado

Mensajes: 16726


Ver Perfil
« : Septiembre 16, 2004, 09:35:44 »

Este script abre un archivo de texto que está en el servidor, lee un dato de el (el número de visitas), incrementa el número en 1 y lo muestra por pantalla.

Para leer el archivo se usa el siguiente código:


$file = 'your file location goes here';  # must be readable/writable by your webserver's user
open (FILE, "+>>" . $file) or die "cannot open $file for reading and appending: $!";
flock(FILE, 2) or die "cannot lock $file exclusively: $!";


Una vez abierto el archivo nos debemos mover al inicio de este para leer su contenido completo:


seek FILE, 0, 0;


Y con esto leeremos el texto:


my @file_contents = <FILE>;


Ahora debemos extraer el número. Para ello usamos expresiones regulares.


my $counterVar;

if ($file_contents[0] =~ /^(d+)$/)
{
   $counterVar = $1;  # $1 is captured by the ()'s in the regular expression
}
else
{
   $counterVar = 'COUNTER ERROR';  # the regular expression didn't match
}


Incrementamos el número:


$counterVar++;


Guardamos el número en el archivo de texto del servidor:


print FILE $counterVar; 
close (FILE);


Mostramos el número de visitantes por pantalla:


print STDOUT "Content-type: text/html ";  # this line is always needed!
print STDOUT "<STRONG>You are the " . $counterVar . "th visitor to this page.  Welcome!</STRONG><BR>" . " ";



Código entero del Script:


#!/usr/local/bin/perl -w


$file = 'your file location goes here';  # must be readable/writable by your webserver's user
open (FILE, "+>>" . $file) or die "cannot open $file for reading and appending: $!";
flock(FILE, 2) or die "cannot lock $file exclusively: $!";

seek FILE, 0, 0;

my @file_contents = <FILE>;

truncate FILE, 0;

my $counterVar;

if ($file_contents[0] =~ /^(d+)$/)
{
   $counterVar = $1;  # $1 is captured by the ()'s in the regular expression
}
else
{
   $counterVar = 'COUNTER ERROR';  # the regular expression didn't match
}

$counterVar++;  # auto-increment the same variable with 1
                # note: with this code, if counterVar is NOT an incrementable number, $counterVar will be set to 1.
                # that's not necessarily a bad thing.

print FILE $counterVar;
close (FILE);

print STDOUT "Content-type: text/html ";  # this line is always needed!
print STDOUT "<STRONG>You are the " . $counterVar . "th visitor to this page. Welcome!</STRONG><BR>" . " ";

end(0);

En línea
Páginas: 1   Ir Arriba
  Imprimir  
 
Ir a:  

Mas buscadas: apuntes audio belleza bolsa cancer carpet carrera casas computadora credito cross curso informatica divx dolar drivers e mule economia explorer grafica hardware higiene industria industrial informatica internet libros linux mantenimiento manuales media medicina nutricion online paginas web politica posicionamiento programacion red red alert salud seguro seo software tecnologia trucos windows universidad venta video web windows winrar

UseBB Port by Gaia Modified & Upgraded by Croco Articulos PHP | Impulsado por SMF 1.1.6.
© 2005, Simple Machines LLC. Todos los Derechos Reservados.

Página creada en 0.141 segundos con 19 consultas.