safe (method)

Last updated: Fri, Apr 14, 2023 4:53 pm
Return to Knowledgebase


The purpose of this method is to safely redirect users to a given location, after checking if the location is valid and accessible.




# Parameter Data Type Required Description
1 $location string Yes The location you wish to redirect to
2 $args='' string Yes Additional arguments
3 $location = null Yes undocumented
4 $args='' Yes undocumented

Further Documentation:

The method takes two parameters: $location, which is the URL or file path to redirect to, and $args, which is a string of additional arguments to append to the URL, if any.

The method first sets some global variables such as $us_url_root, $settings, and $user, which are used to construct the redirect URL.

If $location is not null, the method checks if debug mode is enabled in $settings, and if so, logs a diagnostic message with information about the redirect using the logger function.

The method then checks if the $location is a valid URL or file path by using the file_exists function and checking if it starts with 'http://' or 'https://'. If it is not a valid URL or file path, it tries different prefixes such as $us_url_root, '../', 'users/', and so on, to see if the
file exists in any of these directories. If it finds a match, it updates $location with the full path to the file.

If $args is not empty, the method appends it to $location to include any additional arguments in the URL.

If headers have not already been sent to the browser, the method uses the header function to send a Location header with the updated $location, and exits the script using the exit function.

If headers have already been sent to the browser, the method uses JavaScript to redirect to the $location, or if JavaScript is disabled, uses a meta refresh tag to redirect.

Overall, the "safe" method provides a robust way to safely redirect users to a given location, even if the file path or URL is not immediately accessible, and works around potential issues with headers already being sent.