TUTO PHP - Système de parrainage
Il existe différentes façons de mettre en place un système de parrainage sur un site internet. Je vous en propose donc une relativement simple qui vous donnera l'architecture de base en l'intégrant à un espace membre déjà créé ! Bien sûr, n'hésitez pas à ajuster le code en fonction de votre but, à créer des systèmes de rangs par points, etc.
- inscription.php
<?php
$bdd = new PDO('mysql:host=127.0.0.1;dbname=espace_membre', 'root', '');
if(isset($_GET['p']) AND !empty($_GET['p'])){
$parrain_uniqid = htmlspecialchars($_GET['p']);
$req_parrain = $bdd->prepare('SELECT id FROM membres WHERE uniqid = ?');
$req_parrain->execute(array($parrain_uniqid));
$parrain_exist = $req_parrain->rowCount();
if($parrain_exist == 1) {
$id_parrain = $req_parrain->fetch();
$id_parrain = $id_parrain['id'];
}
}
if(isset($_POST['forminscription']))
{
$pseudo = htmlspecialchars($_POST['pseudo']);
$mail = htmlspecialchars($_POST['mail']);
$mail2 = htmlspecialchars($_POST['mail2']);
$mdp = sha1($_POST['mdp']);
$mdp2 = sha1($_POST['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))
{
$reqmail = $bdd->prepare("SELECT * FROM membres WHERE mail = ?");
$reqmail->execute(array($mail));
$mailexist = $reqmail->rowCount();
if($mailexist == 0)
{
if($mdp == $mdp2)
{
$longueurKey = 15;
$key = "";
for($i=1;$i<$longueurKey;$i++) {
$key .= mt_rand(0,9);
}
$insertmbr = $bdd->prepare("INSERT INTO membres(pseudo, mail, motdepasse, confirmkey, uniqid, id_parrain) VALUES(?, ?, ?, ?, ?, ?)");
if(isset($id_parrain) AND !empty($id_parrain)) {
$insertmbr->execute(array($pseudo, $mail, $mdp, $key, uniqid(), $id_parrain));
} else {
$insertmbr->execute(array($pseudo, $mail, $mdp, $key, uniqid(), 0));
}
$header="MIME-Version: 1.0\r\n";
$header.='From:"PrimFX.com"<support@primfx.com>'."\n";
$header.='Content-Type:text/html; charset="uft-8"'."\n";
$header.='Content-Transfer-Encoding: 8bit';
$message='
<html>
<body>
<div align="center">
<a href="http://127.0.0.1/Tutos%20PHP/%2314%20%28Espace%20membre%29/confirmation.php?pseudo='.urlencode($pseudo).'&key='.$key.'">Confirmez votre compte !</a>
</div>
</body>
</html>
';
mail($mail, "Confirmation de compte", $message, $header);
$erreur = "Votre compte a bien été créé ! <a href=\"connexion.php\">Me connecter</a>";
}
else
{
$erreur = "Vos mots de passes ne correspondent pas !";
}
}
else
{
$erreur = "Adresse mail déjà utilisée !";
}
}
else
{
$erreur = "Votre adresse mail n'est pas valide !";
}
}
else
{
$erreur = "Vos adresses mail ne correspondent pas !";
}
}
else
{
$erreur = "Votre pseudo ne doit pas dépasser 255 caractères !";
}
}
else
{
$erreur = "Tous les champs doivent être complétés !";
}
}
?>
<html>
<head>
<title>TUTO PHP</title>
<meta charset="utf-8">
</head>
<body>
<div align="center">
<h2>Inscription</h2>
<br /><br />
<form method="POST" action="">
<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>
</body>
</html>
- profil.php
<?php
session_start();
$bdd = new PDO('mysql:host=127.0.0.1;dbname=espace_membre', 'root', '');
include_once('cookieconnect.php');
if(isset($_GET['id']) AND $_GET['id'] > 0)
{
$getid = intval($_GET['id']);
$requser = $bdd->prepare('SELECT * FROM membres WHERE id = ?');
$requser->execute(array($getid));
$userinfo = $requser->fetch();
?>
<html>
<head>
<title>TUTO PHP</title>
<meta charset="utf-8">
</head>
<body>
<div align="center">
<h2>Profil de <?php echo $userinfo['pseudo']; ?></h2>
<br /><br />
<?php
if(!empty($userinfo['avatar']))
{
?>
<img src="membres/avatars/<?php echo $userinfo['avatar']; ?>" width="150" />
<?php
}
?>
<br /><br />
<?php
if(isset($_SESSION['id']) AND $_SESSION['id'] != $getid) {
$isfollowingornot = $bdd->prepare('SELECT * FROM follow WHERE id_follower = ? AND id_following = ?');
$isfollowingornot->execute(array($_SESSION['id'],$getid));
$isfollowingornot = $isfollowingornot->rowCount();
if($isfollowingornot == 1) {
?>
Vous suivez cette personne <a href="follow.php?followedid=<?php echo $getid; ?>">Ne plus suivre cette personne</a>
<br /><br />
<?php }else { ?>
<a href="follow.php?followedid=<?php echo $getid; ?>">Suivre cette personne</a>
<br /><br />
<?php
}
}
?>
Pseudo = <?php echo $userinfo['pseudo']; ?>
<br />
Mail = <?php echo $userinfo['mail']; ?>
<br />
<?php
$parrainages = $bdd->prepare('SELECT id FROM membres WHERE id_parrain = ?');
$parrainages->execute(array($getid));
$parrainages = $parrainages->rowCount();
?>
Nombre de parrainages = <?php echo $parrainages; ?>
<br />
<?php
if(isset($_SESSION['id']) AND $userinfo['id'] == $_SESSION['id'])
{
?>
<br />
<u>Parrainer un ami:</u> inscription.php?p=<?php echo $userinfo['uniqid']; ?><br /><br />
<a href="editionprofil.php">Editer mon profil</a>
<a href="reception.php">Mes messages</a>
<a href="deconnexion.php">Se déconnecter</a>
<?php
}
?>
</div>
</body>
</html>
<?php
}
?>
5 Commentaires
POSTER UN COMMENTAIRE
Vous devez être connecté pour pouvoir poster un commentaire...
Pas encore de compte ? Créez-en un ici !
MSTC
12 December 2015 à 10:54Pourrais-je avoir le code ?
Azok30
20 March 2017 à 22:37@MSTC Oui, tu paye comme on la plus part de personnes respectueuses de travail bien fait.
Michima
11 April 2017 à 11:49Bonjour, Code très explicite. Merci pour ce tutoriel
Kasilon
30 July 2017 à 22:19Bonjour, j'ai un site plus développé avec un design et tout et une sécurité optimal, je viens de représenter à peu prêt ce tuto, mais dans la page d'inscription, sa ne fonctionne pas, merci de me contacter sur fcb (seul moyen de contact) https://www.facebook.com/profile.php?id=100008373458183
essai
03 January 2018 à 17:09Bonjour,
Dommage que la bdd ne soit pas à télécharger pour les essais !