Forums / Templates, Stylesheets, Page Layout / Determine if user is logged in

Determine if user is logged in

Locked

Views: 1,899

Results 1 to 8 of 8
This thread is locked. New replies are disabled.
4 May 2007, 2:20 PM
#1
artcoder avatar

artcoder

Zen Follower

Join Date:
Jan 2007
Posts:
166
Plugin Contributions:
0

Determine if user is logged in

How do I determine if a user session is currently logged in or not?

I'm want to write something like ...

if ( user is logged in ) {
    // display log out link
} else {
   // display log in link
}
 

I know how to display the log in link, but do not know how to write the condition in the if statement. Also I have not figured out what is the logout link yet.

Thanks in advanced.

4 May 2007, 3:08 PM
#2
Kim avatar

Kim

Obaa-san

Join Date:
Jun 2003
Location:
West Coast, North America
Posts:
26,684
Plugin Contributions:
0

Re: Determine if user is logged in

Why are you having to write new code? Look in the tpl_header.php

Please do not PM for support issues: a private solution doesn't benefit the community.

Be careful with unsolicited advice via email or PM - Make sure the person you are talking to is a reliable source.

5 May 2007, 5:08 PM
#3
artcoder avatar

artcoder

Zen Follower

Join Date:
Jan 2007
Posts:
166
Plugin Contributions:
0

Re: Determine if user is logged in

Thanks. I found the code.

The reason was, I had a page that was outside of the catalog which was not using the ZenCart infrastructure. And I wanted it to display the login or logoff link as appropriate.

So now I just added...

<?php if ($_SESSION['customer_id']) { ?>
<a href="catalog/index.php?main_page=login">Log In</a>
<?php } else { ?>
<a href="catalog/index.php?main_page=logoff">Log Out</a>
<?php } ?>

to it.

5 May 2007, 5:57 PM
#4
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Determine if user is logged in

Are you putting that code outside of Zen Cart?

If so, that will be a problem as the $_SESSION['customer_id'] exists inside Zen Cart, and not outside of Zen Cart ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

5 May 2007, 6:25 PM
#5
artcoder avatar

artcoder

Zen Follower

Join Date:
Jan 2007
Posts:
166
Plugin Contributions:
0

Re: Determine if user is logged in

Yes, I put that code outside of the ZenCart installed directory. I know I should be using EZPages instead.

But it seemed to work okay for me. You may be correct that certain code will only work inside the zen cart directory (especially when using zen cart functions). However, the $_SESSION is a global variable in PHP. And that is the only thing I'm using. As long as the user does not close their browser, that variable should be accessible across all pages in the domain. I think. But I could be wrong.

5 May 2007, 8:04 PM
#6
artcoder avatar

artcoder

Zen Follower

Join Date:
Jan 2007
Posts:
166
Plugin Contributions:
0

Re: Determine if user is logged in

I got the code reversed in the above.

It should be ...

<?php if ($_SESSION['customer_id']) { ?>
<a href="catalog/index.php?main_page=logoff">Log Out</a>
<?php } else { ?>
<a href="catalog/index.php?main_page=login">Log In</a>
<?php } ?>
5 May 2007, 8:23 PM
#7
artcoder avatar

artcoder

Zen Follower

Join Date:
Jan 2007
Posts:
166
Plugin Contributions:
0

Re: Determine if user is logged in

Ajeh:

Are you putting that code outside of Zen Cart?

If so, that will be a problem as the $_SESSION['customer_id'] exists inside Zen Cart, and not outside of Zen Cart ...

Hmmm.... Looks like you are correct.

$_SESSION['customer_id'] is returning an empty string to me for PHP pages that are outside my zencart catalog directory (even though I'm logged in).

Is there something in the ZenCart code that is restricting the scope of that variable to be only inside ZenCart? Or am I mis-understanding how the PHP $_SESSION variable works?

6 May 2007, 1:44 AM
#8
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Determine if user is logged in

The session is setup for Zen Cart ... then you step outside of Zen Cart and you lose our session and go into a new one for your other site ...

In Zen Cart you are in a little capsule, so to speak ... and we have the code designed to follow you around where ever you go ... within Zen Cart code ...

So, if you step outside ... poof all gone ... :eek:

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!