✅ Notizie, temi, plugin WEB e WordPress. Qui condividiamo suggerimenti e le migliori soluzioni per siti web.

I veri gestori di eccezioni in PHP e Javascript

32

@Jordan Hall twitta veri gestori di eccezioni in PHP e Javascript.

Gestore di eccezioni Javascript

try {
  // something
} catch (e) {
   window.location.href = "<a class="vglnk" target="_blank" href="http://stackoverflow.com/search?q=[js" rel="nofollow"><span>http</span><span>://</span><span>stackoverflow</span><span>.</span><span>com</span><span>/</span><span>search</span><span>?</span><span>q</span><span>=[</span><span>js</span></a>] + "
                        + e.message;
}

Quindi, se le cose vanno storte, la pagina verrà reindirizzata a StackOverflow per le risposte.

Gestore di eccezioni PHP

Allo stesso modo, ecco le versioni di PHP:

try {
  // something
} catch (Exception $e) {
  header("Location: <a class="vglnk" target="_blank" href="https://stackoverflow.com/search?q=[php" rel="nofollow"><span>https</span><span>://</span><span>stackoverflow</span><span>.</span><span>com</span><span>/</span><span>search</span><span>?</span><span>q</span><span>=[</span><span>php</span></a>] ".$e->getMessage());
}

Puoi dividere il try-catch in due file e lasciare che il server includa automaticamente il tuo codice PHP all’interno di questo grande try-catch.

Avrai bisogno delle direttive php auto_prepend_file e auto_append_file. e modificare il. file htaccess.

php_value auto_prepend_file "/path/to/file/before.php"
php_value auto_append_file "/path/to/file/after.php"

Inserisci quanto segue before.php

e metti il ​​resto in after.php

} catch (Exception $e) {
  header("Location: <a class="vglnk" target="_blank" href="https://stackoverflow.com/search?q=[php" rel="nofollow"><span>https</span><span>://</span><span>stackoverflow</span><span>.</span><span>com</span><span>/</span><span>search</span><span>?</span><span>q</span><span>=[</span><span>php</span></a>] ".$e->getMessage());
}

In alternativa, puoi inserire queste definizioni in php.ini (richiede la modalità cgi, interessa l’intero server web).

auto_prepend_file  = "/path/to/file/before.php"
auto_append_file   = "/path/to/file/after.php"

Fonte di registrazione: helloacm.com

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More