Bonjour,
J'ai rencontrer une difficulté quand j'ai voulu passer mon login system du procédural en MVC et notamment sur deux variables en particulier, $userexist et $mailexist qui sont dans mon model AuthManager.php, elles servent a aller vérifié dans la bdd si une ligne existe déja. $userexist m'est retourné comme undefined lors la connexion et $mailexist n'est pas prise en compte lors de l'inscription, par exemple deux mêmes email peuvent être mises dans ma BDD. Si quelqu'un avait une solution car je suis un peu perdu ! Merci beaucoup
controller UserController.php :
model AuthManager.php :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 function checkLogin($mailconnect) { session_start(); $authManager = new P4OC\site\Model\AuthManager(); $mailconnect = htmlspecialchars($_POST['mailconnect']); $mdpconnect = $_POST['mdpconnect']; if(!empty($mailconnect) AND !empty($mdpconnect)) { $auth = $authManager->checkLoginManager($mailconnect); if($userexist == 1) { if (password_verify($mdpconnect, $userinfo['motdepasse'])) { $_SESSION['id'] = $userinfo['id']; $_SESSION['pseudo'] = $userinfo['pseudo']; $_SESSION['mail'] = $userinfo['mail']; header("Location: index.php?action=showDash&id=".$_SESSION['id']); } else { throw new Exception("Le mot de passe est non valide"); } } else { throw new Exception("Mauvais mail ou mot de passe !"); } } else { throw new Exception("Tous les champs doivent être complétés !"); } } function inscription($pseudo, $mail, $mdp_hash) { $authManager = new P4OC\site\Model\AuthManager(); $pseudo = htmlspecialchars($_POST['pseudo']); $mail = htmlspecialchars($_POST['mail']); $mail2 = htmlspecialchars($_POST['mail2']); $mdp = $_POST['mdp']; $mdp2 = $_POST['mdp2']; print_r($mdp); echo "<br>"; print_r($mdp2); if(!empty($_POST['pseudo']) AND !empty($_POST['mail']) AND !empty($_POST['mail2']) AND !empty($_POST['mdp']) AND !empty($_POST['mdp2'])) { $pseudolength = strlen($pseudo); if($pseudolength <= 255) { if($mail == $mail2) { if(filter_var($mail, FILTER_VALIDATE_EMAIL)) { $auth = $authManager->checkEmailManager($mail); if($mailexist == 0) { if($mdp == $mdp2) { $mdp_hash = password_hash($mdp, PASSWORD_BCRYPT); $auth = $authManager->insertMemberManager($pseudo, $mail, $mdp_hash); header('Location:index.php?action=showLogin'); } else { throw new Exception("Vos mots de passes ne correspondent pas !"); } } else { throw new Exception("Adresse mail déjà utilisée !"); } } else { throw new Exception("Votre adresse mail n'est pas valide !"); } } else { throw new Exception("Vos adresses mail ne correspondent pas !"); } } else { throw new Exception("Votre pseudo ne doit pas dépasser 255 caractères !"); } } else { throw new Exception("Tous les champs doivent être complétés !"); } }
view inscription.php :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 <?php namespace P4OC\site\Model; require_once(MODEL."Manager.php"); class AuthManager extends Manager { public function checkEmailManager($mail) { $db = $this->dbConnect(); $reqmail = $db->prepare("SELECT * FROM users WHERE mail = ?"); $reqmail->execute(array($mail)); $mailexist = $reqmail->rowCount(); return $reqmail; } public function insertMemberManager($pseudo, $mail, $mdp_hash) { $db = $this->dbConnect(); $insertmbr = $db->prepare("INSERT INTO users(pseudo, mail, motdepasse) VALUES(?, ?, ?)"); $insertmbr->execute(array($pseudo, $mail, $mdp_hash)); return $insertmbr; } public function checkLoginManager($mailconnect) { $db = $this->dbConnect(); $requser = $db->prepare("SELECT * FROM users WHERE mail = ?"); $requser->execute(array($mailconnect)); $userexist = $requser->rowCount(); $userinfo = $requser->fetch(); return $userinfo; } }
view login.php :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 <?php $title = "Espace Client - Inscription"; ?> <?php ob_start(); ?> <div align="center"> <h2>Inscription</h2> <a href="index.php">Accueil</a> <a href="index.php?action=showLogin">Login</a> <br /><br /> <form method="POST" action="index.php?action=inscription"> <table> <tr> <td align="right"> <label for="pseudo">Pseudo :</label> </td> <td> <input type="text" placeholder="Votre pseudo" id="pseudo" name="pseudo" value="<?php if(isset($pseudo)) { echo $pseudo; } ?>" /> </td> </tr> <tr> <td align="right"> <label for="mail">Mail :</label> </td> <td> <input type="email" placeholder="Votre mail" id="mail" name="mail" value="<?php if(isset($mail)) { echo $mail; } ?>" /> </td> </tr> <tr> <td align="right"> <label for="mail2">Confirmation du mail :</label> </td> <td> <input type="email" placeholder="Confirmez votre mail" id="mail2" name="mail2" value="<?php if(isset($mail2)) { echo $mail2; } ?>" /> </td> </tr> <tr> <td align="right"> <label for="mdp">Mot de passe :</label> </td> <td> <input type="password" placeholder="Votre mot de passe" id="mdp" name="mdp" /> </td> </tr> <tr> <td align="right"> <label for="mdp2">Confirmation du mot de passe :</label> </td> <td> <input type="password" placeholder="Confirmez votre mdp" id="mdp2" name="mdp2" /> </td> </tr> <tr> <td></td> <td align="center"> <br /> <input type="submit" name="forminscription" value="Je m'inscris" /> </td> </tr> </table> </form> <?php if(isset($erreur)) { echo '<font color="red">'.$erreur."</font>"; } ?> </div> <?php $content = ob_get_clean(); ?> <?php require(VIEWBACK.'template.php');
et mon routeur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 <?php $title = 'Mon blog'; ?> <?php ob_start(); ?> <?php session_start(); ?> <div align="center"> <h2>Connexion</h2> <a href="index.php">Accueil</a> <a href="index.php?action=showInscription">Inscription</a> <br /><br /> <form method="POST" action="index.php?action=login"> <input type="email" name="mailconnect" placeholder="Mail" /> <input type="password" name="mdpconnect" placeholder="Mot de passe" /> <br /><br /> <input type="submit" name="formconnexion" value="Se connecter !" /> </form> <?php if(isset($erreur)) { echo '<font color="red">'.$erreur."</font>"; } ?> </div> <?php $content = ob_get_clean(); ?> <?php require(VIEWBACK.'template.php'); ?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 <?php include_once('_config.php'); require_once(CONTROLLERFRONT.'Home.php'); require_once(CONTROLLERBACK.'UserController.php'); try { if (isset($_GET['action'])) { if ($_GET['action'] == 'showHome') { showHome(); } elseif ($_GET['action'] == 'showPost') { if (isset($_GET['id']) && $_GET['id'] > 0) { showPost(); } else { throw new Exception('Aucun identifiant de billet envoyé'); } } elseif ($_GET['action'] == 'addComment') { if (isset($_GET['id']) && $_GET['id'] > 0) { if (!empty($_POST['author']) && !empty($_POST['comment'])) { addComment($_GET['id'], $_POST['author'], $_POST['comment']); } else { throw new Exception('Tous les champs ne sont pas remplis !'); } } else { throw new Exception('Aucun identifiant de billet envoyé'); } } elseif ($_GET['action'] == 'showLogin') { showLogin(); } elseif ($_GET['action'] == 'showDash') { showDash(); } elseif ($_GET['action'] == 'showInscription') { showInscription(); } elseif ($_GET['action'] == 'showDash') { showDash(); } elseif ($_GET['action'] == 'logout') { logout(); } elseif ($_GET['action'] == 'inscription') { if(isset($_POST['forminscription'])) { inscription($_POST['pseudo'], $_POST['mail'], $_POST['mdp']); } } elseif ($_GET['action'] == 'login') { if(isset($_POST['formconnexion'])) { checklogin($_POST['mailconnect']); } } } else { showHome(); } } catch(Exception $e) { echo 'Erreur : ' . $e->getMessage(); }
Partager