50
Module I-C3 : Applications Web IUT R&T 2 e année mercredi 22 juin 2022 IC3 : cours 2 1 David Mercier

Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

Embed Size (px)

Citation preview

Page 1: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

Module I-C3 : Applications WebIUT R&T 2e année

mardi 11 avril 2023 IC3 : cours 2 1

David Mercier

Page 2: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 2

PHP : Hypertext PreprocessorIntroduction

Page 3: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 3

PHP : Hypertext PreprocessorArchitecture 3 tiers

Niveau 1client

Niveau 1client

Niveau 2Serveur web

Niveau 2Serveur web

Niveau 3DonnéesNiveau 3Données

HTTPHTML

TCP (SQL)

HTTPHTML/PHP

Exemple d’architecture 3 tiers

Page 4: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 4

PHP : Hypertext PreprocessorExemple 1 : une première page PHP (1/2)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xml?ns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> <head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15"/> <title> première page PHP </title>

</head><body>

<?php echo "<p> Bonjour tout le monde ! Ceci est déjà un script PHP </p>"; ?></body></html>

mapage.phpEn pratique ici : à mettre dans votre /home/~user/public_html/

Page 5: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 5

PHP : Hypertext PreprocessorExemple 1 : une première page PHP (2/2)

Affichage http://iut-gtr2/~user/mapage.php

Code source généré (plus de PHP)

Page 6: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 6

PHP : Hypertext PreprocessorExemple 2 : détection du navigateur utilisé par un visiteur

• Information stockée dans une variable réservée (superglobale) de PHP : $_SERVER['HTTP_USER_AGENT'].

• $_SERVER contient toutes les informations relatives au serveur web

• Variables PHP commencent toutes par un signe dollar.

Affichage possible

Page 7: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 7

PHP : Hypertext PreprocessorExemples de variables prédéfinies

Variable Description$_SERVER['DOCUMENT_ROOT'] Racine du serveur

$_SERVER['HTTP_ACCEPT_LANGUAGE'] Langage accepté par le navigateur

$_SERVER['HTTP_HOST'] Nom de domaine du serveur

$_SERVER['HTTP_USER_AGENT'] Type de navigateur

$_SERVER['PATH_INFO'] Chemin WEB du script

$_SERVER['PATH_TRANSLATED'] Chemin complet du script

$_SERVER['REQUEST_URI'] Chemin du script

$_SERVER['REMOTE_ADDR'] Adresse IP du client

$_SERVER['REMOTE_PORT'] Port de la requête HTTP

$_SERVER['QUERY_STRING'] Liste des paramètres passés au script

$_SERVER['SERVER_ADDR'] Adresse IP du serveur

$_SERVER['SERVER_ADMIN'] Adresse de l'administrateur du serveur

$_SERVER['SERVER_NAME'] Nom local du serveur

$_SERVER['SERVER_SIGNATURE'] Type de serveur

$_SERVER['REQUEST_METHOD'] Méthode d'appel du script

Page 8: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 8

PHP : Hypertext PreprocessorLes types

Affichage :

Page 9: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 9

PHP : Hypertext PreprocessorConcaténer deux chaînes

Page 10: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 10

PHP : Hypertext PreprocessorTranstypage (cast)

Page 11: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 11

PHP : Hypertext PreprocessorFonctions utiles liées aux types

Excellente documentation !

Page 12: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 12

PHP : Hypertext PreprocessorTableaux classiques

HTML :

Page 13: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 13

PHP : Hypertext PreprocessorTableaux associatifs

Page 14: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 14

PHP : Hypertext PreprocessorTableaux : fonctions de tri

Page 15: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 15

PHP : Hypertext PreprocessorStructure de contrôle : if

Page 16: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 16

PHP : Hypertext PreprocessorStructure de contrôle : switch

Remarque :Test d’égalité sur chaîne de caractères…

Page 17: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 17

PHP : Hypertext PreprocessorBoucle for

Page 18: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 18

PHP : Hypertext PreprocessorBoucle while

Page 19: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 19

PHP : Hypertext Preprocessorfonctions

Page 20: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 20

PHP : Hypertext PreprocessorLa fonction date()

Pour avoir les dates en français ajouter :$error = @setlocale('LC_TIME', 'fr_FR.utf8','fra');

Voir : http://fr3.php.net/manual/fr/function.date.php

Page 21: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 21

PHP : Hypertext PreprocessorLa fonction time()

Au moment de la création de ce transparent

Page 22: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 22

