Salut,
J'ai un problème qui me fait tout loupé, à 1 jours de la fin de mon stage (comme par hasard). C'est donc très urgent !
Jusqu'à ce midi, tout allait bien, mon fichier javascript avec les appels aja onctionnait parfaitement bien.
Le problème survient alors que je voulait faire des tests, et après avoir modifié 2-3 trucs. Prenons 2 projet pour exemple :
- Le projet d'authentification sécurisé (sur PHP 5.4 car CentOS7, donc pas d'API de cryptage)
Tout fonctionnait ce matin. les réponse s'envoyait correctement. Seulement, j'ai maintenant un ' qui s'envoit evant le JSON que j'envois dans le success callback, sans raison puisque j'ai juste un json_encode et tout est bien formaté.
Code PHP : 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 <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Nagios</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Language" content="en" /> <meta name="robots" content="noindex, nofollow" /> <script src="http://code.jquery.com/jquery-1.10.2.js"></script> <link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script src="/authentif/js/function.js"></script> </head> <body style="background-color: #eee;"> <?php if(!isset($_SESSION['User']) && isset($_COOKIE['id'])){ //Si une session n'est pas démarré mais qu'il existe un cookie echo "<script>authByCookie('".$_COOKIE['id']."');</script>"; }else{ if(isset($_SESSION['User'])){ //Si une session a été démarrée ?> <p id="userInfo">Vous êtes connecté en tant que <?=$_SESSION['User']?></p> <button onclick="disconnect()" class="btn btn-primary">Se déconnecter</button> <?php }else{ ?> <div class="container"> <div id="login-form" class="form-signin" style="width:300px; margin : auto;"> <h2 class="form-signin-heading">NagiosPlus Login</h2> <label for="pseudo" class="sr-only">Pseudo</label> <input type="text" id="pseudo" class="form-control" placeholder="Pseudo" required="" autofocus=""> <label for="pass" class="sr-only">Password</label> <input type="password" id="pass" class="form-control" placeholder="Password" required=""> <div class="checkbox"> <label> <input id="remember" type="checkbox" value="remember-me"> Remember me </label> </div> <button id="submit" onclick="tryConnect()" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> <div id="resultat"></div> </div> </div> <?php } ?> </body> <script> $("input").keydown(function(event){ if(event.keyCode == 13){ $('#submit').click(); } }); </script> </html> <?php } ?>
Code Javascript : 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 function tryConnect(){ var save = 0; if($('#remember').is(':checked')){ save = 1; } $.ajax({ type: 'POST', url: '/authentif/auth.php', data: {"pseudo" : $('#pseudo').val(), "pass" : $('#pass').val(), "save" : save }, dataType:'JSON', success: function(data) { console.log(data); if(data != false){ //si le mot de passe est bon var arr = $.map(data, function(el) { return el; }); if(arr[0] == true){ //si le mot de passe est à changer console.log(arr); $('#login-form').remove(); $('.container').append(' <div id="mdpChange" class="form-signin" style="width:300px; margin : auto;"></div>'); $('#mdpChange').append('<h2 class="form-signin-heading">Password Change</h2>') $('#mdpChange').append('<input type="hidden" id="id" value="'+arr[1]+'" />') $('#mdpChange').append('<label for="pass1" class="sr-only">New password</label>') $('#mdpChange').append('<input type="password" id="pass1" class="form-control" placeholder="New password" required="" autofocus="">') $('#mdpChange').append('<label for="pass2" class="sr-only">Password Confirmation</label>') $('#mdpChange').append('<input type="password" id="pass2" class="form-control" placeholder="Retype the password" required="">') $('#mdpChange').append('<button id="submit" onclick="changePassword()" class="btn btn-lg btn-primary btn-block" type="submit">Change it</button>') $('#mdpChange').append('<div id="resultat"></div>') }else{ $("#resultat").empty(); window.location.reload(); } }else{ // si le mot de passe est à changer $("#resultat").empty(); if (data == false){ $("#resultat").append("<p style='color: red;'>Erreur : Le mot de passe ou le pseudo est faux</p>"); }else{ $("#resultat").empty(); window.location.reload(); } } } }); }
Code PHP : 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 function auth($pseudo, $pass, $save){ try{ $bdd = new PDO('mysql:host='.DB_HOST.';port='.DB_PORT.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS); }catch (Exception $e){ die('Erreur : ' . $e->getMessage()); } $exist = $bdd->query('SELECT COUNT(*) as nb from User where pseudo = "'.$pseudo.'"'); //Existence d'un utilisateur if($exist->fetchColumn() > 0){ $result = $bdd->query('SELECT id, passHashed, isAdmin, authChang, nagiosCFG, massack, dashtool from User where pseudo ="'.$pseudo.'"'); $data = $result->fetch(); if($data['passHashed'] === crypt($_POST["pass"], $data['passHashed'])){ //Si le cookie a expiré, l'utilisateur n'a rien demandé, donc l'info est toujours en base. Le supprimer if(!isset($_COOKIE['id'])){ cleaningBase($data ['id']); } if($data["authChang"] == 1){ //Si le mot de passe a eu l'autorisation d'être changé $datasent = array('passChange' => true, 'id' => $data['id']); echo json_encode($datasent); exit(); } //Enregistrement de l'authentifié à sa demande et enregistrement if($save == 1){ $cookid = generateCookieId(); $insertCookie = $bdd->prepare("INSERT INTO Cookie_Link value('".$cookid."',".$data['id'].")"); $insertCookie->bindParam(1, $cookid); $insertCookie->bindParam(2, $data['id']); $insertCookie->execute(); setcookie('id', $cookid, time() + 365*24*3600, null, null, false, true); } //Variable de session $_SESSION["User"]=$pseudo; $_SESSION["id"]=$data['id']; $_SESSION["is_Admin"]=isAdmin($data['isAdmin']); $_SESSION["nagiosCFG"]=$data['nagiosCFG']; $_SESSION["massack"]=$data['massack']; $_SESSION["dashtool"]=$data['dashtool']; //Enregistrement Log si spécifié if(LOG_ALL_AUTH == true){ logConnect($data['id'], 1); } $data = array('User' => $pseudo, 'admin' => isAdmin($data['isAdmin']), 'id' => $data['id']); echo json_encode($data); // si le mot de passe correspond exit(); }else { echo json_encode(false); // si le mot de passe ne correspond pas exit(); } }else{ echo json_encode(false); //si l'utilisateur n'existe pas exit(); } } function authByCookie($id){ try{ $bdd = new PDO('mysql:host='.DB_HOST.';port='.DB_PORT.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS); }catch (Exception $e){ die('Erreur : ' . $e->getMessage()); } $exist = $bdd->query('SELECT COUNT(*) as nb from Cookie_Link where id = "'.$id.'"'); //Existence d'une entrée avec ce cookie if($exist->fetchColumn() > 0){ //S'il y en a une, on peut valider $get = $bdd->query('SELECT User.id, User.pseudo, User.isAdmin, User.nagiosCFG, User.massack, User.dashtool from User join Cookie_Link on (User.id = Cookie_Link.idUser) where Cookie_Link.id = "'.$id.'"'); $data = $get->fetch(); $_SESSION["User"]=$data['pseudo']; $_SESSION["id"]=$data['id']; $_SESSION["is_Admin"]=isAdmin($data['isAdmin']); $_SESSION["nagiosCFG"]=$data['nagiosCFG']; $_SESSION["massack"]=$data['massack']; $_SESSION["dashtool"]=$data['dashtool']; if(LOG_ALL_AUTH == true){ logConnect($data['id'], 0); } exit(); }else{ //Sinon, on peut supprimer le cookie puisqu'il est faux setcookie('id', null, time()-1, null, null, false, true); if(LOG_ALL_AUTH == true){ logDisconnect($data['id'], 0); } exit(); } } function generateCookieId(){//Nettoyage en cas d'expiration de cookie sans déconnection $characts = 'abcdefghijklmnopqrstuvwxyz'; $characts .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $characts .= '1234567890'; $flag = false; try{ $bdd = new PDO('mysql:host='.DB_HOST.';port='.DB_PORT.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS); }catch (Exception $e){ die('Erreur : ' . $e->getMessage()); } while($flag == false){ $code_aleatoire = ''; for($i=0;$i < 16;$i++) { $code_aleatoire .= $characts[ rand() % strlen($characts) ]; } $isGood = $bdd->query('SELECT COUNT(*) as id from Cookie_Link where id = "'.$code_aleatoire.'"'); if($isGood->fetchColumn() > 0){ // Si (même si c'est pratiquement impossible) il existe déjà, on refait $flag = false; }else{ $flag = true; } } return $code_aleatoire; }
Tout marche normalement, ça s'actualise et affiche le message adéquate quand il le faut. Maintenant, il faut actualisé à cause de ce '
- La création de dashboard dans un projet de dashboard :
Içi, tout fonctionne et tout s'est mis à jour
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 $.ajax({ type: 'POST', url: 'addDashboard.php', data: {"nom" : $("#nomDash").val(), "id" : $("#user_id").val()}, dataType:'JSON', success : function(id){ flushAll(); chargeDashboard(id); $.growlUI('Succés', 'Dashboard Ajouté'); } });
Merci d'avance de votre aide.
Partager