Function: redirectTo
avatar
Jérôme Kägi


Function: redirectTo

The redirectTo function is used to redirect the user to a specified URL and optionally set a session message. It serves as a wrapper for JScms_redirect, allowing developers to include custom messages during redirection, such as success, error, or informational messages.

Syntax


redirectTo($url, $messageKey = null, $message = null)
    

Parameters

  • $url: (string) The target URL to which the user should be redirected.
  • $messageKey: (string|null) Optional. The key used to store the message in the session, such as 'successmsg', 'errormsg', or 'infomsg'. Default is null.
  • $message: (string|null) Optional. The message to store in the session under $messageKey. Default is null.

Returns

This function does not return a value. It sets a session message (if provided) and redirects the user to the specified URL using JScms_redirect.

Example Usage


<?php
// Redirect to the homepage without a message
redirectTo(BASE_URL);

// Redirect to a page with a success message
redirectTo(BASE_URL, 'successmsg', $lang["successful"]);

// Redirect to a login page with an error message
redirectTo(BASE_URL, 'errormsg', $lang["login_required"]);

// Redirect to a profile page with an informational message
redirectTo(BASE_URL, 'infomsg', $lang["profile_updated"]);
?>
    

Best Practices

  • Validate and sanitize the $url to prevent potential security vulnerabilities like open redirects.
  • Use descriptive $messageKey values (e.g., 'successmsg', 'errormsg', 'infomsg') to ensure clarity when retrieving messages.
  • Test the session storage and retrieval of messages to verify correct functionality.

Common Issues

  • Session Not Started: Ensure that the PHP session is started before using this function; otherwise, the message will not be stored.
  • Invalid URLs: Verify that the $url parameter is a valid and accessible URL.
  • Conflicting Keys: Ensure that the $messageKey does not overwrite critical session keys unintentionally.
Was this article helpful?
0 out of 0 found this helpful