Helpful Information
 
 
Category: Programming Articles
Verify if user is logged in for non-vb pages, using Jquery

1 - vbconnect.php

<?php
chdir('./YOUR_FORUM_FOLDER_NAME');
define('CVD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once(CVD . '/global.php');

$vbulletin->input->clean_array_gpc('p', array('ajax' => TYPE_BOOL));

if($vbulletin->GPC['ajax'] == true)
{
$result = array(
'username' => $vbulletin->userinfo['username'],
'userid' => $vbulletin->userinfo['userid'],
'userpost' => $vbulletin->userinfo['posts'],
);
echo json_encode($result);
}
?>


2- jquery_cookie.js
Create this file using the code from this page (https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js)

3- jquery_vbulletin.js
Change XXX with any another prefix wich do you want

$(document).ready(function ()
{
$.ajax({
url:'vbconnect.php',
type:'POST',
data:'&ajax=1',
cache:false,
async:false,
dataType:'json',
success:function(result){
$.cookie('XXXuserid', result['userid'], { expires: 7, path: '/' });
$.cookie('XXXusername', result['username'], { expires: 7, path: '/' });
$.cookie('XXXuserpost', result['userpost'], { expires: 7, path: '/' });},
error:function(){
$.cookie('XXXuserid', '', { expires: 7, path: '/' });
$.cookie('XXXusername', '', { expires: 7, path: '/' });
$.cookie('XXXuserpost', '', { expires: 7, path: '/' });}
})
});

4- In your php file (external file and not in directory of the forum)
Change XXX with your prefix using in jquery_vbulletin.js file

<?php

$vb_userid = $_COOKIE['XXXuserid'];
$vb_username = $_COOKIE['XXXusername'];
$vb_userpost = $_COOKIE['XXXuserpost'];

if($vb_userid)
{
// The guest is logged in
// Exemple
$output = "Welcome $vb_username";
}
else
{
// The guest is not logged in
// Exemple
$output = "Welcome " . $_SERVER['REMOTE_ADDR'] . " , you are not connected to the forum";
}

?>


5- Your output (html / php)

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="your/path/to/javascript/directory/jquery_cookie.js"></script>
<script src="your/path/to/javascript/directory/jquery_vbulletin.js"></script>
<?php echo $output; ?>

interesting...

thanks for sharing :up:

nice coding, but what versions are compatible?

@Princeton: with pleasure
@abdobasha2004: vb3 - vb4
--> for vb5 I don't have this version but I think it is the same. You can do a simple test only with vbconnect.php (with little modification)


<?php
chdir('./YOUR_FORUM_FOLDER_NAME');
define('CVD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once(CVD . '/global.php');

$result = array(
'username' => $vbulletin->userinfo['username'],
'userid' => $vbulletin->userinfo['userid'],
'userpost' => $vbulletin->userinfo['posts'],
);
echo json_encode($result);
?>

Go to www.yourdomain.com/vbconnect.php and see the result :)

4- In your php file (external file and not in directory of the forum)
Change XXX with your prefix using in jquery_vbulletin.js file


Hello, If you could please explain about part 4?

I dont really understand if I should create a php file or where its located if there is one! Thank you very much in advance

Hello, If you could please explain about part 4?

I dont really understand if I should create a php file or where its located if there is one! Thank you very much in advance

This would be the file you create, as this is about working with external pages, note the bit that reads "(external file and not in directory of the forum)". This would be the page/file you want to do something like this in:

// The guest is logged in
}
else
{
// The guest is not logged in

.Me










privacy (GDPR)