View Single Post
  #2  
Old 05-07-2006, 08:16 AM
bbcentral's Avatar
bbcentral bbcentral is offline
 
Join Date: Apr 2006
Location: Australia
Posts: 96
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi!
I'm doing exactly this right now, I haven't finished but I managed to get the basics working.

Here's a little background info to explain what MY requirements were:
1) Created a site in 2003, became popular so I built my own integrated forum
2) Switched to phpBB2 in 2004/5, but created custom registration and login pages, as well as change profile etc (all running from one user table).
3) Recently purchased vbulletin (hooked on it! ), in the process of rebuilding my main site and the integrated forum.

What I need is:
-Users will register through my own custom page
-Users shouldn't have to create separate accounts for main site and forum
-Users shouldn't have to login twice, changing between main site and forum should be smooth
-Other functions on main site will feed off user table, including downloads (login to download), comments linked to user profiles


I'm guessing this is similar to what you're wanting to do.

The first thing you should try is creating your own login form.
I found some code here for doing that.

Every page on the site should include this code, we'll save it in a file called 'authentication.inc.php':

PHP Code:
<?php

// any functions related to user authentication

// ########## SET PHP ENVIRONMENT 
error_reporting(E_ALL & ~E_NOTICE);

// ###### DEFINE IMPORTANT CONSTANTS 

define('THIS_SCRIPT''add comment');

// #####REQUIRE BACK-END 
chdir($_SERVER['DOCUMENT_ROOT'].'/forum');
require(
'./global.php');
chdir($_SERVER['DOCUMENT_ROOT']);


// define(DIR, $_SERVER['DOCUMENT_ROOT'].'forum');
// chdir($_SERVER['DOCUMENT_ROOT'].'forum');
// require_once($_SERVER['DOCUMENT_ROOT'].'forum/global.php');

// ##### HARD CODE JAVASCRIPT PATHS
$headinclude str_replace('clientscript'$vbulletin->options['bburl'] . '/clientscript'$headinclude);
?>
This assumes your forum is in '/forum/'. This code gives you access to pretty much all of vbulletin, very handy


On any page that has a login form, you have to include this javascript in your head:
HTML Code:
<script type="text/javascript" src="/forum/clientscript/vbulletin_md5.js">
</script>
The actual login form should be something like this:
PHP Code:
<?php

// Check if logged in
if (is_logged_in()) {

    echo 
"Welcome Back, <b>";
    echo 
$vbulletin->userinfo['username'];
    echo 
" !</b>";
    
    
// As we are logged in display logout link
    // Redirect back to the page you are on right now. The page is /downloads/index.php in this case
    // You have a choice between creating your own logout.php file or linking directly to it
    // Here is linking to it:
    
echo "<a href=\"/forum/login.php?$session[sessionurl]do=logout&amp;url=/downloads/index.php&amp;logouthash=$logouthash";
    echo 
$vbulletin->userinfo['logouthash'];
    echo 
"\">";
    echo 
"<font size=\"1\" face=\"verdana\">Log Out</font></a>";
    
// Here is linking to a logout.php file (see below)
    
echo "<a href=/logout.php?redir=/downloads/index.php>Log Out</a>";

}
else {

    
// Not logged in, so display the login form

    
if($error) {
        
// If an error was encountered, display it
        
echo "<font color=red>".$error."</font><br>";
    }
?><form action="/login.php" method="post" onsubmit="md5hash(vb_login_password,vb_login_md5password,vb_login_md5password_utf);">
<label for="username">User:</label> <input type="text" id="username" name="vb_login_username" class="login" />
<label for="password">Pass:</label> <input type="password" id="password" name="vb_login_password" class="login" />
<label for="cb_cookieuser_navbar">Save Login:</label> <input name="cookieuser" type="checkbox" id="cb_cookieuser_navbar" value="1" checked="checked" />
<input type="hidden" name="s" value="<?=$session['sessionhash']?>" />
<input type="hidden" name="do" value="login" />
<input type="hidden" name="vb_login_md5password" />
<input type="hidden" name="vb_login_md5password_utf" />
<input type="hidden" name="url" value="/downloads/index.php" />
<input type="submit" name="login" alt="Login!" /></form>
<?php
}
// End if not logged in

?>
The login form looks quite complex but it's not as terrible as it seems.
Basically when you submit the form, the javascript file we included above takes the value in the vb_login_password field and encrypts it to md5
It then sets vb_login_password to '', so in your login script you are only interested in the md5password value.

