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 | Révision précédente Prochaine révision Les deux révisions suivantes | ||
utilisateurs:hypathie:tutos:php_connexion_mysql [09/06/2021 18:14] Hypathie [Objet PDO] |
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 16:38] Hypathie [BDD mysql/mariadb] |
||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
* Objet : code php de connexion | * Objet : code php de connexion | ||
* Niveau requis : {{tag>débutant}} | * Niveau requis : {{tag>débutant}} | ||
- | * Commentaires : //Contexte d'utilisation du sujet du tuto. // FIXME | + | * 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à !.]] :-) | ||
- | ===== Objet PDO ===== | + | ===== BDD mysql/mariadb ===== |
- | === créer une instance de l'objet === | + | ==== Créer une base ==== |
- | <code php> | + | === Script sql=== |
- | // file name : Connexion.class.php | + | <code sql> |
- | <?php | + | //nath_test.sql |
- | class Database | + | CREATE DATABASE IF NOT EXISTS `nath_test`; |
- | { | + | |
- | 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 === | + | USE `nath_test`; |
+ | |||
+ | CREATE TABLE IF NOT EXISTS `users` ( | ||
+ | `id` int(11) NOT NULL, | ||
+ | `name` varchar(250) NOT NULL, | ||
+ | `surname` varchar(250) NOT NULL | ||
+ | ) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=UTF8; | ||
- | <code php> | + | INSERT INTO `users` (`id`, `name`, `surname`) VALUES |
- | // file name : RequetesIdentification.class.php | + | (1, 'Jean-Marc', 'Joseph'), |
+ | (2, 'Nathalie', 'D urso'), | ||
+ | (3, 'Lionel', 'Joseph'), | ||
+ | (4, 'Samuel', 'D urso'), | ||
+ | (5, 'Helene-Fleur', 'D urso'); | ||
- | <?php | + | ALTER TABLE `users` |
- | error_reporting(E_ALL); | + | ADD PRIMARY KEY (`id`); |
- | 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; | + | ALTER TABLE `users` |
- | } | + | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=28; |
- | } | + | |
- | + | ||
- | </code> | + | |
- | === Traitements === | + | CREATE TABLE IF NOT EXISTS `tree_country_state_city` ( |
+ | `id` int(11) NOT NULL, | ||
+ | `name` varchar(250) NOT NULL, | ||
+ | `parent_id` int(11) NOT NULL | ||
+ | ) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=UTF8; | ||
- | <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 | + | INSERT INTO `tree_country_state_city` (`id`, `name`, `parent_id`) VALUES |
- | { | + | (1, 'USA', 0), |
- | private $requeteIdentification; | + | (2, 'Canada', 0), |
- | private $msg1 = "<p style='color:purple'>Vous n'êtes pas identifié...</p>"; | + | (3, 'Australia', 0), |
- | private $msg2 = "<p style='color:purple'>Vous n'êtes plus identifié; veuillez vous identifier à nouveau...</p>"; | + | (4, 'New York', 1), |
- | private $msg3 = "<p style='color:red'>Pseudo et/ou mot de passe incorrect(s)...</p>"; | + | (5, 'Alabama', 1), |
- | private $msg4 = "<p style='color:blue'>Bienvenue </p>"; | + | (6, 'California', 1), |
+ | (7, 'Ontario', 2), | ||
+ | (8, 'British Columbia', 2), | ||
+ | (9, 'New South Wales', 3), | ||
+ | (10, 'Queensland', 3), | ||
+ | (11, 'New York city', 4), | ||
+ | (12, 'Buffalo', 4), | ||
+ | (13, 'Albany', 4), | ||
+ | (14, 'Birmingham', 5), | ||
+ | (15, 'Montgomery', 5), | ||
+ | (16, 'Huntsville', 5), | ||
+ | (17, 'Los Angeles', 6), | ||
+ | (18, 'San Francisco', 6), | ||
+ | (19, 'San Diego', 6), | ||
+ | (20, 'Toronto', 7), | ||
+ | (21, 'Ottawa', 7), | ||
+ | (22, 'Vancouver', 8), | ||
+ | (23, 'Victoria', 8), | ||
+ | (24, 'Sydney', 9), | ||
+ | (25, 'Newcastle', 9), | ||
+ | (26, 'City of Brisbane', 10), | ||
+ | (27, 'Gold Coast', 10); | ||
- | //// Conctructor //// | + | ALTER TABLE `tree_country_state_city` |
+ | ADD PRIMARY KEY (`id`); | ||
- | public function __construct() | ||
- | { | ||
- | $this->requeteIdentification = new RequetesIdentification(); | ||
- | } | ||
+ | ALTER TABLE `tree_country_state_city` | ||
+ | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=28; | ||
- | |||
- | |||
- | //////////////////////////////////// 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> | </code> | ||
- | === Front dossier public par exemple === | + | === Executer le script=== |
- | + | ||
- | <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 user> | ||
+ | mysql -u root -p < ~/user/Test/nath_test.sql | ||
</code> | </code> | ||
+ | ===== Objet PDO ===== | ||