Publicado el 27 de Febrero del 2017
1.045 visualizaciones desde el 27 de Febrero del 2017
260,0 KB
34 paginas
Creado hace 19a (27/11/2005)
Páginas WEB Accesibles
Breve introducción a PHP
Luis Fernando Llana Díaz
Departamento de Sistemas Informáticos y Programación
27 de noviembre de 2005
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Programación en el cliente
JavaScript
Applets de Java
Flash de Macromedia.
Características:
Se ejecutan en el cliente: el navegador directamente o con
plug-in.
Son elementos externos al HTML. Marcas: script, object.
JavaScript es interpretado en el ordenador, suele incluir
errores (páginas sólo para Internet Explorer).
Applets y Flash son tecnologías propietarias.... sólo los
propietarios pueden desarrollar plug-ins.
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Programación en el servidor
PHP,
JSP, Java Servlets
ASP,
Muchos más: perl, python....
Características
El cliente siempre recibe HTML, no necesita nada más que un
navegador.
Los cálculos se hacen en el servidor, en el lenguaje de
programación elegido.
Accesible, si el programador lo hace bien.
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
PHP: hola mundo I
/usr/share/doc/phpdoc/html/index.html
http://www.php.net/manual/es/
< html >
< head >
< title > Ejemplo de PHP </ title >
</ head >
< body >
<? php echo " <p > Hola Mundo </ p > " ; ? >
</ body >
</ html >
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
PHP: hola mundo II
El cliente recibe
< html >
< head >
< title > Ejemplo de PHP </ title >
</ head >
< body >
<p > Hola Mundo </ p >
</ body >
</ html >
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Saliendo de HTML
Un programa, la salida del programa se inserta en su lugar
<? php echo ( " si quieres servir d o c u m e n t o s XHTML " .
" o XML , haz como aqu & iacute ;\ n " ); ? >
El valor de una expresión:
<?= e x p r e s s i o n ? > Esto es una a b r e v i a t u r a
de " <? echo e x p r e s s i o n ? > "
<? php
if ( $ e x p r e s s i o n ) {
? >
< strong > This is true . </ strong >
<? php
} else {
? >
< strong > This is false .$ </ strong >
<? php
}
? >
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Formularios I
< form action = " accion . php " method = " POST " >
Su nombre : < input type = " text " name = " nombre " / >
Su edad : < input type = " text " name = " edad " / >
< input type = " submit " >
</ form >
Fichero accion.php
Hola <? php echo $_POST [ " nombre " ]; ? >.
Tiene <? php echo $_POST [ " edad " ]; ? > a & ntilde ; os
El cliente finalmente recibe
Hola José .
Tiene 22 años
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Formularios II
< form action = " accion . php " method = " GET " >
Su nombre : < input type = " text " name = " nombre " / >
Su edad : < input type = " text " name = " edad " / >
< input type = " submit " >
</ form >
Fichero accion.php
Hola <? php echo $_GET [ " nombre " ]; ? >.
Tiene <? php echo $_GET [ " edad " ]; ? > a & ntilde ; os
El cliente finalmente recibe
Hola José .
Tiene 22 años
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
GET vs POST
GET para datos que ocupan poco.
Los datos van en la URL, se puede acceder con un enlace:
<a href = " accion . php ? nombre = patata & amp ; edad =23 " >
POST para datos que ocupan mucho
Se pueden mandar ficheros
< form id = " f o r m u l a r i o " action = " p r o c e s a S u g e r e n c i a . php "
method = " post "
enctype = " m u l t i p a r t / form - data " >
. . . . . . . . . . . .
. . . . . . . . . . . .
< input id = " fichero " type = " file "
name = " fichero "
t a b i n d e x = " 7 " >
. . . . . . . . . . . . .
</ form >
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Guardando ficheros
<? php
f u n c t i o n g u a r d a F i c h e r o ( $directorio , $ f i c h e r o ) {
$fp = fopen ( $ d i r e c t o r i o . " / lock . txt " , " w + " );
while (! flock ( $fp , LOCK_EX )) { sleep (1); }
$ t i m e S t a m p = g e t m i c r o t i m e ();
$ f i c h e r o D e s t i n o = $ t i m e S t a m p . $_FILES [ $ f i c h e r o ][ " name " ];
m o v e _ u p l o a d e d _ f i l e ( $_FILES [ $ f i c h e r o ][ " t m p _ n a m e " ] ,
$ d i r e c t o r i o . " / " . $ f i c h e r o D e s t i n o );
flock ( $fp , LOCK_UN ); fclose ( $fp );
return $ f i c h e r o D e s t i n o ;
}
while ( list ( $clave , $valor ) = each ( $_FILES )) {
g u a r d a F i c h e r o ( d i r e c t o r i o D e s t i n o , $clave );
? >
<tr >
<td > <? php echo $valor [ " name " ]? > </ td >
<td > <? php echo $valor [ " size " ]? > </ td >
<td > <? php echo $valor [ " type " ]? > </ td >
</ tr >
<? php } ? >
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Datos permanentes: sesiones I
s e s s i o n _ s t a r t ();
define ( accesos , " accesos " );
define ( continuar , " c o n t i n u a r " );
define ( borrar , " borrar " );
$ a c c e s o s = $ _ S E S S I O N [ accesos ];
if ( isset ( $_GET [ borrar ]) ) {
unset ( $ _ S E S S I O N [ accesos ]);
}
if ( isset ( $ _ S E S S I O N [ accesos ]) ) {
$ _ S E S S I O N [ accesos ]++;
} else {
$ _ S E S S I O N [ accesos ]=1;
}
$ a c c e s o s = $ _ S E S S I O N [ accesos ];
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Datos permanentes: sesiones II
<? php
s e s s i o n _ s t a r t ();
. . . . . . . . . . . . . . . .
? >
<! DOCTYPE HTML PUBLIC " -// W3C // DTD HTML 4.01 T r a n s i t i o n a l // EN " >
< html >
< head >
< meta http - equiv = " Content - Type " content = " text / html ; ch arset = ut
< title > Ejemplo de sesiones </ title >
</ head >
< body >
<p > Has a c c e d i d o <?= $ a c c e s o s ? > veces </ p >
< form action = " session . php " >
<p >
< button name = " <?= c o n c o n t i n u a r ? > " > Continuar </ button >
< button name = " <?= borrar ? > " > Borrar sesion </ button >
</p >
</ form >
</ body >
</ html >
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Tipos I
PHP no es lenguaje tipado
una variable puede cambiar de tipo cuando se hace una
asignación.
no se declaran las variables
Los tipos se corresponden con los valores, no con las variables.
Cuatro tipos escalares:
boolean
integer
float (número de punto-flotante, también conocido como
’double’)
string
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Tipos II
Dos tipos compuestos:
array, indexados con cualquier tipo: tabla hash.
object
Y finalmente dos tipos especiales:
resource
NULL
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Variables I
<? php
$var = " Bob " ;
$Var = " Joe " ;
echo " $var , $Var " ;
$4site = ’ not yet ’;
$_4site = ’ not yet ’;
? >
// o u t p u t s " Bob , Joe "
// i n v a l i d ; s t a r t s with a n u m b e r
// v a l i d ; s t a r t s with an u n d e r s c o r e
PRECAUCI ÓN: se puede usar una variable sin dar un valor
inicial, PHP no se va a quejar.
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Variables II
<ul >
<li >: <?= $i ? >: </ li >
<? php $i = true ; ? >
<li >: <?= $i ? >: </ li >
<? php $i = 1; ? >
<li >: <?= $i ? >: </ li >
<? php $i = 2.3; ? >
<li >: <?= $i ? >: </ li >
<? php $i = ’ Hola ’; ? >
<li >: <?= $i ? >: </ li >
<li >: <?= $i ? >: </ li >
</ ul >
< ul >
< li > :1: </ li >
< li > :1: </ li >
< li > :2.3: </ li >
< li >: Hola : </ li >
</ ul >
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Constantes
<? php
define ( alumnosDB , " alumnos " );
define ( dirFotos , " / home / luis / d o c e n c i a / alumnos / fotos / " );
define ( urlFotos , " /~ luis / f o t o s A l u m n o s / " );
? >
$ n o m b r e F i c h e r o = d i r F o t o s . $dni . $ext ;
m o v e _ u p l o a d e d _ f i l e ( $foto [ ’ t m p _ n a m e ’] , $ n o m b r e F i c h e r o );
$ f i c h e r o F o t o = u r l F o t o s . $dni . $ext ;
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Arrays I
$correo = array ( " para " = > " a l o n s o @ s i p . ucm . es " ,
" de " = > " FROM : FORTE r e g i s t r a t i o n " ,
" subject " = > " R e g i s t r a t i o n FORTE 2004 " );
define ( early , " early " );
define ( late , " late " );
define ( forte2004 , " f o r t e 2 0 0 4 " );
define ( tutorial , " t u t o r i a l " );
define ( workshop , " w o r k s h o p " );
define ( f o r t e 2 0 0 4 _ w o r k s h o p , " f o r t e 2 0 0 4 _ w o r k s h o p " );
define ( regular , " regular " );
define ( student , " student " );
$tarifa [ f o r t e 2 0 0 4 ][ student ][ late ]=300;
$tarifa [ f o r t e 2 0 0 4 ][ student ][ early ]=250;
$tarifa [ f o r t e 2 0 0 4 ][ regular ][ late ]=500;
$tarifa [ f o r t e 2 0 0 4 ][ regular ][ early ]=450;
Luis Fernando Llana Díaz
Páginas WEB Accesibles
Departamento de Sistemas Informáticos y Programación
Arrays II
$tarifa [ t u t o r i a l ][ student ][ late ]=25;
$tarifa [ t u t o r i a l ][ student ][ early ]=25;
$tarifa [ t u t o r i a l ][ regular ][ late ]=25;
$tarifa [ t u t o r i a l ][ regular ][ early ]=25;
$tarifa [ w o r k s h o p ][ student ][ late ]=300;
$tarifa [ w o r k s h o p ][ student ][ early ]=250;
$tarifa [ w o r k s h o p ][ regular ][ late ]=300;
$tarifa [ w o r k s h o p ][ regular ][ early ]=250;
$tarifa [ f o r t e 2 0 0 4 _ w o r k s h o p ]
Comentarios de: Páginas WEB Accesibles - Breve introducción a PHP (0)
No hay comentarios