Page 1 of 1

Sessions..cookies.. help

Posted: Fri Oct 23, 2009 10:49 am
by ctellier
Hi I have a question. I would like my homepage (not my store index.php) to be able to recognize if a customer is logged in. How would I do this? I know it has somthing to do with sessions and or cookies, but I just dont know how to go about it. ???

site is set up like this:
Homepage: /index.php
Store: /store/index.php

Thanks

Re: Sessions..cookies.. help

Posted: Fri Oct 23, 2009 10:21 pm
by ctellier
Bump.

Re: Sessions..cookies.. help

Posted: Fri Oct 23, 2009 11:58 pm
by Qphoria
It uses the php session to store the customer logged in data.
Try the session_start() command on your homepage index.php and i believe it carries over

See the example here: http://php.net/manual/en/function.session-start.php

If logged in, there is a session variable called customer_id

Code: Select all

session_start();
if (isset($_SESSION['customer_id'])) {
echo "Customer is logged in";
}

Re: Sessions..cookies.. help

Posted: Sat Oct 24, 2009 5:03 am
by ctellier
Thanks, works great ;D

Re: Sessions..cookies.. help

Posted: Sat Oct 24, 2009 6:38 am
by Qphoria
really? wow i hadn't even tested it.. i was going on theory! lol Good to know

Re: Sessions..cookies.. help

Posted: Sat Oct 24, 2009 12:05 pm
by ctellier
Yup, but now I need to figure out how to echo or print the firstname? I have it set up like this...

In my main sites header.php i have this

Code: Select all

<?php
if (isset($_SESSION['customer_id'])) {
	include_once "logged.php";
}
else {
	include_once "notlogged.php";
}
?>
Now if a customer is NOT logged in the notlogged.php shows the email and password inputs. which works fine.

My problem is this, I want to echo the customers name in the logged.php. I tried this but nothing...

Code: Select all

<?php echo $this->data['firstname'];?>
I also tried this :

Code: Select all

<?php echo "Welcome $firstname";?>
only welcome shows up..

Re: Sessions..cookies.. help

Posted: Sat Oct 24, 2009 8:45 pm
by Qphoria
Well thats all class stuff loaded from the index.php of the cart. You'd basically need to add session variables to the login process to store all these extra stuff you want to access outside of the store, as that is your common variable base between the two.

EDIT: system/helper/customer.php
FIND:

Code: Select all

$this->session->data['customer_id'] = $customer_query->row['customer_id'];
AFTER, ADD:

Code: Select all

$this->session->data['firstname'] = $customer_query->row['firstname'];
Do the same for any other variables you want to work with. You can see a list of all the available data in that same code area

Then in that same file,
FIND:

Code: Select all

unset($this->session->data['customer_id']);
AFTER, ADD:

Code: Select all

unset($this->session->data['firstname']);
And any other variables you added

Re: Sessions..cookies.. help

Posted: Sun Oct 25, 2009 2:38 am
by ctellier
Ok this is how I did the echo

Code: Select all

<span class="dark2">Welcome Back <?php echo ($_SESSION['firstname']);?>!</span>
And it works. Is that the right way to do it?
Thanks alot!

Re: Sessions..cookies.. help

Posted: Sun Oct 25, 2009 8:15 pm
by Qphoria
Well, if they aren't logged in it might give an error since the variable won't be set. This would be best:

Code: Select all

<span class="dark2">Welcome Back <?php echo (isset($_SESSION['firstname'])) ? $_SESSION['firstname'] : '' ?>!</span>

Re: Sessions..cookies.. help

Posted: Mon Oct 26, 2009 11:46 am
by ctellier
Remember i had this:

Code: Select all

<?php
if (isset($_SESSION['customer_id'])) {
   include_once "logged.php";
}
else {
   include_once "notlogged.php";
}
?>
so if they are not logged it wont even run

Code: Select all

<span class="dark2">Welcome Back <?php echo ($_SESSION['firstname']);?>!</span>
so is that ok? or should i still change the logged.php to what you put?

Re: Sessions..cookies.. help

Posted: Mon Oct 26, 2009 12:26 pm
by Qphoria
Yea thats fine