[ 139002 ]
[ 38.103.63.16 ]
Réalisation du site en PHP (partie 1)Ici nous allons parler de la réalisation du site en PHP. Nous allons donc reprendre, notre site créé en HTML, dans le cours précédent Réalisation du site en HTML (part.1) et lui ajouter une structure PHP. Voici notre fichier index.html de départ :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>Mon premier site en HTML</title>
<meta http-equiv="Content-Style-Type" content="text/css">
</head>
<body style="font-family: Arial, sans-serif;font-size: 12px; color: #000000;">
<table width="600" align="center" valign="top" cellspacing="2" cellpadding="2" border="0">
<tr valign="middle">
<td width="600" height="90" colspan="2" bgcolor="#0068B4">
<div style="font-size: 24px; color: #FFFFFF;">Mon premier site en HTML</div>
</td>
</tr>
<tr valign="top">
<td width="120" height="350" rowspan="2" bgcolor="#606DB2">
<div style="font-size: 13px;"><b>MENU :</b><br>
<ul>
<li><a href="index.html"> Accueil</a></li>
<li><a href="rubrique1.html"> Rubrique 1</a></li>
<li><a href="rubrique2.html"> Rubrique 2</a></li>
<li><a href="rubrique3.html"> Rubrique 3</a></li>
<li><a href="rubrique4.html"> Rubrique 4</a></li>
</ul>
</div>
</td>
<td width="480" height="310" bgcolor="#AFD394">
<div>contenu du site</div>
</td>
</tr>
<tr valign="middle">
<td width="480" height="40" bgcolor="#CDBCA3">
<div>2005 Copyright by Moi</div>
</td>
</tr>
</table>
</body>
</html>
Et nos fichiers rubrique1.html, rubrique2.html, etc... de départ :
Etape 1 - La structureDans un premier temps il va falloir renommer notre fichier index.html en, index.php. Attention ! Toutes lignes de codes en noir, sont toutes les modifications que nous apportons au fur et à mesure de l'explication! Ouvrez ce fichier index.php, et modifiez ce qui suit : <?php error_reporting(E_ALL ^ E_NOTICE); $page = $_GET['page']; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> <html> <head> <title>Mon premier site en PHP</title> <meta http-equiv="Content-Style-Type" content="text/css"> </head> <body style="font-family: Arial, sans-serif;font-size: 12px; color: #000000;"> <table width="600" align="center" valign="top" cellspacing="2" cellpadding="2" border="0"> <tr valign="middle"> <td width="600" height="90" colspan="2" bgcolor="#0068B4"> <div style="font-size: 24px; color: #FFFFFF;">Mon premier site en PHP</div> </td> </tr> <tr valign="top"> <td width="120" height="350" rowspan="2" bgcolor="#606DB2"> <div style="font-size: 13px;"><b>MENU :</b><br> <ul> <li><a href="index.html"> Accueil</a></li> <li><a href="rubrique1.html"> Rubrique 1</a></li> <li><a href="rubrique2.html"> Rubrique 2</a></li> <li><a href="rubrique3.html"> Rubrique 3</a></li> <li><a href="rubrique4.html"> Rubrique 4</a></li> </ul> </div> </td> <td width="480" height="310" bgcolor="#AFD394"> <div> <?php if (file_exists("pages/$page.html") == TRUE) {include("pages/$page.html");} else {include("pages/accueil.html");} ?> </div> </td> </tr> <tr valign="middle"> <td width="480" height="40" bgcolor="#CDBCA3"> <div>2005 Copyright by Moi</div> </td> </tr> </table> </body> </html> Ce script ajoutée à notre fichier, sert à vérifier que la page demandée par les liens, est bien existante dans le chemin spécifié.
if (file_exists("pages/$page.html") == TRUE)
{include("pages/$page.html");}
else
{include("pages/accueil.html");}
Grâce à cette technique, nous évitons, d'afficher une "Erreur 404" lorsque l'URL n'est pas valide. Passons à l'étape suivante Suite du Cours |