Firefox PHP

If not logged in, redirect to login.php - How?

Posted by Robert Angle 
If not logged in, redirect to login.php - How?
August 01, 2009 01:58PM
I want users who visit the forum to be directed immediately to the log in page instead of having to click the login link at the top of the page.

- Bob
Re: If not logged in, redirect to login.php - How?
August 01, 2009 03:13PM
The Phorum Lockdown Module might be of help here. IIRC, that module allows you to redirect the users to the login page if they are not logged in. The module does have a whole bunch of other feature. You'd have to check whether it allows you to only use the login redirection. I don't remember the specifics.

If the module does more than needed, it should be possible to strip it down to only have the login redirection feature in a module.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: If not logged in, redirect to login.php - How?
August 01, 2009 03:37PM
Thanks, I'll look into that module.

I've been trying to find where in the code or template files it says "Sorry, no forums to show here", figuring that's where I could make my modification.

Unfortunately I find trying to search for this phrase using XP impossible.

- Bob
Re: If not logged in, redirect to login.php - How?
August 01, 2009 03:52PM
All language strings are in the language files, so that's where you should look for the string. Then you can search using the key for the appropriate language string. This one is triggered from the code probably.

But apart from how to search for the language strings, remember that the required feature does need to redirect the user on other pages as well (e.g. the list or read page). So only changing this particular spot would not be enough.

I think that it's possible to handle this from the header template too. At the start of that template, you could add some code to see if the user is logged in, e.g.

{IF NOT LOGGEDIN}
  {IF NOT PHORUM_PAGE "login" AND NOT PHORUM_PAGE "register"}
    ... redirect to the login page ...
  {/IF}
{/IF}

Something like that. But better start with checking out the module, since that might do the trick already.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: If not logged in, redirect to login.php - How?
August 01, 2009 05:09PM
I get the following error when I activate this mod...

Quote

Forbidden

You don't have permission to access /phorum/login.php on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I'm gonna go digging through it though. I've found where I can get what I want by modifying the index_new and index_classic files, but would rather not do that as I would forget about it when upgrading.

- Bob
Re: If not logged in, redirect to login.php - How?
August 01, 2009 06:19PM
Alright, I found a solution that is going to work for me and probably would others who maintain a private forum for registered users. Instead of seeing the message "Sorry, no forums to show here", I had wanted a redirect to the log in page. However, the log in page can be displayed on the index.php page by modifying your message.tpl file as follows...


{IF ERROR}<div class="attention">{ERROR}</div>{/IF}

{IF OKMSG}
<div class="information">
{OKMSG}
{IF URL->CLICKHERE}
<p><a href="{URL->CLICKHERE}">{CLICKHEREMSG}</a></p>
{/IF}
{IF URL->REDIRECT}
<p><a href="{URL->REDIRECT}">{BACKMSG}</a></p>
{/IF}
</div>
{/IF}

<--THIS IS THE PART THAT IS ADDED -->

{IF NOT LOGGEDIN}
<div class="generic">
<table border="0" cellspacing="0" class="form-table">
<tr>
<td width="50%" valign="top">

<h4>Enter your username and password to log in.</h4>
<form action="url.to.your/phorum/login.php" method="post">
<input type="hidden" name="forum_id" value="0" />
<input type="hidden" name="redir" value="url.to.your/phorum/index.php" />

Username:<br />
<input type="text" id="username" name="username" size="30" value="" /><br />
<br />
Password:<br />
<input type="password" id="password" name="password" size="30" value="" /><br />
<br />
<input type="submit" value="Submit" />
</form>

</td>
<td width="50%" valign="top">
<h4>Did you forget your password?</h4>
<form action="url.to.your/phorum/login.php" method="post">
<input type="hidden" name="forum_id" value="0" />
<input type="hidden" name="redir" value="url.to.your/phorum/index.php" />

Enter your email address below and a new password will be sent to you.<br />
<br />
<input type="text" name="lostpass" size="30" value="" /><br />
<br />
<input type="submit" value="Submit" />
</form>
</td>
</tr>
</table>
</div>

&nbsp;<BR>

<div class="information"><a href="url.to.your/phorum/register.php">Not Registered? Click here to register now.</a></div>

{/IF}


I cannot think of any other instance where the message.tpl file would be utilized on a forum like mine that is totally private, so this should work out great for me.

Hope this benefits someone else too.

- Bob
Re: If not logged in, redirect to login.php - How?
August 01, 2009 06:31PM
This way, the session will probably be initialized without cookie support. The normal login.php script will set a cookie to let the browser proof that it has cookie support.
If this template trick works for you then:

- add <?php ob_start() ?> to the very start of header.tpl.

- add code to set a cookie to your code from above.

Language: PHP
<?php global $PHORUM; setcookie( "phorum_tmp_cookie", "this will be destroyed once logged in", 0, $PHORUM["session_path"], $PHORUM["session_domain"]); ?>


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: If not logged in, redirect to login.php - How?
August 02, 2009 09:24PM
Thank you Maurice.

Yes, this template trick seems to be working flawlessly. I added the single line to the very top of header.tpl and the other code within the IF NOT LOGGED IN portion and it works great. I can tell the cookie gets set now because the query string no longer shows in the address bar.

I think this has turned out to be the perfect solution for me :-)

- Bob
Re: If not logged in, redirect to login.php - How?
August 03, 2009 02:08AM
Good! We like perfect solutions ;-)
Have fun with it.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Sorry, only registered users may post in this forum.

Click here to login