logo Debian Debian Debian-France Debian-Facile Debian-fr.org Forum-Debian.fr Debian ? Communautés logo inclusivité

Debian-facile

Bienvenue sur Debian-Facile, site d'aide pour les nouveaux utilisateurs de Debian.

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 → ODT PDF Export

Ceci est une ancienne révision du document !


PHP connexion mysql

Objet PDO

créer une instance de l'objet

// 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;
    }
}

utiliser l'objet

// 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;
    }
} 
 

Traitements

//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;
        }
    }
}

Front dossier public par exemple

// 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>&copy; 2020 comgocom.pw<p>
        </div>
    </body>
 
</html>
<?php
    $managerIdentification->msgInfoCookie();
?>
utilisateurs/hypathie/tutos/php_connexion_mysql.1625753770.txt.gz · Dernière modification: 08/07/2021 16:16 par Hypathie

Pied de page des forums

Propulsé par FluxBB