Vous n'êtes pas identifié(e).
L'icône rouge permet de télécharger chaque page du wiki visitée au format
PDF et la grise au format ODT →
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente | Prochaine révision Les deux révisions suivantes | ||
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 16:16] Hypathie [PHP connexion mysql] |
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 16:17] Hypathie [Objet PDO] |
||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
* Commentaires : developpement web | * Commentaires : developpement web | ||
* Débutant, à savoir : [[:doc:systeme:commandes:le_debianiste_qui_papillonne|Utiliser GNU/Linux en ligne de commande, tout commence là !.]] :-) | * Débutant, à savoir : [[:doc:systeme:commandes:le_debianiste_qui_papillonne|Utiliser GNU/Linux en ligne de commande, tout commence là !.]] :-) | ||
+ | |||
+ | ===== BDD mysql/mariadb ===== | ||
===== Objet PDO ===== | ===== Objet PDO ===== | ||
- | |||
- | === créer une instance de l'objet === | ||
- | |||
- | <code php> | ||
- | // file name : Connexion.class.php | ||
- | <?php | ||
- | class Database | ||
- | { | ||
- | private static $cont = null; | ||
- | |||
- | public function __construct() { | ||
- | die('Init function is not allowed'); | ||
- | } | ||
- | |||
- | public static function connect() | ||
- | { | ||
- | |||
- | if ( null == self::$cont ) | ||
- | { | ||
- | try | ||
- | { | ||
- | self::$cont = new PDO('mysql:host=localhost;dbname=comgocom;charset=utf8','root', 'arthuretmax2020'); | ||
- | |||
- | // self::$cont = new PDO('mysql:host=localhost;dbname=essaiLogin;charset=utf8','essaiLogin', 'essaiLogin'); | ||
- | } | ||
- | catch(PDOException $e) | ||
- | { | ||
- | die($e->getMessage()); | ||
- | } | ||
- | } | ||
- | return self::$cont; | ||
- | } | ||
- | |||
- | public static function disconnect() | ||
- | { | ||
- | self::$cont = null; | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | === utiliser l'objet === | ||
- | |||
- | <code php> | ||
- | // file name : RequetesIdentification.class.php | ||
- | |||
- | <?php | ||
- | error_reporting(E_ALL); | ||
- | ini_set('display_errors', '1'); | ||
- | |||
- | require 'Connexion.class.php'; | ||
- | |||
- | class RequetesIdentification | ||
- | { | ||
- | |||
- | public function __construct() | ||
- | { | ||
- | $this->pdo = Database :: connect(); | ||
- | } | ||
- | |||
- | |||
- | |||
- | public function isPseudoExists($info) | ||
- | { | ||
- | // On veut voir si tel pseudo ayant pour id $info existe. | ||
- | if (is_int($info)) | ||
- | { | ||
- | $q = $this->pdo->query("SELECT COUNT(*) FROM personne WHERE id =$info;")->fetchColumn(); | ||
- | } | ||
- | else | ||
- | { | ||
- | // Sinon, c'est qu'on veut vérifier que le "pseudo" existe ou pas. | ||
- | $clean_info= $this->pdo->quote($info); | ||
- | $q = $this->pdo->query("SELECT COUNT(*) FROM personne WHERE pseudo=$clean_info;")->fetchColumn(); | ||
- | } | ||
- | | ||
- | return (bool)$q; | ||
- | } | ||
- | } | ||
- | |||
- | </code> | ||
- | |||
- | === Traitements === | ||
- | |||
- | <code php> | ||
- | //file name : ManagerIdentification.class.php | ||
- | <?php | ||
- | error_reporting(E_ALL); | ||
- | ini_set('display_errors', '1'); | ||
- | require '../../dto/RequetesIdentification.class.php'; | ||
- | require('Validations.class.php'); | ||
- | |||
- | class ManagerIdentification | ||
- | { | ||
- | private $requeteIdentification; | ||
- | private $msg1 = "<p style='color:purple'>Vous n'êtes pas identifié...</p>"; | ||
- | private $msg2 = "<p style='color:purple'>Vous n'êtes plus identifié; veuillez vous identifier à nouveau...</p>"; | ||
- | private $msg3 = "<p style='color:red'>Pseudo et/ou mot de passe incorrect(s)...</p>"; | ||
- | private $msg4 = "<p style='color:blue'>Bienvenue </p>"; | ||
- | |||
- | |||
- | //// Conctructor //// | ||
- | |||
- | public function __construct() | ||
- | { | ||
- | $this->requeteIdentification = new RequetesIdentification(); | ||
- | } | ||
- | |||
- | |||
- | |||
- | |||
- | //////////////////////////////////// méthodes pour la page identification.php /////////////////////////////// | ||
- | |||
- | private function creerTableauInfosIdentification($pseudo, $motpasse) | ||
- | { | ||
- | $infos = array ( | ||
- | 'pseudo' => $pseudo, | ||
- | 'motPasse' => $motpasse, | ||
- | ); | ||
- | return $infos; | ||
- | } | ||
- | |||
- | private function infosAffichageSubmitIdentification() | ||
- | { | ||
- | if ( $_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST)) | ||
- | { | ||
- | $pseudo = htmlspecialchars_decode((trim($_POST['pseudoInput']))); | ||
- | $motPasse = ""; | ||
- | $infos = $this->creerTableauInfosIdentification($pseudo, $motPasse); | ||
- | //var_dump($infos); | ||
- | return $infos; | ||
- | } | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | === Front dossier public par exemple === | ||
- | |||
- | <code php> | ||
- | // fale name : identification.php | ||
- | |||
- | <?php | ||
- | error_reporting(E_ALL); | ||
- | ini_set('display_errors', '1'); | ||
- | require '../../outils/ManagerIdentification.class.php'; | ||
- | ?> | ||
- | |||
- | <!DOCTYPE html> | ||
- | <html> | ||
- | <head> | ||
- | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
- | <title>formulaire d'identification</title> | ||
- | <link rel="stylesheet" href="../../style/masterPage.css"> | ||
- | <link rel="stylesheet" href="../../style/formulaire.css"> | ||
- | <link rel="icon" type="image/x-icon" href="../../images/hypathieIco.ico" /> | ||
- | </head> | ||
- | <body> | ||
- | <div class="header"> | ||
- | <?php include("../includes/headerPages.php"); ?> | ||
- | </div> | ||
- | |||
- | <div class="body"> | ||
- | <div class="validation"> | ||
- | <?php | ||
- | $managerIdentification = new ManagerIdentification(); | ||
- | $infos = $managerIdentification->infosVideIdentification(); | ||
- | $infos = $managerIdentification->creerIdentification(); | ||
- | | ||
- | ?> | ||
- | </div> | ||
- | <div class="formulaire"> | ||
- | <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> | ||
- | <legend>S'identifier : </legend> | ||
- | <p><span class="error">* Champs obligatoires</span></p> | ||
- | <label>Pseudonyme</label> | ||
- | </br> | ||
- | <input type="text" name="pseudoInput" value="<?php echo $infos['pseudo'] ?>"> | ||
- | <span class="error">*</span><br /> | ||
- | <label>Mot de passe</label> | ||
- | </br> | ||
- | <input type="password" name="passInput" value="<?php echo $infos['motpasse'] ?>" | ||
- | placeholder="**************" | ||
- | > | ||
- | <span class="error">*</span><br /> | ||
- | <input type="submit" name="connexion" value="Connexion" | ||
- | <?php '<script type="text/javascript">location.reload(); </script>' ?> | ||
- | /> | ||
- | </form> | ||
- | </div> | ||
- | </div> | ||
- | <div class="footer"> | ||
- | <p>© 2020 comgocom.pw<p> | ||
- | </div> | ||
- | </body> | ||
- | |||
- | </html> | ||
- | <?php | ||
- | $managerIdentification->msgInfoCookie(); | ||
- | ?> | ||
- | |||
- | </code> | ||