phpBB

Development Wiki

Function.redirect

From phpBB Development Wiki

redirect –– Redirects the user to another page

Description

  1. mixed redirect ( $url [, $return [, $disable_cd_check ]] )

Redirects the user to another page then exits the script nicely. This function is intended for urls within the board. It's not meant to redirect to cross-domains.

Parameters

Parameter Required Usage
url (string) Yes The url to redirect to.
return (bool) No (false) If true, do not redirect but return the sanitized URL. Default is no return.
disable_cd_check (bool) No (false) If true, redirect() will redirect to an external domain. If false, the redirect point to the boards url if it does not match the current domain. Default is false.

Return Values

If the $return parameter is set to true, redirect returns a URL string. Otherwise, it will exit the script upon redirecting the user to the redirect URL.

Examples

Example #1 from viewtopic.php

This example shows the redirect function usage within the viewtopic.php page, which is redirecting the user to the topic if the post_id was incorrect but the topic_id was found.

if (!$topic_data)
{
    
// If post_id was submitted, we try at least to display the topic as a last resort...
    
if ($post_id && $forum_id && $topic_id)
    {
        
redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx""f=$forum_id&t=$topic_id"));
    }

    
trigger_error('NO_TOPIC');
}

Example #2 from viewforum.php

This example is from viewforum.php and takes the forum_link and redirects it to the URL you specified for that link forum.

// We redirect to the url. The third parameter indicates that external redirects are allowed.
redirect($forum_data['forum_link'], falsetrue);
exit;

Example #3 Redirect Bots/Spiders

In this example, we will ensure a user is logged in, if not, we display a login box. If they are a bot or spider, we will redirect them to the Board Index.

// is the user logged in?
if (!$user->data['is_registered'])
{
    if (
$user->data['is_bot'])
    {
        
// the user is a bot, send them back to home plate...
        
redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
    }
    
// the user is not logged in, give them a chance to login here...
    
login_box('''LOGIN');
}

Notes

The third parameter disable_cd_check must be set to true to allow for external URL redirects, this is a security measure.
To redirect other users to other pages, for instance after a performed action, use the meta_refresh function, this will display the current page for a specified number of seconds before redirecting the user to the redirect page

See Also