Académique Documents
Professionnel Documents
Culture Documents
Timestamp d’unix
Exemple de timestamp :
1216736314
Correspondant à 22/07/2008 16:18:34
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Gestion des dates
Fonction time()
La fonctiontime() retourne l’heure courante mesurée avec
le timestamp UNIX.
1. <?php
2. echo "Maintenant: " . time();
3. // Maintenant: 1216736314
4.
5. echo " en une semaine: " . time() + (3600 * 24 * 7);
6. // en une semaine: 1217341114
7. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Gestion des dates
Fonction mktime()
La fonction mktime() retourne l’heure avec le timestamp
UNIX arguments selon les arguments donnés.
1. <?php
2. echo "Timestamp retourne : " . mktime(16,18,34,07,22,2008);
3. // Timestamp retourne : 1216736314
4. ?>
année.
N.ZIDANE
GLR
ENSET RABAT
Gestion des dates
Fonction date()
La fonction date() retourne une chaine formatée selon le
format specifié.
1. <?php
2. echo date("d/m/y H:i:s",1216736314);
3. // 22/07/08 16:18:34
4. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Gestion des dates
Fonction date()
Les principaux caractères de la fonction date
i Minutes [00..59]
GLR
s Secondes [00.59]
ENSET RABAT
Gestion des dates
Fonction checkdate()
La fonction check_date() vérifie la validité d’une date
1. <?php
2. if(checkdate(12,31,2000)){
3. echo " La date est correcte";
4. }else{
5. echo " La date est incorrecte";
6. }
7. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Pause-réflexion
Fonctions Mathematique
Fonction round()
N.ZIDANE
GLR
ENSET RABAT
Mathematical Fonctions
Fonction sqrt()
La fonctionsqrt() retourne la racine carré d l’argument.
1. <?php
2. echo sqrt(25);
3. // 5
4. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Mathematical Fonctions
Fonction exp()
La fonction exp() retourne e puissance de l’argument.
1. <?php
2. echo exp(5);
3. // 148.413159103
4. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Mathematical Fonctions
Fonction pow()
La fonction pow() retourne le argument puissance du
second argument.
1. <?php
2. echo pow(2,8);
3. // 256
4. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Mathematical Fonctions
Fonction floor()
La fonction floor() retourne le nombre arrondi par défaut.
1. <?php
2. echo floor(6.9);
3. // 6
4. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Mathematical Fonctions
Fonction ceil()
La fonction ceil() retourne le nombre arrondi par excès.
1. <?php
2. echo ceil(6.9);
3. // 7
4. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Mathematical Fonctions
Fonction round()
La fonction round() retourne le premier argument arrondi
à la précision spécifiée par le second argument.
1. <?php
2. echo round(3.4);
3. // 3
4. echo round(3.5);
5. // 4
6. echo round(1.95582,2);
7. // 1.96
8. ?>
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Pause-réflexion
Fonctions d’Information
Fonction var_dump()
La fonction var_dump() affiche une information structurée
sur une ou plusieurs expressions incluant leurs types et
leurs valeurs.
1. <?php
2. $var = array(1, "ENSET");
3. var_dump($var);
4. ?>
array(2){
2014/2015
[0]=> int(1)
[1]=> string(5) "ENSET"
}
N.ZIDANE
GLR
ENSET RABAT
Information Fonctions
Fonction var_export()
La fonction var_export() Retourne une information
structurée sur une ou plusieurs expressions incluant leurs
types et leurs valeurs qui un code PHP valide.
1. <?php
2. $var = array(1,"ENSET");
3. var_export($var);
4. ?>
array(
2014/2015
0 => 1,
1 => ‘ENSET',
)
N.ZIDANE
GLR
ENSET RABAT
Information Fonctions
Fonction print_r()
La fonction print_r() affiche une information sur une
variable lisible par l’utilisateur.
1. <?php
2. $var = array(1,"ENSET");
3. print_r($var);
4. ?>
Array
(
[0] => 1
2014/2015
Submit
2014/2015
<?php
$chemin = ‘dossier_img/';
$chemin .= $_FILES[‘image']['name'];
N.ZIDANE
move_uploaded_file(
$_FILES[‘image']['tmp_name'],
GLR
$chemin
ENSET RABAT
);
?>
Form handling
$_FILES Variable
Variables $_FILES :
Cookies.
Sessions.
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Cookies & Sessions
Cookies
Le cookie est un texte stocké dans le pc du client.
Composé d’un nom et d’une valeure.
Utilisé pour :
L’authentication.
Stocker les preferences.
E-commerce.
2014/2015
…
Les cookies envoyées dans l’entete HTTP.
N.ZIDANE
Cookies
GET
GET /page1.php
/page1.php HTTP/1.1
HTTP/1.1
Host:
Host: www.webdev.org
www.webdev.org
HTTP/1.1
HTTP/1.1 200
200 OK
OK
Content-type:
Content-type: text/html
text/html
Set-Cookie:
Set-Cookie: name=value
name=value
...
...
2014/2015
GET
GET /page2.php
/page2.php HTTP/1.1
HTTP/1.1
N.ZIDANE
Host:
Host: www.webdev.org
www.webdev.org
Cookie:
Cookie: name=value
name=value
GLR
Accept:
Accept: */*
*/*
ENSET RABAT
Cookies & Sessions
Cookies
variable prédéfinie:
$_COOKIE : Tableau associatif de variables transféré
via HTTP.
Cookies
Exemples :
Ajout de cookies :
// Définir un cookie qui existe tant le navigateur n’est pas fermé.
setcookie('cookie1', $contenu1);
// Définir un cookie qui existera Durant 30 .
setcookie('cookie2', $contenu2, time() + 60 * 60 * 24 * 30);
echo $_COOKIE['cookie1'];
N.ZIDANE
Détruire un cookie :
GLR
Sessions
Sessions
= Session Key
/login.php /login.php
Session User 1
Session User 2
/help.php /about.php
2014/2015
User 1 User 2
N.ZIDANE
GLR
Web Server
ENSET RABAT
Cookies & Sessions
Sessions
variable prédéfinie:
$_SESSION Tableau associatif de variables utilisées
dans le script en cours.
Sessions
Exemples :
Création de session et ajout de variable de session :
session_start();
$_SESSION['username’] = $_POST['username'];
session_destroy();
Pause-réflexion
Exercises (1/6)
Nous allons utilisés les cookies et la session dans une
application web!
Exercises (2/6)
Exercises (4/6)
• Si l’authentification est bonne redirection vers la page web
d’accueil de l’application
• Sinon réafficher le formulaire d’authentification avec le message
authentification erronée.
2014/2015
N.ZIDANE
GLR
ENSET RABAT
Cookies & Sessions
Exercises (5/6)
Fonction require_once().
N.ZIDANE
GLR
ENSET RABAT
Inclusion Fonctions
Presentation
Vous pouvez inserer le contenu d’un fichier php dans un
autre grâce à 2 fonctions :
include().
require().
cas d’erreur.
N.ZIDANE
GLR
ENSET RABAT
Exemples :
include() :
<!-- menu.php -->
<a href="/default.php">Home</a>
<a href="/tutorials.php">Tutorials</a>
<a href="/references.php">References</a>
<a href="/examples.php">Exemples</a>
<a href="/about.php">About Us</a>
<a href="/contact.php">Contact Us</a>
2014/2015
<p>Some text.</p>
ENSET RABAT
</body>
</html>
Exemples :
require() :
<!-- menu.php -->
<a href="/default.php">Home</a>
<a href="/about.php">About Us</a>
<a href="/contact.php">Contact Us</a>
<div class="leftmenu">
<?php require("menu.php"); ?>
</div>
<h1>Welcome to my home page.</h1>
N.ZIDANE
<p>Some text.</p>
</body>
</html>
GLR
ENSET RABAT
Que se passe-t-il si on execute index.php ?
<!-- index.php --> <!-- script1.php -->
<?php <?php
?> ?>
2014/2015
function display($var) {
echo $var;
}
GLR
ENSET RABAT
?>
Cette erreur survient:
Pourquoi?
Car un code php qui lui-même inclu est inclu par un
2014/2015
Solution?
GLR
?> ?>
2014/2015
function display($var) {
echo $var;
}
GLR
ENSET RABAT
?>
Pause-réflexion
Cookies
Cookies &
&
N.ZIDANE
Include
Include files
files
Sessions
Sessions
GLR
ENSET RABAT
Langage PHP
Publications
PHP 5 avancé
5e édition.
Éric DASPET
Cyril PIERRE DE GEYER
2014/2015
Available on Cyberlibris.
http://library.supinfo.com/BookDetails.aspx?type=cyberlibris&docid=40001068
N.ZIDANE
Web sites
http://php.net/
GLR
ENSET RABAT
http://www.nexen.net/
http://www.zend.com/
Félicitations
Vous avez suivi avec succès le module
de cours sur les fonctions les cookies ,
les sessions et les inclusions