ouvag/admin/inc/generate.php

55 lines
1.4 KiB
PHP

<?php
session_start();
// On teste si la variable de session existe et contient une valeur
if(empty($_SESSION['login']))
{
// Si inexistante ou nulle, on redirige vers le formulaire de login
header('Location: ../auth.php');
exit();
}
include("config.inc.php");
include("connect.inc.php");
include("fonctions.inc.php");
// Requête MySQL
$query = $bdd->query('SELECT reference,password FROM clients WHERE password IS NULL');
$nb = $query->rowCount();
// Si le retour est vide, on affiche un petit message
if ($query->rowCount() > 0) {
// on génére un password pour chaque référence
while ($results = $query->fetch(PDO::FETCH_OBJ)) {
$ref = $results->reference;
$mdp = passwdgen(10);
// Préparation de la requête
$sql ="
UPDATE clients
SET password = :mdp
WHERE reference = :ref
";
$stmt = $bdd->prepare($sql);
$stmt->bindParam(':mdp', $mdp, PDO::PARAM_STR);
$stmt->bindParam(':ref', $ref, PDO::PARAM_STR);
// Exécution de la requête
$stmt->execute();
// On enregistre que les mots de passe sont générés
$sql ="
UPDATE admin
SET option_value = 1
WHERE option_id = 9
";
$stmt = $bdd->prepare($sql);
// Exécution de la requête
$stmt->execute();
// Clore la requête
$stmt->closeCursor();
$stmt = NULL;
header('Location: ../index.php?$generate=true&$nb='.$nb);
}
} else {header('Location: ../index.php?$generate=false');}
?>