PHP : Hypertext PreprocessorLecture d’un fichier ligne par ligne

Documentation :

Page 23: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 23

PHP : Hypertext PreprocessorFormulaire 1

mapage8_form1.php

Page 24: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 24

PHP : Hypertext PreprocessorFormulaire 1

mapage8_form1_repetBonjour.php

mapage8_form1.php

Page 25: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 25

PHP : Hypertext PreprocessorFormulaire 1

Page 26: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 26

PHP : Hypertext PreprocessorFormulaire 2

Page 27: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 27

PHP : Hypertext PreprocessorFormulaire 2

Page 28: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 28

PHP : Hypertext PreprocessorFormulaire 2

Page 29: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 29

PHP : Hypertext PreprocessorFormulaire 2

action

À essayer !

Voir aussi la fonction print_r

Page 30: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 30

PHP : Hypertext PreprocessorFormulaire 2

action

À essayer !

Voir aussi la fonction print_r

Page 31: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 31

PHP : Hypertext PreprocessorLes Sessions : motivations

Page 32: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 32

PHP : Hypertext PreprocessorLes Sessions : principe

Page 33: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

1. 1ière Demande

page

2. Renvoie page + id session

mardi 11 avril 2023 IC3 : cours 2 33

PHP : Hypertext PreprocessorLes Sessions : Grandes lignes

clientclient

Serveur webServeur web

3. Demande page + id session

4. Renvoie page + id session

Fichier de sessions

Génération d’un identifiant de session

Stockage des informations de session

Récupérationdes données

Calcul et envoi de la page

Page 34: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 34

PHP : Hypertext PreprocessorLes Sessions : cas des cookies acceptés

Page 35: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 35

PHP : Hypertext PreprocessorLes Sessions : cas des cookies non acceptés

session_name() session_id()

Page 36: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 36

PHP : Hypertext PreprocessorExemple de début de page comportant une session

<? php/* initialisation de la session : ce code est avant tout code HTML, donc avant le DTD en particulier*/session_start();

// écrire ‘TOTO’ dans la variable ‘mavariable’$_SESSION[‘mavariable’] = ‘TOTO’;

$tab = array(‘un’, ’dos’,’ tres’);$_SESSION[‘tableau’] = $tab;

?>

Page 37: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 37

PHP : Hypertext PreprocessorFermer une session

Page 38: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 38

PHP : Hypertext PreprocessorUtilisation du SGDB PostgreSQL

Page 39: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 1 39

PHP : Hypertext PreprocessorRappel : architecture 3 tiers

Niveau 1client

Niveau 1client

Niveau 2Serveur web

Niveau 2Serveur web

Niveau 3DonnéesNiveau 3Données

HTTPHTML

TCP (SQL)HTTPHTML/PHP

Couche présentation Couche métier Couche accès aux données

Page 40: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 40

PHP : Hypertext PreprocessorConnexion au serveur

<?php// connexion à une base de données nommée "marie" $conn = pg_connect("dbname=marie");

// connexion à une base de données nommée "marie" sur l'hôte "localhost" sur le port "5432" $conn2 = pg_connect("host=localhost port=5432 dbname=marie");

// connexion à une base de données nommée "marie" sur l'hôte "mouton" avec un// nom d'utilisateur et un mot de passe $conn3 = pg_connect("host=mouton port=5432 dbname=marie user=agneau password=foo");

// connexion à une base de données nommée "test" sur l'hôte "mouton" avec un// nom d'utilisateur et un mot de passe$conn_string = "host=mouton port=5432 dbname=test user=agneau password=bar";$conn4 = pg_connect($conn_string);?>

Page 41: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 41

PHP : Hypertext PreprocessorExécuter une requête SQL

<?php $conn = pg_connect("…");

$result = pg_query ($conn, "SELECT nom FROM joueur");

/* renvoie une ressource de résultat si la requête s’est exécuté correctement,FALSE sinon*/?>

Page 42: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 42

PHP : Hypertext PreprocessorLire le résultat d’une requête

/* renvoie Un tableau contenant la ligne demandée fonction d’une ressource de résultat ,FALSE s’il ne reste plus de lignes*/

Par exemple, affichage du premier élément de chaque ligne :

while ($row = pg_fetch_row($result)) { echo "$row[0] <br />\n";

} Documentation :

Page 43: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 43

PHP : Hypertext PreprocessorUn exemple sur notre serveur PostgreSQL iut-gtr2

