Jump to content

whmcs and wordpress logout integration code


Recommended Posts

I want to show client name in wordpress when his is logged in to the WHMCS. What i did is i create a login/ register in wordpress home page and when this client have logged in show his name instead.

 

Below are something i have try out.

 

i create the WHMCS and as subdomain in my wordpress root file.

In my wordpress header.php i entered the following:

 

<?php

 

require_once(ABSPATH. 'whmcs/dbconnect.php');

require_once(ABSPATH. 'whmcs/includes/functions.php');

require_once(ABSPATH. 'whmcs/includes/clientareafunctions.php');

 

if ($_SESSION['uid']) {

$result = mysql_query("SELECT firstname FROM tblclients WHERE id=".(int)$_SESSION['uid']);

$data = mysql_fetch_array($result);

$clientname = $data[0];

echo $clientname;

} else {

echo 'Not logged in';

}

 

?>

 

But these coding doesn't work, instead i received a 'Down for Maintenance' message in wordpress.

 

Anyone have idea what gone wrong here?

Link to comment
Share on other sites

Create a file (say wp-custom.php) in your WHMCS root folder. Start session in that file:

 

<?php
session_start();

 

Now in your wp theme file, use following code(use your whmcs db credentials):

 

require_once "PATH-TO-WHMCS/wp-custom.php";
$db_host = "DB-HOST";
$db_user = "DB-USER";
$db_pass = "DB-USER-PASSWORD";
$db_name = "DB-NAME";
$con = mysql_connect($db_host, $db_user, $db_pass);
$db =  mysql_select_db($db_name, $con);
if ($_SESSION['uid']) {
$name1 = mysql_query("SELECT firstname FROM tblclients WHERE id=".(int)$_SESSION['uid']);
$name2 = mysql_query("SELECT lastname FROM tblclients WHERE id=".(int)$_SESSION['uid']);
 	$data1 = mysql_fetch_array($name1);
 	$data2 = mysql_fetch_array($name2);
 	$firstname = $data1[0];
 	$lastname = $data2[0];

 	$content = '<div> ' . $firstname . ' ' . $lastname . '<br/>';
       echo $content;
}
else{
echo "Not logged in!";
}

mysql_close($con);

You can put this code in function.php and call from anywhere of the theme.

Link to comment
Share on other sites

  • 2 months later...

Great !

 

