Simplifier son code PHP pour utiliser la POO

Par FrenchGaming Team, il y a 10 ans


Voila j'ai un code qui permet de sauvgarder des choses dans des variables $_SESSION car je l'ai utilise sur un autre page. j'ai essayer de le simplifier mais je trouve pas comment . Aurrait t'il une méthode en POO

if($_SERVER['HTTP_REFERER'] == $adresseSite + 'commande') { if(isset($_POST) && isset($_POST['jeu']) && isset($_POST['email_psn']) && isset($_POST['password_psn']) && isset($_POST['token_custom_site']) && isset($_POST['f5_img'])) { if($_POST['token_custom_site'] == $_SESSION['token']) { if(filter_var($_POST['email_psn'], FILTER_VALIDATE_EMAIL)) { $_SESSION['jeu'] = filter_var(htmlentities($_POST['jeu'], FILTER_SANITIZE_STRING)); $_SESSION['contact'] = filter_var(htmlentities($_POST['contact'], FILTER_SANITIZE_STRING)); $_SESSION['email_psn'] = filter_var(htmlentities($_POST['email_psn'], FILTER_SANITIZE_STRING)); $_SESSION['password_psn'] = filter_var(htmlentities($_POST['password_psn'], FILTER_SANITIZE_STRING)); $_SESSION['niveau'] = filter_var(htmlentities($_POST['niveau'], FILTER_SANITIZE_STRING)); $_SESSION['argent'] = filter_var(htmlentities($_POST['argent'], FILTER_SANITIZE_STRING)); $_SESSION['maxcomp'] = filter_var(htmlentities($_POST['maxcomp'], FILTER_SANITIZE_STRING)); $_SESSION['unlock'] = filter_var(htmlentities($_POST['unlock'], FILTER_SANITIZE_STRING)); $_SESSION['prestige'] = filter_var(htmlentities($_POST['prestige'], FILTER_SANITIZE_STRING)); $_SESSION['level'] = filter_var(htmlentities($_POST['level'], FILTER_SANITIZE_STRING)); $_SESSION['classes'] = filter_var(htmlentities($_POST['classes'], FILTER_SANITIZE_STRING)); $_SESSION['modded'] = filter_var(htmlentities($_POST['modded'], FILTER_SANITIZE_STRING)); $_SESSION['tenu'] = filter_var(htmlentities($_POST['tenu'], FILTER_SANITIZE_STRING)); $_SESSION['extrainformation'] = filter_var(htmlentities($_POST['extrainformation'], FILTER_SANITIZE_STRING)); header('Location:paiement'); } else { echo '<div class="alert alert-danger" style="background-color: #f87961;border-color: transparent;color: #fff;"><button type="button" class="close" data-dismiss="alert" aria-hidden="true" style="color: #fff;">×</button> L\'adresse email saisit est incorrect.</div>'; } } else { echo '<div class="alert alert-danger" style="background-color: #f87961;border-color: transparent;color: #fff;"><button type="button" class="close" data-dismiss="alert" aria-hidden="true" style="color: #fff;">×</button> Token incorrect.</div>'; } } else { echo ''; } } else { echo ''; } ?>

1 réponse

DevMath, il y a 10 ans

Salut, il y as pas vraiment besoin de POO pour simplifier mais pour la fonction isset() elle peut prendre plusieurs paramètres.
Tu peux aussi a la place de stocker 1 par 1 les donnée de $_POST dans $_SESSION , parcourir un tableau qui s'occupe de faire sa en une fois, voici ton code simplifié :

if($_SERVER['HTTP_REFERER'] == $adresseSite + 'commande') { if(isset($_POST, $_POST['jeu'], $_POST['email_psn'], $_POST['password_psn'], $_POST['token_custom_site'], $_POST['f5_img'])) { if($_POST['token_custom_site'] == $_SESSION['token']) { $post2session = ['jeu', 'contact', 'email_psn', 'password_psn', 'niveau', 'argent', 'maxcomp', 'unlock', 'prestige', 'level', 'classes', 'modded', 'tenu', 'extrainformation']; if(filter_var($_POST['email_psn'], FILTER_VALIDATE_EMAIL)) { foreach ($post2session as $key) { $_SESSION[$key] = filter_var(htmlentities($_POST[$key], FILTER_SANITIZE_STRING)); } header('Location:paiement'); } else { echo '<div class="alert alert-danger" style="background-color: #f87961;border-color: transparent;color: #fff;"><button type="button" class="close" data-dismiss="alert" aria-hidden="true" style="color: #fff;">×</button> L\'adresse email saisit est incorrect.</div>'; } } else { echo '<div class="alert alert-danger" style="background-color: #f87961;border-color: transparent;color: #fff;"><button type="button" class="close" data-dismiss="alert" aria-hidden="true" style="color: #fff;">×</button> Token incorrect.</div>'; } } } ?>

Faire un echo de rien '' , revient au même de ne rien faire, alors pas besoin de else{}