View Full Version : How to connect to a vB database & sessions
kill_emma
10-14-2003, 10:30 PM
Right, here's what i'm trying to do, i'm on vB3 Beta 7, and i'm trying to connect to the database and use the vB permisions and the whois online thinger, but i dont know how. Let me plug in this info right now, because i'm stupid and i'm running on localhost so this info is safe.
Server- localhost
Database- forums
Username- root
Password-
(I dont use a pass)
And yes you can discuss this because it's not vB3 hacking, you arent editing any vB files, just accessing them and the database. The only thing i'm editing is my index page on my root site :P.
bradsears
10-15-2003, 03:59 PM
I'm a junior vb hacker but I think the first thing you want to do is take a look at global.php
Then you can include it in your script and essentially you get access to some good functions and variables.
I've had no success using sessions after including vb3beta7 global.php (see my other thread) ... because I think vbulletin clears the session every time global.php runs.
NTLDR
10-15-2003, 04:05 PM
<?
chdir('/path/to/forums');
require_once('./global.php');
// your code here
?>
kill_emma
10-15-2003, 06:45 PM
chdir('./forum');
require_once('./global.php');
$db = mysql_connect ('localhost','root','');
mysql_select_db ('general',$db);
i'm using that and it aint workin.
NTLDR
10-15-2003, 07:52 PM
Requiring global.php creates a database connection to you vBulletin database and creates the users session for you.
kill_emma
10-15-2003, 09:02 PM
ok, so now i'm using:
<?php
chdir('forum');
require_once('./global.php');
echo "members: $numbermembers<br>" .
"threads: $totalthreads<br>" .
"posts: $totalposts<br>" .
"users online: $totalonline<br>";
?>
no workie, i get this error:
Warning: chdir(): No such file or directory (errno 2) in C:\network\Apache2\htdocs\index.php on line 51
line 51 of course being chdir('forum');
NTLDR
10-15-2003, 09:17 PM
chdir('forum');
The above isn't valid, enter the full path to the forums directory and it will work. Note that the total threads posts etc won't be displayed even if it works.
kill_emma
10-15-2003, 09:19 PM
why wont it work? :(
NTLDR
10-15-2003, 09:28 PM
Because you actually have to get the values to fill the variables, they aren't done automatically by requiring global.php.
kill_emma
10-16-2003, 06:09 PM
how are they done if i may ask :P
NTLDR
10-16-2003, 06:13 PM
All the code to get that information can be found in index.php :)
Assuming you are using in your 'htdocs' a /forum folder:
<?php
require_once('./forum/global.php');
$memberscount = $DB_site->query_first("
SELECT COUNT(*) AS total,MAX(userid) AS max FROM user
");
$memberstotal = number_format($memberscount['total']);
$threadscount = $DB_site->query_first("
SELECT COUNT(*) AS total FROM thread
");
$threadstotal = number_format($threadscount['total']);
$postscount = $DB_site->query_first("
SELECT COUNT(*) AS total FROM post
");
$poststotal = number_format($postscount['total']);
if ($displayloggedin)
{
$datecut = $ourtimenow - $cookietimeout;
$loggedins = $DB_site->query_first("
SELECT COUNT(*) AS sessions
FROM session
WHERE userid=0
AND lastactivity>$datecut
");
$numberguest = $loggedins['sessions'];
$numberregistered = 0;
$loggedins = $DB_site->query("
SELECT DISTINCT session.userid,username,invisible,usergroupid
FROM session
LEFT JOIN user ON (user.userid=session.userid)
WHERE session.userid>0
AND session.lastactivity>$datecut
ORDER BY invisible ASC, username ASC
");
if ($loggedin = $DB_site->fetch_array($loggedins))
{
$numberregistered++;
while ($loggedin = $DB_site->fetch_array($loggedins))
{
$numberregistered++;
}
}
$DB_site->free_result($loggedins);
$onlinetotal = $numberregistered + $numberguest;
}
$totalposts = number_format($countposts['posts']);
print_r('Members: ' . $memberstotal . '<br />');
print_r('Threads: ' . $threadstotal . '<br />');
print_r('Posts: ' . $poststotal . '<br />');
print_r('Online Users: ' . $onlinetotal);
?>
kill_emma
10-17-2003, 01:52 AM
Warning: main(./includes/init.php): failed to open stream: No such file or directory in C:\network\Apache2\htdocs\forum\global.php on line 18
Fatal error: main(): Failed opening required './includes/init.php' (include_path='.;c:\php4\pear') in C:\network\Apache2\htdocs\forum\global.php on line 18
btw: yes, i'm using htdocs/forum :P
EDIT:
i redid the top part to this
chdir('c:\network\Apache2\htdocs\forum');
require_once('./global.php');
and now i'm getting:
vBulletin Message
Unable to add cookies, header already sent.
File: C:\network\Apache2\htdocs\index.php
Line: 26
line 26 is where the table my page begins on is.
chdir('./forum');
require_once('./global.php');
kill_emma
10-17-2003, 06:19 PM
still getting the same message
bradsears
10-17-2003, 06:28 PM
Make sure you do the includes before your actual html .... you can't have any html tags before the include.
The key for you is "header already sent".
Some things need to be done by the server before the page starts being sent. This includes redirects but it would not surprise me if cookies were included as well.
Start with an empty script and test things step by step.
kill_emma
10-17-2003, 09:35 PM
w00t, now it works.
2 things then i'm done!
1: everything will work except for the users online... why?
2: how would i display a user's info from the getinfo template (ie $userinfo[avatarurl])?
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.