Ajout del'inscription, connection et deconnection avec bd

This commit is contained in:
Thomas FOLLEA 2024-06-09 17:31:35 +02:00
parent 6ff83097b2
commit c61c485651
9 changed files with 147 additions and 67 deletions

@ -58,7 +58,7 @@ $autoload['packages'] = array();
| |
| $autoload['libraries'] = array('user_agent' => 'ua'); | $autoload['libraries'] = array('user_agent' => 'ua');
*/ */
$autoload['libraries'] = array(); $autoload['libraries'] = array('session');
/* /*
| ------------------------------------------------------------------- | -------------------------------------------------------------------

@ -50,5 +50,10 @@ defined('BASEPATH') OR exit('No direct script access allowed');
| my-controller/my-method -> my_controller/my_method | my-controller/my-method -> my_controller/my_method
*/ */
$route['default_controller'] = 'albums'; $route['default_controller'] = 'albums';
$route['connexion'] = 'ConnexionController/connexion';
$route['inscription'] = 'InscriptionController/inscription';
$route['traitement'] = 'InscriptionController/traitement';
$route['authentifier'] = 'ConnexionController/authentifier';
$route['deconnexion'] = 'ConnexionController/deconnexion';
$route['404_override'] = ''; $route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE; $route['translate_uri_dashes'] = FALSE;

@ -11,19 +11,14 @@ class Albums extends CI_Controller {
$this->load->helper('html'); $this->load->helper('html');
$this->load->helper('url'); $this->load->helper('url');
$this->load->helper('form'); $this->load->helper('form');
$this->filter = $this->input->get('filter');
} }
public function index(){ public function index(){
if ($this->filter == "co") {
$this->load->view('layout/header');
$this->load->view('new_user');
}else{
$albums = $this->model_music->getAlbums(); $albums = $this->model_music->getAlbums();
$this->load->view('layout/header'); $this->load->view('layout/header');
$this->load->view('albums_list',['albums'=>$albums]); $this->load->view('albums_list',['albums'=>$albums]);
$this->load->view('layout/footer'); $this->load->view('layout/footer');
}
} }
} }

@ -0,0 +1,46 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ConnexionController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url', 'html'));
$this->load->library('session');
}
public function connexion() {
$this->load->view('layout/header');
$this->load->view('connexion');
$this->load->view('layout/footer');
}
public function authentifier() {
if($_SERVER["REQUEST_METHOD"] == "POST"){
$email = $_POST['email'];
$password = $_POST['password'];
if($email != "" && $password != ""){
$this->load->database();
$query = $this->db->query("SELECT * FROM users WHERE email = '$email' AND mdp = '$password'");
$result = $query->row(); // Récupérer la première ligne de résultat
if($result){ // Vérifier si l'utilisateur existe
$this->session->set_userdata('pseudo', $result->pseudo);
redirect('../index.php');
} else {
$data['error_msg'] = "Email ou mot de passe incorrect.";
}
}
$this->load->view('layout/header');
$this->load->view('connexion', $data);
$this->load->view('layout/footer');
}
}
public function deconnexion() {
$this->session->unset_userdata('pseudo');
$this->session->sess_destroy();
redirect('../index.php');
}
}

@ -0,0 +1,47 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class InscriptionController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url', 'html'));
}
public function inscription() {
$this->load->view('layout/header');
$this->load->view('inscription');
$this->load->view('layout/footer');
}
public function traitement() {
if(isset($_POST['ok'])){
$this->load->database();
$prenom = ucfirst(strtolower($this->input->post('prenom')));
$nom = strtoupper($this->input->post('nom'));
$pseudo = $this->input->post('pseudo');
$mdp = $this->input->post('pass');
$email = $this->input->post('email');
$data = array(
'pseudo' => $pseudo,
'nom' => $nom,
'prenom' => $prenom,
'mdp' => $mdp,
'email' => $email
);
$this->db->insert('users', $data);
$data['confirmation_message'] = "Inscription réussie ! Vous êtes maintenant inscrit.";
$this->load->view('layout/header');
$this->load->view('inscription', $data);
$this->load->view('layout/footer');
}
}
}

@ -0,0 +1,12 @@
<h1>Se connecter</h1>
<form action="authentifier" method="POST">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Mot de passe:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Connexion</button>
</form>
<?php if(isset($error_msg)): ?>
<div class="error-message"><?= $error_msg ?></div>
<?php endif; ?>

@ -0,0 +1,24 @@
<h1>S'inscrire</h1>
<form action="traitement" method="POST">
<label for="nom">Votre nom*</label>
<input type="text" id="nom" name="nom" placeholder="Entrez votre nom" required>
<label for="prenom">Votre prénom*</label>
<input type="text" id="prenom" name="prenom" placeholder="Entrez votre prénom" required>
<label for="pseudo">Votre pseudo*</label>
<input type="text" id="pseudo" name="pseudo" placeholder="Entrez votre pseudo" required>
<label for="email">Votre email*</label>
<input type="email" id="email" name="email" placeholder="Entrez votre adresse email" required>
<label for="pass">Votre mot de passe*</label>
<input type="password" id="pass" name="pass" placeholder="Entrez votre mdp" required>
<input type="submit" value="M'inscrire" name="ok">
</form>
<?php if(isset($confirmation_message)): ?>
<div class="confirmation-message"><?= $confirmation_message ?></div>
<?php endif; ?>

@ -19,9 +19,15 @@
</ul> </ul>
<ul> <ul>
<li><?=anchor('albums','Albums');?></li> <li><?=anchor('albums','Albums');?></li>
<li><?=anchor('artistes','Artistes');?></li> <li><?=anchor('artistes','Artistes');?></li>
<li><?=anchor('Albums/?filter=co','Se connecter');?></li> <?php if ($this->session->userdata('pseudo')) : // Vérifier si l'utilisateur est connecté ?>
</ul> <li><?= $this->session->userdata('pseudo'); ?></li>
</nav> <li><?= anchor('deconnexion', 'Se déconnecter'); ?></li>
</body> <?php else : ?>
<li><?= anchor('connexion', 'Se connecter'); ?></li>
<li><?= anchor('inscription', 'Inscription'); ?></li>
<?php endif; ?>
</ul>
</nav>
</body>
</html> </html>

@ -1,55 +0,0 @@
<!doctype html>
<html lang="en" class="authentification page">
<head>
<meta charset="UTF-8" />
<title>CONNEXION</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<?=link_tag('assets/style.css')?>
</head>
<body>
<section class="connexion">
<div class="authentification">
<h3> Déjà abonné </h3>
<form action="reponse.php" method="GET">
<p>Adresse mail</p>
<input type="text" name="Email">
<p>Mot de passe</p>
<input type="text" name="PW">
<input type=submit value="Connexion">
</div>
<div class="new">
<h3> Création de compte </h3>
<form action="reponse.php" methode="GET">
<p>Nom</p>
<input type="text" name="Nom">
<p>Prénom</p>
<input type="text" name="Prenom">
<p>Adresse mail</p>
<input type="text" name="Email">
<p>Mot de passe</p>
<input type="text" name="PW">
<input type=submit value="Créer">
</div>
</form>
</main>
</body>
</html>