I try many many, too many things ! For a loooooooooooooong time :-(

 

I can't used include and opendir because my WordPress and my whmcs are on the same shared server in 2 seperate cpanel account.

 

So i try your method, supress the call to php file with the start_session and add it on the top ogf the WordPress header code like that:

 

<?php

session_start();

$db_host = "localhost";

............

 

Now i can access to the database, but he say that i'm not loggin.

 

I'm not developpeur but i understand that you check that with session, and i believe the start_session must be on the whmcs side in the php file....

 

If i'm right, a solution for me were to use cookie for checking login/logout ?

I understand that it is possible to use cookie method on a domain (WordPress) and his subdomain (my whmcs)

 

If anybody can help me before i'm going crazy, or do the job for a little fee...

 

Sorry for my bad English !

Link to comment
Share on other sites

If WHMCS and WP are in two different IPs, session will not work, and will require COOKIE.

 

Great !

 

I try many many, too many things ! For a loooooooooooooong time :-(

 

I can't used include and opendir because my WordPress and my whmcs are on the same shared server in 2 seperate cpanel account.

 

So i try your method, supress the call to php file with the start_session and add it on the top ogf the WordPress header code like that:

 

<?php

session_start();

$db_host = "localhost";

............

 

Now i can access to the database, but he say that i'm not loggin.

 

I'm not developpeur but i understand that you check that with session, and i believe the start_session must be on the whmcs side in the php file....

 

If i'm right, a solution for me were to use cookie for checking login/logout ?

I understand that it is possible to use cookie method on a domain (WordPress) and his subdomain (my whmcs)

 

If anybody can help me before i'm going crazy, or do the job for a little fee...

 

Sorry for my bad English !

Link to comment
Share on other sites

Here is the solution:

 

On very top of your header.tpl file (from whmcs templates), add following code:

 

{if $loggedin}
{php}
	$value = $_SESSION['uid'];
	setcookie("whmcsuid", $value, 0, "/", ".domain.com"); //you should not use www
{/php}
{/if}

 

Now in wordpress, in your theme's functions.php, add:

 

$db_host = "DB-HOST"; /*you need to put IP of your whmcs cPanel, also you will have to add your wp cpanel IP in the remote MySQL option of your WHMCS cpanel. */
$db_user = "DB-USER";
$db_pass = "DB-USER-PASSWORD";
$db_name = "DB-NAME";
$con = mysql_connect($db_host, $db_user, $db_pass);
$db =  mysql_select_db($db_name, $con);
if ($_COOKIE['whmcsuid']) {
   $name1 = mysql_query("SELECT firstname FROM tblclients WHERE id=".(int)$_COOKIE['whmcsuid']);
   $name2 = mysql_query("SELECT lastname FROM tblclients WHERE id=".(int)$_COOKIE['whmcsuid']);
     $data1 = mysql_fetch_array($name1);
     $data2 = mysql_fetch_array($name2);
     $firstname = $data1[0];
     $lastname = $data2[0];

     $content = '<div> ' . $firstname . ' ' . $lastname . '<br/>';
       echo $content;
}
else{
echo "Not logged in!";
}

mysql_close($con);  

Link to comment
Share on other sites

I had a heart attack when i see my name in my header ^^

 

It did not work correctly if i put the code in my header WordPress (break my layout)

But if i put it in a widget area in my header it work's great !

 

Is it a safe way to achieve this ? I mean shall i put in place a login option for publique area in my WHMCS ?

A friend said that i must replace any of session uid i can found in WordPress ?!?

 

Again thank you a lot for your help !

Don't know how to thank you !

Link to comment
Share on other sites

Well, the div was not closed, so it was breaking your layout. Following code will fix it:

 

$db_host = "DB-HOST"; /*you need to put IP of your whmcs cPanel, also you will have to add your wp cpanel IP in the remote MySQL option of your WHMCS cpanel. */
$db_user = "DB-USER";
$db_pass = "DB-USER-PASSWORD";
$db_name = "DB-NAME";
$con = mysql_connect($db_host, $db_user, $db_pass);
$db =  mysql_select_db($db_name, $con);
if ($_COOKIE['whmcsuid']) {
   $name1 = mysql_query("SELECT firstname FROM tblclients WHERE id=".(int)$_COOKIE['whmcsuid']);
   $name2 = mysql_query("SELECT lastname FROM tblclients WHERE id=".(int)$_COOKIE['whmcsuid']);
     $data1 = mysql_fetch_array($name1);
     $data2 = mysql_fetch_array($name2);
     $firstname = $data1[0];
     $lastname = $data2[0];

     $content = '<div> ' . $firstname . ' ' . $lastname . '<br/></div>';
       echo $content;
}
else{
echo "Not logged in!";
}

mysql_close($con);  

 

Without session or cookie, you won't be able to check either an user is logged in or not, and it's not a security leak.

However you should do a check when user is logged out and destroy cookie, or it will show your information on your wp site, as long as you don't close the browser.

Link to comment
Share on other sites

It look like it work better in the functions php than in the widget... But it still break the layout in function.php

However, in widget area it's fine, just in the right place ^^

 

I desactivate WordPress cache plugin and whmcs cache plugin for my Template.

sometimes it show my username correctly. But it clearly won't work has it should.

 

I have a WordPress/whmcs integration with my WordPress header and footer on whmcs, maybe the problem is link.

 

By the way has a system/network engineer i have bypass many of my dev skill...

 

Please PM me if we can make a deal or go further with more reply ^^ or remote assistance...

 

Thank you a lot for your patience !

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated