PDA

View Full Version : Checking if user logged in, on external page (VERY simple version required)


Ingenious
03-12-2009, 11:23 AM
Hi everyone,

I would like users of my site to be able to add comments or rate products in a part of my site that is external to VBulletin. To make this simple and prevent abuse/spam etc I would like to allow this only for forum members who are also logged in.

I am using 3.7.1 but shortly updating to 3.8.1

I've had a look over the mods and bits and bobs and there's a fair few scripts that should allow login/logout in external pages. But I want to keep it simpler than that, sending the user off to the actual forum to log in first (logic behind this, let VBulletin worry about login/paswords, cookies etc, seems more secure than me trying to do it).

All I need to do then is check in my script, which for simplicity will be in the same folder as the forum, whether someone is logged in to VBulletin.

I have seen this sort of code:

// We check if user is logged in
if ($vbulletin->userinfo['userid']!=0) {
// If Logged in display welcome back message

Am I right in thinking before that will work I need to include global.php?

If so is this the right code:

require_once('global.php');

Do I need to worry about checking cookies, or anything else, is this all taken care of by global.php?

Finally, what would be the code to check if the user (assuming already checked they are logged in) is of a particular usergroup. Is it this:


if ($vbulletin->userinfo['usergroupid'] == '6' )
{ etc

The above bits were from a script that had this at the top, so the right person is credited:

Simple vB User login and access control on non vB pages
By Bill@Billspaintball.com
Version 2.00 - March 21st, 2008
For vB 3.7.x

Apologies for asking what appears to be something really simple, I did spend a fair time in search however a lot of the stuff relates to more complicated processes and I'm not too sure what bits I need to get started.

Finally, is there a list somewhere of other user info I can get similar to $vbulletin->userinfo['usergroupid'] ie. what other info userinfo['parameter'] can be used to retrieve?

Regards,
Ingenious

Lynne
03-12-2009, 02:09 PM
Here's an article on writing external pages - [How-To] vBulletin API Basics: Creating Custom Pages & Misc. (https://vborg.vbsupport.ru/showthread.php?t=98009) And you'll see that they include global.php and then can use the user variables and templates and such.

The condition for a usergroup would be (using usergroup 6 here):
if (is_member_of($vbulletin->userinfo, 6))
or for multiple usergroups:
if (is_member_of($vbulletin->userinfo, x,y,z))

Ingenious
03-12-2009, 02:37 PM
Thanks for your reply and that link. This has given me the confidence to try a quick PHP script, and this works a treat (I know this is basic stuff, but I am still a beginner!):

<?
require_once('global.php');

if ($vbulletin->userinfo['userid']!=0) {

echo "User is logged in</br>";
echo "Username is: ".($vbulletin->userinfo['username'])."</br>";
echo "Usergroup is: ".($vbulletin->userinfo['usergroupid'])."</br>";

if (is_member_of($vbulletin->userinfo, 5,6)){

echo "User is member of usergroup 5 or 6</br>";}

if (is_member_of($vbulletin->userinfo, 0,1)){

echo "User is member of usergroup 0 or 1</br>";}else{
echo "User is not a member of usergroup 0 or 1</br>";}


}else{

echo "User is not logged in! (put help and link to forum to log in here)</br>";

}
?>

If I am not logged in, it will say so. I can then log in to the forum and just refresh the page above and it shows me as logged in OK :up: (The other stuff was just testing to show it would recognise certain usergroups).

This is great as I can now restrict options and content on my site to only valid (and logged in) forum members.

JOB DONE :D

Lynne
03-12-2009, 02:45 PM
Good to see it worked!

Good idea to start with something basic and work from there.