The login.php file is something like this:

PHP Code:
<?php

include "authentication.inc.php";

if (
$vbulletin->userinfo['userid']!=0) { // Logged in already (login not required again)

    
if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) {
        
        
// Redirect back to the page you clicked on the link from
        // Of course, you shouldn't link to 'login.php' when the person is already logged in :)
        
header("Location: ".$_SERVER['HTTP_REFERER']);
    }
    else {
        
        
// If no referer was sent, then go back to the home page
        
header("Location: /");
    }
    
// Another alternative is if already logged in, redirect to the user page (eg account settings, profile etc)
}
else {

    
// User is not logged in
    
if(!empty($_POST)) {

        
// If something was posted to /login.php, then process it
        
        // process the username and password submitted from the login form
        
$username mysql_real_escape_string($_POST['vb_login_username']);
        
$password mysql_real_escape_string($_POST['vb_login_md5password']);

        
// Select the userid from the users table
        // Each user has a field called 'salt', this is used to compare the password
        
$sql  "SELECT `userid` FROM `vb_user` ";
        
$sql .= "WHERE `username`='".$username."' ";
        
$sql .= "AND `password`=md5(CONCAT('".$password."',`salt`)) ";
        
$sql .= "LIMIT 1";
        
        
$rs mysql_query($sql);

        if(
mysql_num_rows($rs) == 1) { 
            
// One row was found, meaning they entered the correct username/password

             
$vbulletin->url=$_POST['url']; // The url to redirect to after login

            
chdir($_SERVER['DOCUMENT_ROOT'].'forum'); // IMPORTANT: Set current directory to forum folder
            
require('./login.php'); // You can't simply load 'forum/login.php', it won't work
            
chdir($_SERVER['DOCUMENT_ROOT']); // SET current dir back to the root folder of site
        
}
        else {
            
// No rows were found, incorrect username or password
            
$error 'Invalid username or password'// error message to be displayed
            
require_once "loginform.inc.php"// redraw the login form, with the error message
        
}

    }
    else {

        
// Else display login form, as nothing was posted, ie the person simply visited /login.php
        
require_once "loginform.inc.php"// Simply the login form seen above
    
}
}
?>


The logout.php file should be something like this:

PHP Code:
<?php

require_once('forum/includes/functions_login.php');

if (
$vbulletin->userinfo['userid']!=0) { // If user is logged in

    
process_logout(); // Log them out

}
// Then redirect or display "you have been logged out" message
// This way if someone visits logout.php when they are logged out already, it will pretend to log them out
// and still redirect correctly
header("Location: /");

?>


I think this covers everything for logging in and out. It should be enough for anyone with a decent understanding of PHP.
I recommend creating some custom functions to speed up the process too, such as:

PHP Code:
<?php
function is_logged_in() {

    global 
$vbulletin;

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

        return 
true;
    }

    return 
false;
}
?>
This just says returns true or false depending on if you are logged in or not.
So you can change this:
PHP Code:
<?php
if ($vbulletin->userinfo['userid']!=0) { // If user is logged in

    
process_logout(); // Log them out

}
?>
into this:
PHP Code:
<?php
if (is_logged_in()) { // If user is logged in

    
process_logout(); // Log them out

}
?>

Any total numbers of members should be easy to get using basic SQL:
"SELECT COUNT(userid) FROM vb_users"

As far as the registration pages go, they should be fairly simple, hopefully just fields that you validate and insert into the database.

I haven't done it yet so I can't say. I hope this helped you, having examples to work from certainly helped me get mine working

And one other thing, once you've logged in successfully, load a page with this code:
PHP Code:
<?php
echo "<pre>";
var_dump($vbulletin);
echo 
"</pre>";
?>
This will show you exactly what you have access to just by including the vbulletin core files. Why select it from the database again when it's already there in an array waiting to be used? :P
(BEWARE: This array is HUGE! You will probably have to run a search to find what you're looking for)

Here is a useful array you might want to var_dump:
$vbulletin->userinfo;
You will use this constantly
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02867 seconds
  • Memory Usage 1,873KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD_SHOWPOST
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_html
  • (8)bbcode_php
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • reputationlevel
  • showthread
Included Files:
  • ./showpost.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showpost_start
  • bbcode_fetch_tags
  • bbcode_create
  • postbit_factory
  • showpost_post
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • showpost_complete