general redirect() question

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
worker201
Registered User
Posts: 17
Joined: Fri Apr 11, 2008 3:49 am

general redirect() question

Post by worker201 »

(I know there's a similar active topic about this right now, but I didn't want to hijack the other user's thread)

I have the following bit of code:

Code: Select all

if ( !$userdata['session_logged_in'] )
        {
            message_die(GENERAL_MESSAGE, $lang['Not_authorized']);
            redirect(append_sid("index.$phpEx"));
        }
Basically, if a user that is not logged in tries to do X, he gets bounced to the index page. What else is required in the page for this code to work? As it stands, the message comes up properly, but the redirect doesn't occur. How is the redirect implemented?
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53398
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: general redirect() question

Post by Brf »

That is because message_die terminates the script, so it never executes the redirect.
What you want to do is set

Code: Select all

$template->assign_vars(array{
'META' => '<meta http-equiv="refresh" content="3;url=' . append_sid("index.$phpEx?")  . '">')
));
Before the message_die. Then it will meta-refresh redirect to the index after displaying the message.
worker201
Registered User
Posts: 17
Joined: Fri Apr 11, 2008 3:49 am

Re: general redirect() question

Post by worker201 »

Works like a charm, much appreciated. Your curly brace typo threw me for a minute, but I figured it out.

New code:

Code: Select all

if ( !$userdata['session_logged_in'] )
        {
            $template->assign_vars(array(
                'META' => '<meta http-equiv="refresh" content="3;url=' . append_sid("index.$phpEx?")  . '">'));
            message_die(GENERAL_MESSAGE, $lang['Not_authorized']);
        }
Am I reading this right? content="3;url=X" - that's a strange construction. I'll have to look that one up.

Thanks much for your help.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53398
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: general redirect() question

Post by Brf »

worker201 wrote: Am I reading this right? content="3;url=X" - that's a strange construction. I'll have to look that one up.
Ah.. I had an extra close-paren.
Yes. That is the correct content for a meta refresh. The first parameter is the wait in seconds, while the second parameter is the URL to redirect to.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: general redirect() question

Post by 3Di »

What's the purpose to redirect the not-logged-in users to the index page instead to clearly let them know they have to register in order to do 'blah' ?

Having them facing the login screen I think it is more efficient. This code should do that..:

Code: Select all

if ( !$userdata['session_logged_in'] )
{
	redirect(append_sid("login.$phpEx", true));
}
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
worker201
Registered User
Posts: 17
Joined: Fri Apr 11, 2008 3:49 am

Re: general redirect() question

Post by worker201 »

3Di wrote:What's the purpose to redirect the not-logged-in users to the index page instead to clearly let them know they have to register in order to do 'blah' ?
Perhaps sending them to the login screen would make more sense. But your code does not tell them why they have been sent to the login screen. Sending the potential user to a message screen which tells them that they must be registered and logged in to use the feature in question is more user friendly. Bumping them from there to the login screen is probably the best compromise.
Post Reply

Return to “[2.0.x] MOD Writers Discussion”