Firefox PHP

After login hook

Posted by dve845 
After login hook
January 17, 2011 10:53AM
Hello All,

I wrote an after login hook for my phorum installation. When I hardcode the url like this:

function phorum_monest_after_login($url) {
$url = "[www.monest.net];;
return $url;
}

it works like a charm
however i would like this hook to use a redirect url the is defined before the user is redirected to the phorum login page

so if i write this in a file a.php

$PHORUM['my_redirect']='path/to/this/a.php';

then i would be nice if the hook could do something like this:

function phorum_monest_after_login($url) {
$url = $PHORUM['my_redirect'];
return $url;
}

But this doesn't seem to work, no matter what I try.
Re: After login hook
January 17, 2011 12:04PM
There is nothing magical about the $PHORUM variable. It is a plain variable. It is not a session or so. Therefore, setting the URL in a.php would only work if a.php was included and run in the same request as in which your hook is handled.

Besides that, you started using $PHORUM in the function, without first telling PHP that you would like access to it. Something like this would be needed in the function for doing that:
Language: PHP
function phorum_monest_after_login($url) { global $PHORUM; $url = $PHORUM[';my_redirect';]; return $url; }

But apart from the problems that I see in your code, you might just want to use what Phorum already has in store. You can provide login.php with a "redir" parameter, to let Phorum redirect to the provided page URL. So a URL could look like:

http: //your.website.com/phorumdir/login.php?redir=http: //your.website.com/otherpage.php

(spaces added in the URLs to prevent them from being turned into links)
After login, the otherpage.php will be opened.

As long as you link to pages that reside on the same host as your Phorum installation, this should work. If you want to redirect to a totally different URL like http: //other.website.com/whatever, you wil have to tell Phorum that redirection is allowed (this is a security enhancement that was added in recent versions of Phorum). In this case, go to the admin interface -> General Settings. On that page, update the option "Allow Login-Redirection to the following URLs" (see the help button there for more info.)


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: After login hook
January 18, 2011 05:57PM
Great!
Thank you very much.
However de redir trick doens't seem to work with register.php?redir=...
Re: After login hook
January 18, 2011 06:14PM
That's correct, since the redir parameter feature (I call it a "feature", not a "trick") is only available for the login script. The register script will always redirect to the Phorum login page.

See this thread for some info on redirecting after register.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: After login hook
January 18, 2011 07:07PM
Thank you again

So now I have a mod like this:

Language: PHP
/* phorum module info hook: page_register|mod_register_redirect_page_register hook: after_register|mod_register_redirect_after_register title: Redirect after Registration desc: This module redirects the user to a custom page after registering. priority: run hook after_register after * */   function mod_register_redirect_page_register() { // Buffer output on the register page to prevent "headers already // sent" errors from PHP. ob_start(); }   function mod_register_redirect_after_register($user) { global $PHORUM; phorum_redirect_by_url($PHORUM["DATA"]["URL"]["REDIRECT"]); }

while in a.php executing this:
//include of common.php in head
...
global $PHORUM;
$PHORUM["DATA"]["URL"]["REDIRECT"]='path/to/a.php';

But that doesn't seem toe redirect me after registration to a.php

What have I missed?

Redirecting to a hardcoded page doesn't seem to be that hard. But this seems a whole lot harder. I'm telling myself I'm overlooking something of a puny detail here



Edited 1 time(s). Last edit at 01/18/2011 07:08PM by Maurice Makaay.
Re: After login hook
January 18, 2011 07:16PM
Well, once more then:

There is nothing magical about the $PHORUM variable. It is a plain variable. It is not a session or so. Therefore, setting the URL in a.php would only work if a.php was included and run in the same request as in which your hook is handled.

In other words:

Request 1 to a.php: You set a field in $PHORUM, requests ends, data is gone
Request 2 to register.php: You request the field in $PHORUM, but it's gone

If you need a dynamic redirect URL to redirect to after the register script, then you will have to pass on the URL to the script (e.g. using a "redir" parameter like login.php) and carry that parameter through the register script steps. Forget about putting stuff in $PHORUM, expecting it to be there in the next request.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: After login hook
January 18, 2011 07:29PM
I get it. Thank you.

But if I send a redir param with the register.php
then it too will be gone when I click the button to submit the new users data
I don't know to which script it submints to, but I have to get it somewhere through $_GET['redir'], don't I?

So you suggest I tingle with the registration source code of the phorum software?

Thank again for going through this trouble with me.
I'm not programming web stuff on a daily basis (in case you haven't figured that one out by yourself)
Re: After login hook
January 18, 2011 07:42PM
Never tingle with Phorum's core code. If we thought that was a good idea, we would not have created the module system to begin with.

