Function: JScms_redirect
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


Function: JScms_redirect

The JScms_redirect function is used to redirect the user to a specified URL with an optional HTTP status code. It ensures proper redirection by setting the HTTP Location header and stopping further script execution.

**Syntax**


JScms_redirect($url, $code = 302)
    

**Parameters**

  • $url: (string) The target URL to which the user should be redirected. This URL is decoded using html_entity_decode.
  • $code: (int) Optional. The HTTP status code for the redirection. Default is 302 (Found / Temporary Redirect).

**Returns**

This function does not return a value. It sends a Location header to the browser and terminates the script using exit.

**Example Usage**


<?php
// Redirect to the homepage with a 302 status code
JScms_redirect('https://example.com/home');

// Redirect to a 404 error page with a 404 status code
JScms_redirect('https://example.com/404', 404);
?>
    

**Best Practices**

  • Ensure that the $url is properly validated and sanitized to prevent potential security issues such as open redirects.
  • Use the appropriate HTTP status code for the redirection context. For example:
    • 301 for permanent redirects.
    • 302 for temporary redirects.
    • 404 for not found pages.
  • Call this function before any output is sent to the browser to avoid headers already sent errors.

**Common Issues**

  • Headers Already Sent: Ensure that no output (including whitespace) is sent before calling this function, as it relies on setting HTTP headers.
  • Invalid URLs: Verify that the $url parameter is a valid and accessible URL.
Was this article helpful?
0 out of 0 found this helpful