Zen Cart Logo
Forums / General Questions / how to tell if logged in?

how to tell if logged in?

Locked

Views: 2,150

Results 1 to 15 of 15
This thread is locked. New replies are disabled.
11 Mar 2008, 8:14 PM
#1
adornit avatar

adornit

New Zenner

Join Date:
Jan 2008
Posts:
56
Plugin Contributions:
0

how to tell if logged in?

We want to have this section on the site that only members that are logged in can view. Does anyone know what part of the Zen Cart code to use to verify that someone is a logged in member?

11 Mar 2008, 10:04 PM
#2
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: how to tell if logged in?

There is a session variable you can test for this. Check for $_SESSION['customer_id'] to be not null:

if (!empty($_SESSION['customer_id'])) {
...

Other related variables may also be useful.

11 Mar 2008, 11:01 PM
#3
adornit avatar

adornit

New Zenner

Join Date:
Jan 2008
Posts:
56
Plugin Contributions:
0

Re: how to tell if logged in?

Thanks for the reply. I tried that but it doesn't seem to be working. I have session_start() at the top of the page, and the page I'm working on is in a directory outside of the store if that matters..

11 Mar 2008, 11:09 PM
#4
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: how to tell if logged in?

Well that would make a difference... If you are outside the Zen Cart, its session will not be automatically accessible. PHP pages can be modified somehow to keep the session, but I'm not an expert on that.

12 Mar 2008, 2:07 AM
#5
nhradeuce avatar

nhradeuce

New Zenner

Join Date:
Sep 2006
Posts:
64
Plugin Contributions:
0

Re: how to tell if logged in?

adornit:

Thanks for the reply. I tried that but it doesn't seem to be working. I have session_start() at the top of the page, and the page I'm working on is in a directory outside of the store if that matters..Session variables should work anywhere within the domain so being outside the store should not affect the variable itself. Without seeing your code it's hard to say what the problem is. Are you trying to use Zen functions on your new page or straight PHP? If you are using Zen functions, did you include all the necessary files to allow you to use them?

12 Mar 2008, 4:03 AM
#6
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: how to tell if logged in?

Could the session_start() in your outside page be interfering with the Zen Cart session?

12 Mar 2008, 8:08 PM
#7
adornit avatar

adornit

New Zenner

Join Date:
Jan 2008
Posts:
56
Plugin Contributions:
0

Re: how to tell if logged in?

NHRADeuce:

Session variables should work anywhere within the domain so being outside the store should not affect the variable itself. Without seeing your code it's hard to say what the problem is. Are you trying to use Zen functions on your new page or straight PHP? If you are using Zen functions, did you include all the necessary files to allow you to use them?

Just straight php. The top of the page has session_start() and the only thing I'm using started by zen cart is $_SESSION['customer_id'], but it's not showing anything..

12 Mar 2008, 8:34 PM
#8
adornit avatar

adornit

New Zenner

Join Date:
Jan 2008
Posts:
56
Plugin Contributions:
0

Re: how to tell if logged in?

I'm starting to think there might be a file I need to include to get $_SESSION['customer_id'] to work, but I have no idea what file it would be...

12 Mar 2008, 8:54 PM
#9
nhradeuce avatar

nhradeuce

New Zenner

Join Date:
Sep 2006
Posts:
64
Plugin Contributions:
0

Re: how to tell if logged in?

You should test to see if you have an active session going. Make a page using this code -

<?php

function session_started(){
    if(isset($_SESSION)){ return true; }else{ return false; }
}

//Start the output buffer so we dont create any errors with headers
ob_start();

//Check to see if it has been started
if(session_started()){
    echo 'The session has been started.<br />';
}else{
    echo 'The session has not been started.<br />';
}

//Start the session
echo 'Starting Session...<br />';
session_start();

//Check again
if(session_started()){
    echo 'The session has been started.<br />';
}else{
    echo 'The session has not been started.<br />';
}

//Flush the buffer to screen
ob_end_flush();

?>

Put a link somewhere in Zen to that page, then give it a shot.

12 Mar 2008, 9:22 PM
#10
adornit avatar

adornit

New Zenner

Join Date:
Jan 2008
Posts:
56
Plugin Contributions:
0

Re: how to tell if logged in?

Thanks for the replies. I tried using that, but also adding session_start() at the very top of the page and I got these results..

The session has been started.
Starting Session...
The session has been started.

19 Mar 2008, 8:31 PM
#11
adornit avatar

adornit

New Zenner

Join Date:
Jan 2008
Posts:
56
Plugin Contributions:
0

Re: how to tell if logged in?

does anyone have any other ideas?

26 Mar 2008, 8:55 PM
#12
adornit avatar

adornit

New Zenner

Join Date:
Jan 2008
Posts:
56
Plugin Contributions:
0

Re: how to tell if logged in?

I don't think it matters that its in a different directory. I tried setting a session variable in another folder and could get it to show outside of that. I searched for $_SESSION['customer_id'] in the Zen Cart admin and the only thing I found that CoulD be anything is

$gv_query = $db->bindVars($gv_query, ':customersID', $_SESSION['customer_id'], 'integer');

does anyone know if thats anything? I don't know why it won't work to just put $_SESSION['customer_id'] :(

17 May 2008, 2:29 PM
#13
patchsms avatar

patchsms

New Zenner

Join Date:
May 2008
Posts:
3
Plugin Contributions:
0

Re: how to tell if logged in?

I am having the same problem hoping a bump with more info will help get an answer. My site is setup so that the zen-cart is secondary to primary site(i.e. you connect to primary site and there is an option to connect to store which is a sub directory of public_html. Since Zen-cart already has login/account creation/sql db I want to be able to use those credentials for the site globally where login/registration is needed to see protected areas. as a test I have the following code in the main page:

<div id="Loginhere"> <p>Welcome <?php if (isset($_SESSION['customer_id']) and $_SESSION['customer_id'] != '') {echo $_SESSION['customer_id']; } else { echo 'Guest'; }?></p> <p> <a href="https://mysite.com/zen-cart/index.php?main_page=login">Registration/Login</a> </p> </div>

What happens is new user clicks the link gets redirected to the zen login/account creation page and all is good. Now comes the problem. Navigate back to the main page and as far as it is concerned the user is still a Guest. If I click the Registration/Login it will take me back to the zen cart page and happily identify me as the previously registered user. (i.e. the session variables are still valid)

It's as if the zen-cart area of the site is a seperate "namespace" so to speak and the rest of the site has no clue about the session data.

I don't want to embed the rest of my site within Zen-cart as that makes no sense for what it's about. The store is a sub component NOT the site focus.

Any help would be greatly appreciated
:lamo:

26 Jun 2008, 10:47 PM
#14
swfu avatar

swfu

New Zenner

Join Date:
Jun 2008
Posts:
6
Plugin Contributions:
0

Re: how to tell if logged in?

Did anyone find a solution to this?

3 Jul 2008, 11:14 PM
#15
garrettdavis avatar

garrettdavis

New Zenner

Join Date:
Apr 2008
Posts:
7
Plugin Contributions:
0

Re: how to tell if logged in?

Looking for an answer to this to, would like to check if a visitor is logged in to zen or not outside of zen-cart in the frontend of the site, if not redirect to the login/sign-up page, if they are display the page content. It's a php page on the same site/domain, but no session data is available once I'm logged in and visit the page after logging in to zen.