Frankly speaking, it is pretty clear that you are not doing web programming a lot. The things that you run into are pretty basic.

At the first request to register.php, when providing a redir=... parameter, the data is in $PHORUM['args']['redir']. It is not in $_GET['redir'] because of Phorum's custom URL parsing mechanism.
If you write a module to handle the redirect based on a dynamic URL, you will have to pickup the data from that argument. After that, you could for example add the parameter to $PHORUM['DATA']['POST_VARS']. This fills the template var {POST_VARS}, which is used in the registration form. As a result, when posting the data to the register.php script, the parameter becomes available as $_POST['redir']. That's the basic method of carrying the parameter through multiple requests.

Example code on how to fill the POST_VARS with the data from the redir parameter can be found in login.php.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: After login hook
January 18, 2011 07:44PM
what's also strange is it doesn't seem to work neither if I put the data in a plain old php session

in a.php I write

session_start();
$_SESSION['dest_url']='path/to/a.php';

and my mod becomes:

function mod_register_redirect_page_register()
{
// Buffer output on the register page to prevent "headers already
// sent" errors from PHP.
ob_start();
}

function mod_register_redirect_after_register($user)
{
session_start();
phorum_redirect_by_url($_SESSION['dest_url']);
}

no succes
Re: After login hook
January 18, 2011 07:59PM
So i could write something like this?

function mod_register_redirect_after_register($user)
{
global $PHORUM;
phorum_redirect_by_url($PHORUM['args']['redir']);
}

and that should get me where I want?
Re: After login hook
January 18, 2011 08:04PM
No, that won't work, since after_register will only be after the form POST. $PHORUM['args']['redir'] will only be available at the first GET request to the page.

The reason for $_SESSION to fail, is that Phorum does not startup a session. If you want to use sessions, then you'll have to startup the session yourself for your code.

I think that I'll leave my support for this thread at this. I gave some fairly extended information, but you seem to be missing basic PHP and web development knowledge, which is needed to pick up the interesting bits and put them into working code.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: After login hook
January 18, 2011 08:19PM
Fair enough. One more question and I'll leave it at that.

If some clicks on my link in a.php getting him to register.php?redir=...

It is out of my hands if I'm not supposed to get into that code.

Next time I'm in charge is in the mod. But there the redir isn't available anymore

So I'm not asking anymore what I have to write, but where i have to right it?

Much appreciated if you would give this last question an answer.

Really, thanks a lot!
Re: After login hook
January 18, 2011 08:24PM
Okay, one more answer then and then I'm off (physically as well, since it's way past bed time.) Looking at the code that you already pasted:

It must be available for the first GET request to register.php in the mod function mod_register_redirect_page_register() (that hook is run at the start of every register.php script request).

You won't see it in mod_register_redirect_after_register() though, since that one is only run after the form POSTing has been done (so at least one request later, which is no longer a GET request containing the redir parameter.)


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: After login hook
January 18, 2011 08:35PM
mod function mod_register_redirect_page_register() (that hook is run at the start of every register.php script request).

was my missing link here

thanks a lot!
Sorry, only registered users may post in this forum.

Click here to login