Jump to content

Si produit inexistant redirection permanente vers 404.php


Recommended Posts

Bonjour,

j'ai besoins de l'aide d'un connaisseur :
Je souhaite ajouter un bout de code au sommet de la page product.php en respectant les requetes utilisées par presta/smarty.
Le but: à chaque visite dans la page product.php, récupérer la variable id produit contenue dans l'url ($_GET['id_product']), et vérifier si cet id produit existe dans la bdd products.
Si l'id produit n'existe pas (suite à une suppression par exemple) on effectue une redirection permanente sur la page 404.php.

Dans l'attente d'une aide,
à plus...

Link to comment
Share on other sites

Bonjour,

La gestion de l'erreur suite à une référence produit inexistante existe déjà à la ligne 107 environ du fichier product.php

if (!$id_product = intval(Tools::getValue('id_product')) OR !Validate::isUnsignedId($id_product))
   $errors[] = Tools::displayError('product not found');



Est-ce qu'une simple modification comme ceci suffirait ?

if (!$id_product = intval(Tools::getValue('id_product')) OR !Validate::isUnsignedId($id_product))
   Tools::redirect('404.php');
   //$errors[] = Tools::displayError('product not found');



Je n'ai pas testé.

Bien cordialement
A+

Link to comment
Share on other sites

Merci Devnet pour ta réponse,
malheureusement ça ne fonctionne pas, je pense qu'il faille au préalable créer la fonction redirect() mais pas certain.
En attendant d'avoir une solution j'ai procédé à une requète standard de la bdd, ça marche mais c'est pas très propre pour le script.
voir http://www.esocalice.fr ou http://www.esocalice.net

Cordialement,
Manu

Link to comment
Share on other sites

La fonction est en natif sur le kernel de PerstaShop dans la classe Tools.php :

   /**
   * Redirect user to another page
   *
   * @param string $url Desired URL
   * @param string $baseUri Base URI (optional)
   */
   static public function redirect($url, $baseUri = __PS_BASE_URI__)
   {
       if (isset($_SERVER['HTTP_REFERER']) AND ($url == $_SERVER['HTTP_REFERER']))
           header('Location: '.$_SERVER['HTTP_REFERER']);
       else
           header('Location: '.$baseUri.$url);
       exit;
   }



Bien cordialement
A+

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...