<?php $conn = pg_connect("host=iut-gtr2 port=5432 dbname=ligue1 user=duchmol password=bdrt00");if (!$conn) {

echo "Connexion à la base impossible\n ";exit;

}$result = pg_query($conn, "SELECT nom FROM joueur");if (!$result) {

echo "La requête n’a pu être exécutée.\n" ;exit;

}while ($row = pg_fetch_row($result)) {

echo "Joueur: $row[0] "; echo "<br />\n";

}pg_close ($conn) ;?>

Page 44: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 44

PHP : Hypertext PreprocessorConnexion au serveur PostgreSQL (iut-gtr2)

<?php $conn = pg_connect("host=iut-gtr2 port=5432 dbname=ligue1 user=duchmol password=bdrt00");if (!$conn) {

echo "Connexion à la base impossible\n";exit;

}$result = pg_query($conn, "SELECT nom FROM joueur");if (!$result) {

echo "La requête n’a pu être exécutée.\n" ;exit;

}while ($row = pg_fetch_row($result)) {

echo "Joueur: $row[0] "; echo "<br />\n";

}pg_close ($conn) ;?>

Page 45: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 45

PHP : Hypertext PreprocessorGestion de l’échec de la connexion

<?php $conn = pg_connect("host=iut-gtr2 port=5432 dbname=ligue1 user=duchmol password=bdrt00");if (!$conn) {

echo "Connexion à la base impossible\n ";exit;

}$result = pg_query($conn, "SELECT nom FROM joueur");if (!$result) {

echo "La requête n’a pu être exécutée.\n" ;exit;

}while ($row = pg_fetch_row($result)) {

echo "Joueur: $row[0] "; echo "<br />\n";

}pg_close ($conn) ;?>

Page 46: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 46

PHP : Hypertext PreprocessorExécution d’une requête SQL

<?php $conn = pg_connect("host=iut-gtr2 port=5432 dbname=ligue1 user=duchmol password=bdrt00");if (!$conn) {

echo "Connexion à la base impossible.\n";exit;

}$result = pg_query($conn, "SELECT nom FROM joueur");if (!$result) {

echo "La requête n’a pu être exécutée.\n" ;exit;

}while ($row = pg_fetch_row($result)) {

echo "Joueur: $row[0] "; echo "<br />\n";

}pg_close ($conn) ;?>

Page 47: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 47

PHP : Hypertext PreprocessorGestion de l’échec d’exécution de la requête

<?php $conn = pg_connect("host=iut-gtr2 port=5432 dbname=ligue1 user=duchmol password=bdrt00");if (!$conn) {

echo "Connexion à la base impossible.\n";exit;

}$result = pg_query($conn, "SELECT nom FROM joueur");if (!$result) {

echo "La requête n’a pu être exécutée.\n" ;exit;

}while ($row = pg_fetch_row($result)) {

echo "Joueur: $row[0] "; echo "<br />\n";

}pg_close ($conn) ;?>

Page 48: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 48

PHP : Hypertext PreprocessorAffichage de chaque ligne

du résultat de la requête

<?php $conn = pg_connect("host=iut-gtr2 port=5432 dbname=ligue1 user=duchmol password=bdrt00");if (!$conn) {

echo "Connexion à la base impossible.\n";exit;

}$result = pg_query($conn, "SELECT nom FROM joueur");if (!$result) {

echo "La requête n’a pu être exécutée.\n" ;exit;

}while ($row = pg_fetch_row($result)) {

echo "Joueur: $row[0] "; echo "<br />\n";

}pg_close ($conn) ;?>

Page 49: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 49

PHP : Hypertext PreprocessorFermeture de la base

<?php $conn = pg_connect("host=iut-gtr2 port=5432 dbname=ligue1 user=duchmol password=bdrt00");if (!$conn) {

echo "Connexion à la base impossible.\n";exit;

}$result = pg_query($conn, "SELECT nom FROM joueur");if (!$result) {

echo "La requête n’a pu être exécutée.\n" ;exit;

}while ($row = pg_fetch_row($result)) {

echo "Joueur: $row[0] "; echo "<br />\n";

}pg_close ($conn) ;?>

Page 50: Cours 2 : Introduction au langage PHP Module I-C3 : Applications Web IUT R&T 2 e année samedi 25 octobre 2014IC3 : cours 21 David Mercier

mardi 11 avril 2023 IC3 : cours 2 50

PHP : Hypertext PreprocessorPour aller plus loin : références

Une mine d’informations :

http://fr3.php.net/manual/fr/