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 » Thu Jun 26, 2008 2:51 am

(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: 50698
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: general redirect() question

Post by Brf » Thu Jun 26, 2008 4:17 am

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 » Thu Jun 26, 2008 4:40 am

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: 50698
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: general redirect() question

Post by Brf » Thu Jun 26, 2008 5:03 pm

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
Registered User
Posts: 11703
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: general redirect() question

Post by 3Di » Fri Jun 27, 2008 12:27 am

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));
}
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
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 » Fri Jun 27, 2008 1:48 am

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”

Who is online

Users browsing this forum: No registered users and 4 guests

cron