MyBB Community Forums

Full Version: Quick question about session variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm building a php/mysql site that I want to integrate with a mybb forum. I'm going to use the mybbusers table to house the user info for the other site as well. My question is how do I carry the logged in users' names over to the other site when they leave the forum section? I've tried printing the session variables to the screen, but I just get NULL.

Both sites will be on the same domain, the forums will just be in a "forums" folder in the root. If someone could brief me on the user cookie/session info in mybb, I would greatly appreciate it.

Thanks,
DAStaten
If someone could just tell me the php code to grab a logged in user's username and set it as a session variable, that would work.

Thanks.
I've tried using

$_SESSION['username']

but that's not working...
I've tried

<?php

session_start();
echo $_SESSION['username'];

?>

but that doesn't print anything to the screen. I need to call the session username to carry it to another section of my site that I am building. Any help would be greatly appreciated.

Thanks.
$mybb->user['username'] shows username of logged user.
(2008-07-24, 10:13 PM)DragonFever Wrote: [ -> ]$mybb->user['username'] shows username of logged user.

That is correct; the only condition is that you must include MyBB's global.php before you can access the $mybb variable. MyBB does not use PHP sessions.

<?php
define('IN_MYBB', 1);
require './global.php'; // assuming this file is in the same folder as MyBB

echo $mybb->user['username']; // This would print out the current user's username, or nothing for a guest
?>
Also, please do not create duplicate threads. You may bump your existing thread after 24 hours of no other posts.
You got it, Dennis. Thanks for the assistance.
I just tried that code and I'm getting...

Direct initialization of this file is not allowed.

Please make sure IN_MYBB is defined.
Before the "require './global.php';" line, add:
define('IN_MYBB', 1);

I've updated my previous post.
That did the trick. Thanks!
Ok, I've got my forum uploaded into a folder, "forums", and I'm having a problem since the forum is not in the root directory. Can you tell me what is wrong with this:

<?php

define('IN_MYBB', 1);
require './forums/global.php';

session_start();

$_SESSION['username'] = $mybb->user['username'];

$username = $_SESSION['username'];

echo $username;

?>

I'm getting this error:

Warning: require_once() [function.require-once]: Unable to access ./inc/init.php in /misc/38/000/164/778/0/user/web/thewritershome.com/forums/global.php on line 13

Warning: require_once(./inc/init.php) [function.require-once]: failed to open stream: No such file or directory in /misc/38/000/164/778/0/user/web/thewritershome.com/forums/global.php on line 13

Fatal error: require_once() [function.require]: Failed opening required './inc/init.php' (include_path='.:/usr/share/pear') in /misc/38/000/164/778/0/user/web/thewritershome.com/forums/global.php on line 13


I know it's the path because if I put the test.php in the forums folder it works. I'm just not sure how to change "./global.php" to reflect the new folder structure.
I tried this and I'm still getting the same error:

require ($_SERVER["DOCUMENT_ROOT"]."/forums/global.php");
Ok, I figured it would be best to set the session in member.php when a user logs in. I've tried adding the following code at the end up member.php, but it's still not setting the session. Am I missing something?

session_start();
$_SESSION['username'] = $mybb->user['username'];

Ok, I've tried several ways to do this and the issues have changed each time, so I thought I'd sum up where I'm at and what I'm trying to do now.

I just need to set the logged in user's username as a session variable called "username" so that when they go to a non forum section of my site, I can still call up their username with the session variable. I'm not sure what the code should be or where it should be placed. If someone could provide the solution, I would be forever grateful.

Thanks.
(2008-07-25, 04:39 PM)dastaten Wrote: [ -> ]Can you tell me what is wrong with this:
require './forums/global.php';
require ($_SERVER["DOCUMENT_ROOT"]."/forums/global.php");

Proper way is:
chdir('./forums');
require './global.php';

(2008-07-26, 06:06 PM)dastaten Wrote: [ -> ]Ok, I figured it would be best to set the session in member.php when a user logs in. I've tried adding the following code at the end up member.php, but it's still not setting the session. Am I missing something?

session_start();
$_SESSION['username'] = $mybb->user['username'];

Ok, I've tried several ways to do this and the issues have changed each time, so I thought I'd sum up where I'm at and what I'm trying to do now.

I just need to set the logged in user's username as a session variable called "username" so that when they go to a non forum section of my site, I can still call up their username with the session variable. I'm not sure what the code should be or where it should be placed. If someone could provide the solution, I would be forever grateful.

Thanks.

I'm not sure why you need $_SESSION once you get $mybb working. Try the above solution first, you'll be able to grab the username by the $mybb->user['username'] variable.